import { defaultCountries, parseCountry } from 'react-international-phone';

import taxCodesRaw from './taxCodes.json';

const taxCodesById: Record<string, any> = taxCodesRaw as unknown as Record<string, any>;

export const getTaxCodeInfo = (taxCode: string, locale: string = 'en') => {
  if (!taxCode || !taxCodesById[taxCode]) {
    return null;
  }

  const taxCodeData = taxCodesById[taxCode];
  const language = locale.startsWith('zh') ? 'zh' : 'en';

  return {
    id: taxCode,
    name: taxCodeData.name?.[language] || taxCodeData.name?.en || taxCode,
    description: taxCodeData.description?.[language] || taxCodeData.description?.en || '',
    type: taxCodeData.type,
  };
};

export const getCountryInfo = (countryCode: string) => {
  if (!countryCode) {
    return null;
  }

  const match = defaultCountries.find((item) => item[1] === countryCode.toLowerCase());
  if (!match) {
    return {
      iso2: countryCode.toLowerCase(),
      name: countryCode.toUpperCase(),
      dialCode: '',
    };
  }

  return parseCountry(match);
};
