import*as path from'path';import {chunk}from'lodash-es';/* eslint-disable import/no-unresolved */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
const DB_FILES_PATH = './fixtures/countries_states_cities.json.js';
// Utility function to resolve paths in an AMD environment
function getPathFromAmdModule(relativePath) {
  return path.resolve(__dirname, relativePath);
}
const isValidArray = data => Array.isArray(data);
const loadAllCities = async f => {
  let count = 0;
  try {
    // Dynamically import the JSON data
    const dataModule = await import(getPathFromAmdModule(DB_FILES_PATH));
    const countries = dataModule.default.countries; // Access the countries array within the default export
    if (!isValidArray(countries)) {
      throw new TypeError('Expected an array of countries');
    }
    for (const country of countries) {
      if (!isValidArray(country.states)) {
        throw new TypeError('Expected an array of states');
      }
      for (const state of country.states) {
        if (!isValidArray(state.cities)) {
          throw new TypeError('Expected an array of cities');
        }
        const citiesData = state.cities.map(city => {
          const cityData = {
            _id: city.id,
            name: city.name,
            state: state.id,
            createdAt: new Date(),
            updatedAt: new Date()
          };
          if (city.latitude != null && city.longitude != null) {
            cityData.location = {
              type: 'Point',
              coordinates: [parseFloat(city.longitude), parseFloat(city.latitude)]
            };
          }
          return cityData;
        });
        await Promise.all(chunk(citiesData, 1000).map(async cityChunk => {
          await f?.(cityChunk);
          count += cityChunk.length;
        }));
      }
    }
    return count;
  } catch (error) {
    console.error('Failed to load or process cities:', error);
    throw error;
  }
};
const loadAllStates = async f => {
  let count = 0;
  try {
    // Dynamically import the JSON module
    const dataModule = await import(getPathFromAmdModule(DB_FILES_PATH));
    const countries = dataModule.default.countries; // Access the countries array within the default export
    if (!isValidArray(countries)) {
      throw new TypeError('Expected an array of countries');
    }
    for (const country of countries) {
      if (!isValidArray(country.states)) {
        throw new TypeError('Expected an array of states');
      }
      const statesData = country.states.map(state => {
        const stateData = {
          _id: state.id,
          name: state.name,
          country: country.id,
          createdAt: new Date(),
          updatedAt: new Date()
        };
        if (state.latitude != null && state.longitude != null) {
          stateData.location = {
            type: 'Point',
            coordinates: [parseFloat(state.longitude), parseFloat(state.latitude)]
          };
        }
        return stateData;
      });
      await f?.(statesData);
      count += statesData.length;
    }
    return count;
  } catch (error) {
    console.error('Failed to load or process states:', error);
    throw error;
  }
};
const loadAllCountries = async f => {
  let count = 0;
  try {
    // Dynamically import the JSON data
    const dataModule = await import(getPathFromAmdModule(DB_FILES_PATH));
    const countries = dataModule.default.countries; // Access the countries array within the default export
    if (!isValidArray(countries)) {
      throw new TypeError('Expected an array of countries');
    }
    const countriesData = countries.map(country => {
      const countryData = {
        _id: country.id,
        name: country.name,
        phone_code: country.phone_code,
        currency: country.currency,
        currency_symbol: country.currency_symbol,
        emoji: country.emoji,
        location: {
          type: 'Point',
          coordinates: [parseFloat(country.longitude), parseFloat(country.latitude)]
        },
        createdAt: new Date(),
        updatedAt: new Date()
      };
      return countryData;
    });
    await f?.(countriesData);
    count = countriesData.length;
    return count;
  } catch (error) {
    console.error('Failed to load or process countries:', error);
    throw error;
  }
};export{getPathFromAmdModule,loadAllCities,loadAllCountries,loadAllStates};//# sourceMappingURL=load-utils.js.map
