/** @type {import('./$types').LayoutLoad} */

import { get } from 'svelte/store';

import { api } from '$lib/api';
import { ae_loc, ae_sess, ae_api, slct, slct_trigger } from '$lib/ae_stores';
import { events_loc, events_slct, events_trigger } from '$lib/ae_events_stores';


export async function load({ params, parent, url }) { // route
    // console.log(`Svelte Event Speakers layout.ts data = params:`, params);
    // console.log(`Svelte Event Speakers layout.ts data = route:`, route);
    // console.log(`Svelte Event Speakers layout.ts data = url:`, url);

    let data = await parent();
    console.log(`Svelte Events Speakers layout.ts data = data:`, data);

    data.ae_events_speakers_layout_ts = true;


    let event_id = url.searchParams.get('event_id');
    if (!event_id) {
        // event_id = $ae_loc.site_cfg_json.slct__event_id;
        event_id = data.ae_loc.site_cfg_json.slct__event_id;
    }

    await handle_load_ae_obj_id__event({event_id:  event_id, try_cache: false})
    .then(function (event_obj_results) {
        if (event_obj_results) {
            console.log(`event_obj_results = `, event_obj_results);

            let ae_loc_tmp = get(ae_loc);
            ae_loc_tmp.event_id = event_obj_results.event_id_random;
            ae_loc_tmp.mod.events.event_id = event_obj_results.event_id_random;
            ae_loc.set(ae_loc_tmp);
            console.log(`ae_loc = `, get(ae_loc));

            let slct_tmp = get(slct);
            slct_tmp.event_id = event_obj_results.event_id_random;
            slct_tmp.event_obj = event_obj_results;
            slct.set(slct_tmp);
            console.log(`slct = `, get(slct));

            let slct_trigger_tmp = get(slct_trigger);
            slct_trigger_tmp = 'load__event_presenter_obj_li';
            slct_trigger.set(slct_trigger_tmp);
            console.log(`slct_trigger = `, get(slct_trigger));
        }

        return event_obj_results;
    })


    let ds_code_li = {}; //: key_val; // = ae_loc_tmp.ds;
    // console.log(`ae_ ds_code_li = `, ds_code_li);
    let ds_code: string = '';
    let ds_type: null|string = null;

    ds_code = 'hub__page__events_speakers__create_info_header';
    ds_type = 'text';

    ds_code_li[ds_code] = await api.get_data_store_obj_w_code({
        api_cfg: get(ae_api),
        data_store_code: ds_code,
        data_type: ds_type,
        log_lvl: 2
        })
    .then( function (ds_results) {
        console.log(`ae_ Data Store ${ds_code} = `, ds_results);
        if (ds_results) {
            console.log(`ae_ Data Store ${ds_code} = `, ds_results);
            if (ds_type == 'json') {
                return ds_results.json;
            } else {
                return ds_results.text;
            }
        }
    })
    .finally(function (ds_code_val) {
        console.log(`ae_ ds_code_val = `, ds_code_val);

        return ds_code_val;
    });

    ds_code = 'hub__page__events_speakers__create_info';
    ds_type = 'text';

    ds_code_li[ds_code] = await api.get_data_store_obj_w_code({
        api_cfg: get(ae_api),
        data_store_code: ds_code,
        data_type: ds_type,
        log_lvl: 2
        })
    .then( function (ds_results) {
        console.log(`ae_ Data Store ${ds_code} = `, ds_results);
        if (ds_results) {
            console.log(`ae_ Data Store ${ds_code} = `, ds_results);
            if (ds_type == 'json') {
                return ds_results.json;
            } else {
                return ds_results.text;
            }
        }
    })
    .finally(function (ds_code_val) {
        console.log(`ae_ ds_code_val = `, ds_code_val);

        return ds_code_val;
    });


    let ae_loc_tmp = get(ae_loc);
    console.log(`ae_loc = `, ae_loc_tmp);

    let ae_ds_tmp = ae_loc_tmp.ds;
    // Merge the new data into the existing data
    ae_ds_tmp = {
        ...ae_ds_tmp,
        ...ds_code_li,
    }

    ae_loc_tmp = {
        ...ae_loc_tmp,
        'ds': ae_ds_tmp,
    }
    console.log(`ae_loc = `, ae_loc_tmp);
    ae_loc.set(ae_loc_tmp);

    // return data_struct;
    return data;


    // let data_struct = {
    //     params: params,
    //     sections: [
	// 		{ slug: 'new', title: 'New Speaker' },
	// 		{ slug: 'manage', title: 'Manage' },
    //         { slug: 'test', title: 'Test' },
	// 	],
    //     url: url,

    // };
    // return data_struct;
}


function handle_load_ae_obj_id__event({event_id, try_cache=false}) {
    console.log(`*** handle_load_ae_obj_id__event() *** event_id=${event_id}`);

    let params = {};

	// ae_loc.hub.event_id_qry_status = 'loading';
    let ae_event_obj_get_promise = api.get_ae_obj_id_crud({
        api_cfg: get(ae_api),
        obj_type: 'event',
        obj_id: event_id, // NOTE: This is the FQDN, not normally the ID.
        use_alt_table: false, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
        use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value in the API config.
        params: params,
        log_lvl: 0
        })
    .then(function (event_obj_get_result) {
        if (event_obj_get_result) {
            return event_obj_get_result;
        } else {
            console.log('No results returned.');
            return null;
        }
    })
    .catch(function (error) {
        console.log('No results returned or failed.', error);
    });

    return ae_event_obj_get_promise;
}
