<script lang="ts">
interface Props {
    log_lvl?: number;
    // import { events_func } from '$lib/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 {
    log_lvl = 0,
    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/ae_stores';
// import { ae_util } from '$lib/ae_utils/ae_utils';
// import { api } from '$lib/api';
// import Element_ae_crud from '$lib/element_ae_crud.svelte';
// import Element_data_store from '$lib/element_data_store_v2.svelte';
import Element_manage_event_file_li from '$lib/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/ae_events_stores';

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

// 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 = {};

let dq__where_val: string = `for_type`;
let dq__where_eq_val: string = link_to_type;
let dq__where_for_id_eq_val: string = link_to_id;

// This should only include files that are directly linked to 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 () => {
    let results = await db_events.files
        .where(dq__where_val)
        .equals(dq__where_eq_val)
        .and(file => file.for_id_random == dq__where_for_id_eq_val)
        .reverse()
        .sortBy('created_on')
        // .toArray()
        ;
    return results;
}));
</script>


{#await lq__event_file_obj_li}

<p>Loading...</p>

{:then lq__event_file_obj_li}
<Element_manage_event_file_li
    link_to_type={link_to_type}
    link_to_id={link_to_id}
    lq__event_file_obj_li={lq__event_file_obj_li}
    allow_basic={allow_basic}
    allow_moderator={allow_moderator}
    container_class_li={container_class_li}
    display_mode={display_mode}
    log_lvl={log_lvl}
    />
{:catch error}
<p style="color: red;">{error.message}</p>
{/await}
