<script lang="ts">
// NEW NEW NEW: 2025-01-07
// import { onMount } from 'svelte';
// import { clipboard } from '@skeletonlabs/skeleton';
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_hosted_file_li from '$lib/element_manage_hosted_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_core } from "$lib/ae_core/db_core";
// import { events_loc, events_sess, events_slct, events_trigger } from '$lib/ae_events_stores';
// import { events_func } from '$lib/ae_events_functions';

export let container_class_li: string|Array<string> = [];
export let link_to_type: string;
export let link_to_id: string;
export let allow_basic: boolean = false;
export let allow_moderator: boolean = false;
export let display_mode: string = 'default'; // 'default', 'compact', 'minimal', 'launcher'

console.log(`HERE HERE HERE HERE: link_to_type: ${link_to_type} link_to_id: ${link_to_id}`);

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

let dq__where_val: string = `${link_to_type}_id`; // no more _random ???
let dq__where_eq_val: string = link_to_id ?? 'Q8lR8Ai8hx2FjbQ3C_EH1Q';

// 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.
$: lq__hosted_file_obj_li = liveQuery(async () => {
    console.log(`dq__where_val: ${dq__where_val}`);
    console.log(`dq__where_eq_val: ${dq__where_eq_val}`);
    let results = await db_core.file
        // .where(dq__where_val)
        .where('account_id')
        // .equals(dq__where_eq_val)
        .equals('Q8lR8Ai8hx2FjbQ3C_EH1Q')
        // .reverse()
        // .sortBy('created_on')
        // .toArray()
        ;
    return results;
});
</script>



<!-- {#if lq__hosted_file_obj_li}
    <h3 class="h3">{lq__hosted_file_obj_li.length}&times; files clipped</h3>
    <div class="container">
    <ol>
    {#each lq__hosted_file_obj_li as hosted_file_obj}
        <li>
        {hosted_file_obj.filename}
        </li>
    {/each}
    </ol>
    </div>
{:else}
    <p>No files found</p>
{/if} -->

{#if lq__hosted_file_obj_li}
    <Element_manage_hosted_file_li
        link_to_type={link_to_type}
        link_to_id={link_to_id}
        lq__hosted_file_obj_li={lq__hosted_file_obj_li}
        allow_basic={allow_basic}
        allow_moderator={allow_moderator}
        container_class_li={container_class_li}
        display_mode={display_mode}
        />
{:else}
    <p>No files found</p>
{/if}
