package com.red.circle.other.infra.convertor.sys;

import com.red.circle.common.business.core.util.CountryCodeAliasUtils;
import com.red.circle.framework.core.convertor.ConvertorModel;
import com.red.circle.other.infra.database.rds.entity.sys.SysCountryCode;
import com.red.circle.other.inner.model.cmd.sys.SysCountryCodeCmd;
import com.red.circle.other.inner.model.dto.sys.SysCountryCodeDTO;
import java.util.List;
import java.util.Map;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

/**
 * @author pengliang on 2023/5/24
 */
@Mapper(componentModel = ConvertorModel.SPRING)
public interface CountryCodeInnerConvertor {

  @Mapping(target = "countryCodeAliases", source = ".")
  SysCountryCodeDTO toSysCountryCodeDTO(SysCountryCode sysCountryCode);

  List<SysCountryCodeDTO> toListSysCountryCodeDTO(List<SysCountryCode> sysCountryCodes);

  Map<Long, SysCountryCodeDTO> toMapLongSysCountryCodeDTO(
      Map<Long, SysCountryCode> sysCountryCodeMap);

  @Mapping(target = "aliasCodes", source = "countryCodeAliases")
  SysCountryCode toSysCountryCode(SysCountryCodeCmd cmd);

  default List<String> mapCountryCodeAliases(SysCountryCode sysCountryCode) {
    if (sysCountryCode == null) {
      return List.of();
    }
    return CountryCodeAliasUtils.normalizeCodes(
        CountryCodeAliasUtils.buildMatchCodes(sysCountryCode.getAlphaTwo(), sysCountryCode.getAlphaThree(),
            CountryCodeAliasUtils.parseAliasCodes(sysCountryCode.getAliasCodes())));
  }

  default String mapCountryCodeAliases(List<String> countryCodeAliases) {
    return CountryCodeAliasUtils.toAliasCodesJson(countryCodeAliases);
  }

}
