import type { key_val } from '$lib/stores/ae_stores';
import { api } from '$lib/api/api';

import { db_core } from '$lib/ae_core/db_core';

const ae_promises: key_val = {};

// Updated 2024-10-14
export async function load_ae_obj_li__country_subdivision({
    api_cfg,
    // account_id,
    enabled = 'enabled',
    hidden = 'not_hidden',
    limit = 3500, // There are roughly 3434 as of 2026-02
    offset = 0,
    order_by_li = { sort: 'DESC', name: 'ASC', code: 'ASC' } as const,
    params = {},
    try_cache = true,
    log_lvl = 0
}: {
    api_cfg: any;
    // account_id: string,
    enabled?: 'enabled' | 'all' | 'not_enabled' | undefined;
    hidden?: 'hidden' | 'all' | 'not_hidden' | undefined;
    limit?: number;
    offset?: number;
    order_by_li?: key_val;
    params?: key_val;
    try_cache?: boolean;
    log_lvl?: number;
}) {
    if (log_lvl) {
        console.log(`*** load_ae_obj_li__country_subdivision() ***`);
    }

    const params_json: key_val = {};

    // console.log('params_json:', params_json);

    ae_promises.load__country_subdivision_li = await api
        .get_ae_obj_li_for_lu({
            api_cfg: api_cfg,
            for_lu_type: 'country_subdivision',
            enabled: enabled,
            hidden: hidden,
            limit: limit,
            offset: offset,
            params: params,
            log_lvl: log_lvl
        })
        .then(function (country_subdivision_li_get_result) {
            if (country_subdivision_li_get_result) {
                // handle_db_save_ae_obj_li__country_subdivision({obj_type: 'country_subdivision', obj_li: country_subdivision_li_get_result});
                return country_subdivision_li_get_result;
            } else {
                return [];
            }
        })
        .catch(function (error: any) {
            console.log('No results returned or failed.', error);
        });

    console.log(
        'ae_promises.load__country_subdivision_li:',
        ae_promises.load__country_subdivision_li
    );
    return ae_promises.load__country_subdivision_li;
}
