import'path';import {batch}from'stream-json/utils/Batch';/* eslint-disable import/no-unresolved */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
const DB_FILES_PATH = './fixtures/countries_states_cities.json.js';
const loadAllCities = async (f) => {
    let count = 0;
    try {
        // Dynamically import the JSON data
        const dataModule = await import(DB_FILES_PATH);
        const data = dataModule.default; // Access the default export where the JSON data is expected to be
        // Assuming the structure of your JSON allows direct access to states and cities
        data.forEach((country) => {
            country.states.forEach((state) => {
                const cities = state.cities.map((city) => {
                    const cityData = {
                        _id: city.id,
                        name: city.name,
                        state: state.id,
                    };
                    if (city.latitude != null && city.longitude != null) {
                        cityData.location = {
                            type: 'Point',
                            coordinates: [parseFloat(city.longitude), parseFloat(city.latitude)],
                        };
                    }
                    return cityData;
                });
                // Process batch of cities
                batch({ batchSize: 1000 })(cities, null);
                if (f) {
                    f(cities);
                }
                count += cities.length;
            });
        });
        return Promise.resolve(count);
    }
    catch (error) {
        return Promise.reject(error);
    }
};
const loadAllStates = async (f) => {
    let count = 0;
    try {
        // Dynamically import the JSON module
        const dataModule = await import(DB_FILES_PATH);
        const data = dataModule.default; // Assuming the default export is the JSON data
        // Iterate over each country to process its states
        data.forEach((country) => {
            country.states.forEach((state) => {
                const stateData = {
                    _id: state.id,
                    name: state.name,
                    country: country.id,
                };
                // Only add the location field if latitude and longitude are provided
                if (state.latitude != null && state.longitude != null) {
                    stateData.location = {
                        type: 'Point',
                        coordinates: [parseFloat(state.longitude), parseFloat(state.latitude)],
                    };
                }
                // Here you might want to process or persist the state data
                if (f) {
                    f(stateData);
                }
                count++;
            });
        });
        return count; // Return the total count of processed states
    }
    catch (error) {
        console.error('Failed to load or process states:', error);
        throw error;
    }
};
/**
 * this function is responsible of accession the file, read the content,
 * and parse it into json and return the result when done
 */
const loadAllCountries = async (f) => {
    let count = 0;
    try {
        // Dynamically import the JSON data
        const dataModule = await import(DB_FILES_PATH);
        const countries = dataModule.default; // Assuming the default export contains the countries data
        // Process the countries data
        countries.forEach((country) => {
            // Construct the country data based on the expected format and types
            const countryData = {
                name: country.name,
                _id: country.id,
                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)],
                },
            };
            // Here you would use your persist function or any other logic
            // to handle the processed country data
            if (f) {
                f(countryData);
            }
            count++;
        });
        return Promise.resolve(count);
    }
    catch (error) {
        console.error('Failed to load or process countries:', error);
        return Promise.reject(error);
    }
};export{loadAllCities,loadAllCountries,loadAllStates};