<script lang="ts">
	interface Props {
		// Exports
		container_class_li?: string | Array<string>;
		// export let display_mode: string = 'default'; // 'default', 'compact', 'minimal', 'launcher'
		event_id_random_li?: Array<string>;
		link_to_type: string;
		link_to_id: string;
		order_by?: string;
		hide?: string;
		enable?: string;
		limit?: number;
		offset?: number;
		log_lvl?: number;
	}

	let {
		container_class_li = '',
		event_id_random_li = [],
		link_to_type = 'account',
		link_to_id,
		order_by = 'updated_on',
		hide = 'not_hidden', // all, hidden, not_hidden
		enable = 'enabled', // all, disabled, enabled
		limit = 0,
		offset = 0,
		log_lvl = 0
	}: Props = $props();

	// *** Import Svelte specific

	// *** Import other supporting libraries
	import { liveQuery } from 'dexie';
	import { db_events } from '$lib/ae_events/db_events';

	// *** Import Aether specific variables and functions
	import Comp__event_obj_li from './ae_idaa_comp__event_obj_li.svelte';

	if (log_lvl) {
		console.log(`TEST event: link_to_type: ${link_to_type}; link_to_id: ${link_to_id}`);
	}
	if (log_lvl > 1) {
		console.log(`TEST event: event_id_random_li: ${event_id_random_li}`);
	}

	// Variables
	// let ae_promises: key_val = {};
	// let ae_tmp: key_val = {};
	// let ae_triggers: key_val = {};

	let dq__where_type_id_val: string = `${link_to_type}_id`; // no more "xyz_id_random"
	let dq__where_eq_id_val: string = link_to_id;

	// *** Functions and Logic
	let lq__event_obj_li = $derived(
		liveQuery(async () => {
			// Check if event_id_random_li is an array and has items
			// if (Array.isArray(event_id_random_li)) { // && event_id_random_li.length > 0) {
			if (event_id_random_li && event_id_random_li.length) {
				if (log_lvl) {
					(console.log(`Trying IDB bulkGet: ${event_id_random_li.length}`), event_id_random_li);
				}
				// if (!event_id_random_li.length) {
				//     return [];
				// }
				let results = await db_events.event.bulkGet(event_id_random_li);

				return results;
			} else if (link_to_type && link_to_id) {
				if (log_lvl) {
					console.log(
						`Trying where: ${link_to_type}; equals: ${link_to_id}; event_id_random_li: ${event_id_random_li}`,
						event_id_random_li
					);
				}
				let results: any = null;
				if (order_by == 'name') {
					results = await db_events.event
						.where(dq__where_type_id_val)
						.equals(dq__where_eq_id_val)
						.and((event) => {
							return event.hide == false;
						})
						.and((event) => {
							return event.enable == true;
						})
						.sortBy('name');
					// This should be sorted by a custom sort field
				} else {
					results = await db_events.event
						.where(dq__where_type_id_val)
						.equals(dq__where_eq_id_val)
						.and((event) => {
							return event.hide == false;
						})
						.and((event) => {
							return event.enable == true;
						})
						.sortBy('updated_on');
				}

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

{#if $lq__event_obj_li}
	<Comp__event_obj_li {container_class_li} {link_to_type} {link_to_id} {lq__event_obj_li} {log_lvl}
	></Comp__event_obj_li>
{:else}
	<div class="space-y-2">
		<div class="ae_highlight ae_padding_md ae_row ae_flex_justify_center">
			<span class="fas fa-spinner fa-spin m-1"></span>
			Loading...
		</div>

		<div class="ae_highlight ae_padding_md ae_row ae_flex_justify_center">
			No recovery meetings available to show. The search may need to be changed.
		</div>
	</div>
{/if}
