<script lang="ts">
interface Props {
    // import { events_func } from '$lib/ae_events/ae_events_functions';
    container_class_li?: string | Array<string>;
    link_to_type: string;
    link_to_id: string;
    allow_basic?: boolean;
    allow_moderator?: boolean;
    display_mode?: string; // 'default', 'compact', 'minimal', 'launcher'
}

let {
    container_class_li = [],
    link_to_type,
    link_to_id,
    allow_basic = false,
    allow_moderator = false,
    display_mode = 'default'
}: Props = $props();

import { liveQuery } from 'dexie';

import type { key_val } from '$lib/stores/ae_stores';
// import { ae_util } from '$lib/ae_utils/ae_utils';
import Element_manage_event_file_li from '$lib/elements/element_manage_event_file_li.svelte';

// import { core_func } from '$lib/ae_core_functions';
// import { ae_loc, ae_sess, ae_api, ae_trig, slct, slct_trigger } from '$lib/ae_stores';
import { db_events } from '$lib/ae_events/db_events';
// import { events_loc, events_sess, events_slct, events_trigger } from '$lib/stores/ae_events_stores';

// export let show_convert_btn: null|boolean = null;

// let ae_placeholder_li: key_val = {};
// let ae_promises: key_val = {};
let ae_tmp: key_val = {};
ae_tmp.show__file_li = true;
ae_tmp.show__direct_download = false;
// let ae_triggers: key_val = {};

// WHY: v_event_file joins event_session only via event_file.event_session_id.
// For files linked to a presentation or presenter, event_session_id is NULL on
// the file record itself, so event_session_type_code is always NULL from the API.
// Derive the session type from Dexie: presentation → event_session_id → type_code,
// or presenter → event_presentation_id → event_session_id → type_code.
let lq__context_session_type_code = $derived(
    liveQuery(async () => {
        if (link_to_type === 'event_presentation' && link_to_id) {
            const presentation = await db_events.presentation.get(link_to_id);
            if (!presentation?.event_session_id) return null;
            const session = await db_events.session.get(
                presentation.event_session_id
            );
            return session?.type_code ?? null;
        }
        if (link_to_type === 'event_presenter' && link_to_id) {
            const presenter = await db_events.presenter.get(link_to_id);
            if (!presenter?.event_presentation_id) return null;
            const presentation = await db_events.presentation.get(
                presenter.event_presentation_id
            );
            if (!presentation?.event_session_id) return null;
            const session = await db_events.session.get(
                presentation.event_session_id
            );
            return session?.type_code ?? null;
        }
        return null;
    })
);

let dq__where_val = $derived(`${link_to_type}_id`);
let dq__where_eq_val = $derived(link_to_id);

// This should include all files that are associated with an object (event, location, session, presenter, etc.)
// I am not sure why, but doing reverse() and then sortBy() seems to sort in descending order.
let lq__event_file_obj_li = $derived(
    liveQuery(async () => {
        if (!dq__where_eq_val) return [];
        let results = await db_events.file
            .where(dq__where_val)
            .equals(dq__where_eq_val)
            .or('for_id')
            .equals(dq__where_eq_val)
            .filter((file) => {
                // If using for_id, we should also verify for_type to avoid accidental cross-links
                if (file.for_id === dq__where_eq_val) {
                    return file.for_type === link_to_type;
                }
                return true;
            })
            .reverse()
            .sortBy('created_on');
        // .toArray()
        return results;
    })
);
</script>

<Element_manage_event_file_li
    {link_to_type}
    {link_to_id}
    {lq__event_file_obj_li}
    {allow_basic}
    {allow_moderator}
    {container_class_li}
    {display_mode}
    context_session_type_code={$lq__context_session_type_code ?? null} />
