/** @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 type { key_val } from '$lib/ae_stores';


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

    // const { ae_init, root_layout_ts } = await parent();
    let data = await parent();
    // console.log(`ae_events_badges +layout.ts data:`, data);

    data.ae_sponsorships_layout_ts = true;



    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;

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

    // };

    let loading_results = null;

    // ds_code = 'sponsorships__agree_li';
    // ds_type = 'json';

    ds_code = 'hub__page__sponsorships__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);
            // let ae_loc_tmp = get(ae_loc);
            if (ds_type == 'json') {
                // ds_code_li[ds_code] = ds_results.json;
                // data.ae_ds[ds_code] = ds_results.json;
                return ds_results.json;
            } else {
                // ds_code_li[ds_code] = ds_results.text;
                // data.ae_ds[ds_code] = ds_results.text;
                return ds_results.text;
            }

            // ae_loc.set(ae_loc_tmp);
            // console.log(`ae_loc = `, get(ae_loc));
        }
    })
    .finally(function (ds_code_val) {
        console.log(`ae_ ds_code_val = `, ds_code_val);
        // ae_loc_tmp = {
        //     ...ae_loc_tmp,
        //     'ds': ds_code_li,
        // }
        // console.log(`ae_loc_tmp = `, ae_loc_tmp);

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

        // return true;
        return ds_code_val;
    });

    ds_code = 'hub__page__sponsorships__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);
            // let ae_loc_tmp = get(ae_loc);
            if (ds_type == 'json') {
                // ds_code_li[ds_code] = ds_results.json;
                // data.ae_ds[ds_code] = ds_results.json;
                return ds_results.json;
            } else {
                // ds_code_li[ds_code] = ds_results.text;
                // data.ae_ds[ds_code] = ds_results.text;
                return ds_results.text;
            }

            // ae_loc.set(ae_loc_tmp);
            // console.log(`ae_loc = `, get(ae_loc));
        }
    })
    .finally(function (ds_code_val) {
        console.log(`ae_ ds_code_val = `, ds_code_val);
        // ae_loc_tmp = {
        //     ...ae_loc_tmp,
        //     'ds': ds_code_li,
        // }
        // console.log(`ae_loc_tmp = `, ae_loc_tmp);

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

        // return true;
        return ds_code_val;
    });
    console.log(`ae_ ds_code_li = `, ds_code_li);

    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;
}
