package com.airlock.iam.oauth2.infrastructure.mapper;

import com.airlock.iam.oauth2.domain.model.session.OAuth2ExtendedSession;
import com.airlock.iam.oauth2.infrastructure.transfer.response.OAuth2CustomAttributesData;
import ma.glasnost.orika.CustomMapper;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.MappingContext;

public final class OAuth2ExtendedSessionToCustomAttributesDataMapper {
   public OAuth2ExtendedSessionToCustomAttributesDataMapper(MapperFactory factory) {
      this.configure(factory);
   }

   private void configure(MapperFactory factory) {
      factory.classMap(OAuth2ExtendedSession.class, OAuth2CustomAttributesData.class)
         .customize(new OAuth2ExtendedSessionToCustomAttributesDataMapper.OAuth2ExtendedSessionToCustomAttributesDataCustomMapper())
         .register();
   }

   public static final class OAuth2ExtendedSessionToCustomAttributesDataCustomMapper extends CustomMapper<OAuth2ExtendedSession, OAuth2CustomAttributesData> {
      public void mapAtoB(OAuth2ExtendedSession source, OAuth2CustomAttributesData target, MappingContext context) {
         source.customAttributes().forEach(attribute -> target.addCustomAttribute(attribute.getName(), attribute.getValue()));
      }
   }
}
