package com.airlock.iam.base.infrastructure.mapper.entity;

import com.airlock.iam.base.api.domain.model.Entity;
import com.airlock.iam.base.infrastructure.mapper.jsonapi.JsonApiCollectionMapper;
import com.airlock.iam.base.infrastructure.mapper.jsonapi.JsonApiResourceMapper;
import jakarta.inject.Inject;
import java.util.Collection;
import ma.glasnost.orika.MapperFacade;

public class RestMapper {
   private final MapperFacade orikaMapper;

   @Inject
   public RestMapper(MapperFacade orikaMapper) {
      this.orikaMapper = orikaMapper;
   }

   public <T extends Entity<T>, R> JsonApiResourceMapper<T, R> forEntity(T entity, Class<R> target) {
      return new JsonApiResourceMapper<T, R>(this.orikaMapper, entity, target).withIdSupplier(new EntityIdSupplier<>());
   }

   public <T extends Entity<T>, R> JsonApiCollectionMapper<T, R> forEntities(Collection<T> entities, Class<R> target) {
      return new JsonApiCollectionMapper<>(this.orikaMapper, entities, target).withIdSupplier(new EntityIdSupplier<>());
   }

   public <T, R> JsonApiResourceMapper<T, R> forElement(T element, Class<R> target) {
      return new JsonApiResourceMapper<>(this.orikaMapper, element, target);
   }

   public <T, R> JsonApiCollectionMapper<T, R> forElements(Collection<T> elements, Class<R> target) {
      return new JsonApiCollectionMapper<>(this.orikaMapper, elements, target);
   }

   public <T, R> R map(T source, Class<R> target) {
      return (R)this.orikaMapper.map(source, target);
   }

   public <R> R convert(Object source, Class<R> target) {
      return (R)this.orikaMapper.convert(source, target, null, null);
   }
}
