<script lang="ts">
interface Props {
    // Exports
    container_class_li?: string | Array<string>;
    event_file_obj_li?: Array<any>;
    link_to_type?: string;
    link_to_id?: string;
    allow_basic?: boolean;
    allow_moderator?: boolean;
    show_direct_download?: boolean;
    show_location_fields?: boolean;
    show_presentation_fields?: boolean;
    show_session_fields?: boolean;
    hide_session_code?: boolean;
    log_lvl?: number;
}

let {
    container_class_li = [],
    // display_mode = 'default',
    // event_file_id_li = $bindable(),
    event_file_obj_li = $bindable(),
    link_to_type,
    link_to_id,
    allow_basic = true,
    allow_moderator = true,
    show_direct_download = $bindable(false),
    show_location_fields = false,
    show_presentation_fields = true,
    show_session_fields = true,
    hide_session_code = false,
    log_lvl = $bindable(0)
}: Props = $props();

// Imports
import Comp_event_file_obj_tbl from './ae_comp__event_file_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_file_id_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_file_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 file table.
let lq__event_file_obj_li = $derived(
    liveQuery(async () => {
        let results: any;

        // If data is already provided (from API reports with joins), use it directly
        if (event_file_obj_li?.length) {
            if (log_lvl) {
                console.log(
                    `LQ - Using provided event_file_obj_li (${event_file_obj_li.length} files with API joins)`
                );
            }
            return event_file_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}`
                );
            }
            results = await db_events.file
                .where(dq__where_type_id_val)
                .equals(dq__where_eq_id_val)
                .sortBy('name');
        } else {
            results = [];
        }

        // Check if results are different than the current session version stored under $events_slct
        if (
            $events_slct.event_file_obj_li &&
            JSON.stringify($events_slct.event_file_obj_li) !==
                JSON.stringify(results)
        ) {
            $events_slct.event_file_obj_li = [...results];
            if (log_lvl) {
                console.log(
                    `Session slct li stored version has changed for ID = ${$events_slct.journal_id}`,
                    $events_slct.event_file_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 event_file_obj_li && event_file_obj_li?.length}
    <!-- {#if event_file_id_li && event_file_id_li?.length} -->
    <Comp_event_file_obj_tbl
        {container_class_li}
        {lq__event_file_obj_li}
        {allow_basic}
        {allow_moderator}
        bind:show_direct_download
        {show_presentation_fields}
        {show_session_fields}
        {hide_session_code}
        {log_lvl}></Comp_event_file_obj_tbl>
{:else}
    <section class="flex grow flex-col items-center gap-1 px-1 pb-28 md:px-2">
        <p>No files available to show in table.</p>
    </section>
{/if}
