<script lang="ts">
    let log_lvl: number = 2;

    // *** Import Svelte specific
    import { browser } from '$app/environment';

    // *** Import other supporting libraries

    // *** Import Aether specific variables and functions
    import {
        ae_snip,
        ae_loc,
        ae_sess,
        ae_api,
        ae_trig,
        slct,
        slct_trigger
    } from '$lib/stores/ae_stores';
    import { idaa_loc, idaa_sess, idaa_slct } from '$lib/stores/ae_idaa_stores';

    // import Help_tech from '$lib/e_app_help_tech.svelte';

    interface Props {
        /** @type {import('./$types').LayoutData} */
        data: any;
        children?: import('svelte').Snippet;
    }

    let { data, children }: Props = $props();

    $effect(() => {
        $ae_loc.url_origin = data.url.origin;
        $ae_loc.params = data.params;

        if (log_lvl > 1) {
            console.log(`+layout.svelte data:`, data);
        }
    });

    $effect(() => {
        if (browser) {
            if (data.url.searchParams.get('uuid')) {
                $idaa_loc.novi_uuid = data.url.searchParams.get('uuid'); // data.params.uuid;
            }
            if (data.url.searchParams.get('email')) {
                $idaa_loc.novi_email = decodeURIComponent(data.url.searchParams.get('email'));
            } else {
                $idaa_loc.novi_email = null;
            }
            if (data.url.searchParams.get('full_name')) {
                $idaa_loc.novi_full_name = decodeURIComponent(data.url.searchParams.get('full_name'));
            } else {
                $idaa_loc.novi_full_name = null;
            }
            $idaa_loc.novi_admin_li = $ae_loc.site_cfg_json?.novi_admin_li ?? [];
            $idaa_loc.novi_trusted_li = $ae_loc.site_cfg_json?.novi_trusted_li ?? [];
            // console.log(`$idaa_loc.novi_uuid:`, $idaa_loc.novi_uuid);
            // console.log(`$idaa_loc.novi_admin_li:`, $idaa_loc.novi_admin_li);

            // Reminder: super > manager > administrator > trusted > public > authenticated > anonymous

            // NOTE: This is checking if they are in an iframe *and* have a Novi UUID. We ignore the iframe mode for trusted and above (administrators, managers, etc).
            if (
                $ae_loc?.iframe &&
                $idaa_loc?.novi_uuid?.length == 36 &&
                $idaa_loc?.novi_email?.length > 3 &&
                $idaa_loc?.novi_full_name?.length > 0
            ) {
                $ae_loc.access_type = 'authenticated';
                $ae_loc.super_access = false;
                $ae_loc.manager_access = false;
                $ae_loc.administrator_access = false;
                $ae_loc.trusted_access = false;
                $ae_loc.public_access = false;
                $ae_loc.authenticated_access = true;
                $ae_loc.anonymous_access = true;

                // Resetting these just in case...
                $idaa_loc.bb.qry__hidden == 'not_hidden';
                $idaa_loc.bb.qry__enabled == 'enabled';
            // NOTE: This is sort of temporary while we work on getting Jisti working with IDAA's Novi site.
            } else if (
                $ae_loc?.iframe &&
                $idaa_loc?.novi_uuid?.length == 36
            ) {
                $ae_loc.access_type = 'authenticated';
                $ae_loc.super_access = false;
                $ae_loc.manager_access = false;
                $ae_loc.administrator_access = false;
                $ae_loc.trusted_access = false;
                $ae_loc.public_access = false;
                $ae_loc.authenticated_access = true;
                $ae_loc.anonymous_access = true;

                // Resetting these just in case...
                $idaa_loc.bb.qry__hidden == 'not_hidden';
                $idaa_loc.bb.qry__enabled == 'enabled';
            } else if ($ae_loc?.iframe) {
                $ae_loc.access_type = 'anonymous';
                $ae_loc.super_access = false;
                $ae_loc.manager_access = false;
                $ae_loc.administrator_access = false;
                $ae_loc.trusted_access = false;
                $ae_loc.public_access = false;
                $ae_loc.authenticated_access = false;
                $ae_loc.anonymous_access = true;

                // Resetting these just in case...
                $idaa_loc.bb.qry__hidden == 'not_hidden';
                $idaa_loc.bb.qry__enabled == 'enabled';
            }

            if ($idaa_loc.novi_uuid) {
                let flag = false;
                // NOTE: Check if the novi_uuid is in the novi_admin_li list
                if ($idaa_loc.novi_admin_li) {
                    if ($idaa_loc.novi_admin_li.includes($idaa_loc.novi_uuid)) {
                        $ae_loc.access_type = 'administrator';
                        $ae_loc.super_access = false;
                        $ae_loc.manager_access = false;
                        $ae_loc.administrator_access = true;
                        $ae_loc.trusted_access = true;
                        $ae_loc.public_access = true;
                        $ae_loc.authenticated_access = true;
                        $ae_loc.anonymous_access = true;

                        flag = true;
                    }
                }

                // NOTE: Check if the novi_uuid is in the novi_trusted_li list
                if ($idaa_loc.novi_trusted_li) {
                    if ($idaa_loc.novi_trusted_li.includes($idaa_loc.novi_uuid)) {
                        $ae_loc.access_type = 'trusted';
                        $ae_loc.super_access = false;
                        $ae_loc.manager_access = false;
                        $ae_loc.administrator_access = false;
                        $ae_loc.trusted_access = true;
                        $ae_loc.public_access = true;
                        $ae_loc.authenticated_access = true;
                        $ae_loc.anonymous_access = true;

                        flag = true;
                    }
                }
            }
        }
    });

    // let iframe = data.url.searchParams.get('iframe');
    // if (browser && iframe == 'true') {
    //     console.log('Use iframe layout!');
    //     // data_struct['iframe'] = iframe;
    //     $ae_loc.iframe = true;

    //     document.getElementsByTagName('html')[0].classList.add('iframe');
    //     // document.getElementsByTagName('html')[0].classList.remove('dark');
    //     // document.getElementsByTagName('html')[0].classList.remove('light');

    //     // $ae_loc.app_cfg.show_element__access_type = false;
    //     // $ae_loc.app_cfg.show_element__cfg = false;
    // } else if (browser && iframe == 'false') {
    //     // data_struct['iframe'] = false;
    //     $ae_loc.iframe = false;

    //     document.getElementsByTagName('html')[0].classList.remove('iframe');
    //     // document.getElementsByTagName('html')[0].classList.add('light');
    // }

    // $effect(() => {
    //         if ($ae_loc.iframe && $ae_loc.iframe_height && $ae_loc.iframe_height_modal_body) {
    //         if (log_lvl > 1) {
    //             console.log('Getting new dimensions for iframe with modal:', $ae_loc.iframe_height, $ae_loc.iframe_height_modal_body);
    //         }

    //         let iframe_height = 0;

    //         if ($ae_loc.iframe_height > $ae_loc.iframe_height_modal_body) {
    //             iframe_height = $ae_loc.iframe_height;
    //         } else {
    //             iframe_height = $ae_loc.iframe_height_modal_body;

    //             // console.log($ae_loc.modal_dimensions);

    //             if ($ae_loc.modal_dimensions && $ae_loc.modal_dimensions.header_height) {
    //                 iframe_height = iframe_height + $ae_loc.modal_dimensions.header_height;
    //             }
    //             if ($ae_loc.modal_dimensions && $ae_loc.modal_dimensions.footer_height) {
    //                 iframe_height = iframe_height + $ae_loc.modal_dimensions.footer_height;
    //             }
    //             // iframe_height = iframe_height + 50; // Just in case
    //         }

    //         if (log_lvl > 1) {
    //             console.log(`Suggested new iframe_height with modal: ${iframe_height}`);
    //         }
    //         window.parent.postMessage({'iframe_height': iframe_height}, "*"); // This should be in pixels
    //     } else if ($ae_loc.iframe && $ae_loc.iframe_height) {
    //         if (log_lvl > 1) {
    //             console.log('Suggested new iframe_height:', $ae_loc.iframe_height);
    //         }
    //         window.parent.postMessage({'iframe_height': $ae_loc.iframe_height}, "*"); // This should be in pixels
    //     }
    // });
</script>

{#if browser && ($ae_loc.trusted_access || ($ae_loc.authenticated_access && $idaa_loc.novi_uuid))}
    <!-- e_class="w-xl" -->
    <!-- e_class="float-right" -->
    <!-- <Help_tech
        e_class="w-full"
        show_btn_class="absolute top-0 right-0"
        btn_class="novi_btn"
        additional_kv={{
            'novi_uuid': $idaa_loc.novi_uuid,
            'novi_email': $idaa_loc.novi_email,
            'novi_full_name': $idaa_loc.novi_full_name,
        }}
        >
    </Help_tech> -->
    <!-- <div
        bind:clientHeight={$ae_loc.iframe_height}
        > -->
    {@render children?.()}
    <!-- </div> -->
    {#if $idaa_loc.novi_uuid}
        <span class="text-sm text-gray-500">
            Novi: <span class="fas fa-user m-1"></span>
            {$idaa_loc.novi_uuid}
            {$idaa_loc.novi_full_name ?? 'name not set'}
            {$idaa_loc.novi_email ?? 'email not set'}
        </span>
    {:else}
        <p class="text-sm text-gray-500 text-center">IDAA Novi UUID not found!</p>
    {/if}
{:else if browser}
    <div class="container flex flex-col gap-1 w-full items-center justify-center font-bold p-8 m-8">
        <h1>
            <span class="text-red-500">
                <span class="fas fa-exclamation-triangle"></span>
                Access Denied
                <span class="fas fa-exclamation-triangle"></span>
            </span>
        </h1>
        <p>You do not have access to these IDAA page.</p>

        {#if $ae_loc.iframe}
            In iframe mode
        {/if}

        {#if $idaa_loc.novi_uuid}
            <span class="text-sm text-gray-500">
                Novi: <span class="fas fa-user m-1"></span>
                {$idaa_loc.novi_uuid}
                {$idaa_loc.novi_full_name ?? 'name not set'}
                {$idaa_loc.novi_email ?? 'email not set'}
            </span>
        {:else}
            <p>IDAA Novi UUID not found!</p>
        {/if}
    </div>
{:else}
    <p class="text-sm text-gray-500 text-center">
        <span class="fas fa-spinner fa-spin"></span>
        Loading...
    </p>
{/if}
