<script lang="ts">
interface Props {
    // Exports
    container_class_li?: string | Array<string>;
    // display_mode?: string; // 'default', 'compact', 'minimal', 'launcher'
    // event_presenter_id_random_li?: Array<string>;
    event_presenter_obj_li?: Array<any>;
    link_to_type?: string;
    link_to_id?: string;
    // export let lq__event_presentation_obj
    show_presentation_fields?: boolean;
    show_session_fields?: boolean;
    hide_session_code?: boolean;
    log_lvl?: number;
}

let {
    container_class_li = [],
    // display_mode = 'default',
    // event_presenter_id_random_li = $bindable(),
    event_presenter_obj_li = $bindable(),
    link_to_type,
    link_to_id,
    show_presentation_fields = false,
    show_session_fields = false,
    hide_session_code = false,
    log_lvl = $bindable(0)
}: Props = $props();

// Imports
import Comp_event_presenter_obj_tbl from './ae_comp__event_presenter_obj_tbl.svelte';

import { liveQuery } from 'dexie';
import {
    events_sess,
    events_slct,
    events_trigger
} from '$lib/stores/ae_events_stores';
import { db_events } from '$lib/ae_events/db_events';

$effect(() => {
    if (log_lvl) {
        console.log(`link_to_type: ${link_to_type}; link_to_id: ${link_to_id}`);
    }
});

// Variables
// let ae_promises: key_val = {};
// let ae_tmp: key_val = {};
// let ae_triggers: key_val = {};

let event_presenter_id_random_li: Array<string> = $state([]);

let dq__where_type_id_val = $derived(`${link_to_type}_id`);
let dq__where_eq_id_val = $derived(link_to_id ?? '');

// *** Functions and Logic

// OPTIMIZATION: If event_presenter_obj_li is provided (from API/reports),
// use it directly instead of fetching from IDB. The API returns fully
// enriched data with all joined fields (session names, presentation names, etc.)
// that aren't available in the base IDB presenter table.
let lq__event_presenter_obj_li = $derived(
    liveQuery(async () => {
        let results: any;

        // If data is already provided (from API reports with joins), use it directly
        if (event_presenter_obj_li?.length) {
            if (log_lvl) {
                console.log(
                    `LQ - Using provided event_presenter_obj_li (${event_presenter_obj_li.length} presenters with API joins)`
                );
            }
            return event_presenter_obj_li;
        }

        // Otherwise fetch from IDB for link_to_type/link_to_id lookups
        if (link_to_type && link_to_id) {
            if (log_lvl) {
                console.log(
                    `LQ - Fetching from IDB where: ${dq__where_type_id_val} = ${dq__where_eq_id_val}`
                );
            }
            event_presenter_id_random_li = [];
            results = await db_events.presenter
                .where(dq__where_type_id_val)
                .equals(dq__where_eq_id_val)
                .sortBy('name');
        } else {
            event_presenter_id_random_li = [];
            results = [];
        }

        // Check if results are different than the current session version stored under $events_slct
        if (
            $events_slct.event_presenter_obj_li &&
            JSON.stringify($events_slct.event_presenter_obj_li) !==
                JSON.stringify(results)
        ) {
            $events_slct.event_presenter_obj_li = [...results];
            if (log_lvl) {
                console.log(
                    `Session slct li stored version has changed for ID = ${$events_slct.journal_id}`,
                    $events_slct.event_presenter_obj_li
                );
            }
        } else {
            if (log_lvl > 1) {
                console.log(
                    `Session slct li stored version has not changed for ID = ${$events_slct.journal_id}`
                );
            }
        }

        return results;
    })
);
</script>

<!-- {#if $lq__event_presenter_obj_li && $lq__event_presenter_obj_li?.length} -->
<!-- {#if event_presenter_id_random_li && event_presenter_id_random_li?.length > 0} -->
{#if event_presenter_obj_li && event_presenter_obj_li?.length}
    <!-- {#if event_presenter_id_random_li && event_presenter_id_random_li?.length} -->
    <Comp_event_presenter_obj_tbl
        {container_class_li}
        {lq__event_presenter_obj_li}
        {show_presentation_fields}
        {show_session_fields}
        {hide_session_code}
        {log_lvl}></Comp_event_presenter_obj_tbl>
{:else}
    <section class="flex grow flex-col items-center gap-1 px-1 pb-28 md:px-2">
        <p>No presenters available to show in table.</p>
    </section>
{/if}
