<script lang="ts">
interface Props {
    data?: any;
    log_lvl?: number;
    container_class_li?: string | Array<string>;
    // export let display_mode: string = 'default'; // 'default', 'compact', 'minimal', 'launcher'
    event_device_id_random_li?: Array<string>;
    link_to_type: string;
    link_to_id: string;
    auto_refresh?: number;
}

let {
    data = null,
    log_lvl = 1,
    container_class_li = [],
    event_device_id_random_li = [],
    link_to_type,
    link_to_id,
    auto_refresh = 90000
}: Props = $props();

// Imports
import Comp_event_device_obj_li from '././ae_comp__event_device_obj_li.svelte';

import { liveQuery } from 'dexie';
import { db_events } from '$lib/ae_events/db_events';

$effect(() => {
    if (log_lvl) {
        console.log(
            `Event Device List Wrapper: 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 dq__where_type_id_val = $derived(`${link_to_type}_id`);
let dq__where_eq_id_val = $derived(link_to_id);

// *** Functions and Logic
let lq__event_device_obj_li = $derived(
    liveQuery(async () => {
        if (link_to_type && link_to_id) {
            let results = await db_events.device
                .where(dq__where_type_id_val)
                .equals(dq__where_eq_id_val)
                .sortBy('name');

            return results;
        } else if (event_device_id_random_li.length > 0) {
            let results = await db_events.device.bulkGet(
                event_device_id_random_li
            );

            return results;
        } else {
            return null;
        }
    })
);
</script>

<Comp_event_device_obj_li
    {container_class_li}
    {link_to_type}
    {link_to_id}
    {lq__event_device_obj_li}
    {auto_refresh}
    {log_lvl} />
