<svelte:options accessors={true} />

<script lang="ts">
	import type { Gradio } from "@gradio/utils";
	import { BlockTitle } from "@gradio/atoms";
	import { Block } from "@gradio/atoms";
	import { StatusTracker } from "@gradio/statustracker";
	import type { LoadingStatus } from "@gradio/statustracker";
	import { tick } from "svelte";
	import type { Entry } from "./QuickSearchEntry/Types";
	import QuickSearchEntry from "./QuickSearchEntry/QuickSearchEntry.svelte";
	import type { QuickSearchResults } from "./Types";
	import { queryString, httpGet, throttle } from "./lib/ViewUtils";

	export let gradio: Gradio<{
		change: never;
		submit: never;
		input: never;
	}>;
	export let label = "Textbox";
	export let elem_id = "";
	export let elem_classes: string[] = [];
	export let visible = true;
	export let value = "";
	export let placeholder = "";
	export let show_label: boolean;
	export let scale: number | null = null;
	export let min_width: number | undefined = undefined;
	export let loading_status: LoadingStatus | undefined = undefined;
	export let value_is_output = false;
	export let interactive: boolean;
	export let rtl = false;

	export let url: string = "https://huggingface.co/api/quicksearch";
	export let position: "absolute" | "fixed" = "absolute";

	let entries: Entry[] = [];
	let isOpen = false;
	let lastQuery: string | null = null;
	let numResults = 0;
	let selectedEntryIdx = -1;
	let searchController = new AbortController();

	const THROTTLE_DELAY = 300;

	let el: HTMLTextAreaElement | HTMLInputElement;
	const container = true;

	function handle_change(): void {
		gradio.dispatch("change");
		if (!value_is_output) {
			gradio.dispatch("input");
		}
	}

	async function handle_keypress(e: KeyboardEvent): Promise<void> {
		await tick();
		if (e.key === "Enter") {
			e.preventDefault();
			gradio.dispatch("submit");
		}
	}
	
	async function handleFocus() {
		if (lastQuery === null) {
			await performSearch();
		}
		isOpen = true;
	}
	
	const handleInput = throttle(async () => {
		isOpen = true;
		await tick(); // Let parent component change searchParams such as orgFilters first
		await performSearch();
	}, THROTTLE_DELAY);

	function isSelected(selectedEntryIdx: number, entry: Entry) {
		return entries[selectedEntryIdx]?.id === entry.id && entries[selectedEntryIdx]?.type === entry.type;
	}

	function handleClickEntry(entry: Entry) {
		selectedEntryIdx = entries.findIndex(e => e.id === entry.id && e.type === entry.type);
		console.log(entry);
	}

	$: if (value === null) value = "";

	// When the value changes, dispatch the change event via handle_change()
	// See the docs for an explanation: https://svelte.dev/docs/svelte-components#script-3-$-marks-a-statement-as-reactive
	$: value, handle_change();
	
	async function performSearch() {
		const input = value.trim();
		if (input !== lastQuery) {
			const res = await fetchResults(input);
			if (res.isError) {
				if (!res.aborted) {
					console.error(`QuickSearch Error: ${res.error}`);
				}
				return;
			}

			const resEntries = getResultEntries(res.payload as QuickSearchResults);
			entries = [
				...resEntries,
			];
			console.log(entries);
			numResults = resEntries.length;
			lastQuery = input;
		}
	}
	
	async function fetchResults(input: string = "") {
		searchController.abort(); /// Cancel previous function call if exists
		searchController = new AbortController();

		const res = await httpGet<QuickSearchResults>(
			url +
				queryString({
					q: input,
					type: ["model", "org", "user"],
				}),
			{ signal: searchController.signal }
		);

		return res;
	}

	function getResultEntries(results: QuickSearchResults | null): Entry[] {
		const modelEntries: Entry[] = results.models.map(m => ({
			href: undefined,
			id: m.id,
			_id: m._id,
			label: m.id,
			type: "model",
		}));
		
		const orgEntries: Entry[] = results.orgs.map(o => ({
			href: undefined,
			id: o.name,
			_id: o._id,
			imgUrl: o.avatarUrl,
			label: o.fullname,
			type: "org",
		}));

		const userEntries: Entry[] = results.users.map(u => ({
			href: undefined,
			id: u.user,
			_id: u._id,
			imgUrl: u.avatarUrl.startsWith("/") ? `https://huggingface.co${u.avatarUrl}` : u.avatarUrl,
			label: u.user,
			type: "user",
			description: u.fullname,
		}));

		return [
			...modelEntries,
			...orgEntries,
			...userEntries,
		];
	}
</script>

<Block
	{visible}
	{elem_id}
	{elem_classes}
	{scale}
	{min_width}
	allow_overflow={false}
	padding={true}
>
	{#if loading_status}
		<StatusTracker
			autoscroll={gradio.autoscroll}
			i18n={gradio.i18n}
			{...loading_status}
		/>
	{/if}

	<label class:container>
		<BlockTitle {show_label} info={undefined}>{label}</BlockTitle>

		<input
			data-testid="textbox"
			type="text"
			class="scroll-hide"
			bind:value
			bind:this={el}
			{placeholder}
			disabled={!interactive}
			dir={rtl ? "rtl" : "ltr"}
			on:focus={handleFocus}
			on:keypress={handle_keypress}
			on:input={handleInput}
		/>
		{#if isOpen}
			<div class="{position} z-40 w-full md:min-w-[24rem]">
				<ul
					class="mt-1 max-h-[calc(100vh-100px)] w-full divide-y divide-gray-100 overflow-hidden overflow-y-auto rounded-lg border border-gray-100 bg-white text-sm shadow-lg dark:divide-gray-900"
				>
					{#if !numResults}
						<QuickSearchEntry
							entry={{
								id: "no-result",
								label: "No results found :(",
								type: "no-results",
							}}
							isSelected={false}
							onClick={() => {}}
						/>
					{/if}

					{#if entries.some(x => x.type === "model")}
						{#each entries.filter(x => ["model", "all-models"].includes(x.type)) as entry}
							<li>
								<QuickSearchEntry {entry} isSelected={isSelected(selectedEntryIdx, entry)} onClick={handleClickEntry} />
							</li>
						{/each}
					{/if}
					
					{#if entries.some(x => x.type === "org")}
						{#each entries.filter(x => x.type === "org") as entry}
							<li>
								<QuickSearchEntry {entry} isSelected={isSelected(selectedEntryIdx, entry)} onClick={handleClickEntry} />
							</li>
						{/each}
					{/if}

					{#if entries.some(x => x.type === "user")}
						{#each entries.filter(x => x.type === "user") as entry}
							<li>
								<QuickSearchEntry {entry} isSelected={isSelected(selectedEntryIdx, entry)} onClick={handleClickEntry} />
							</li>
						{/each}
					{/if}
				</ul>
			</div>
		{/if}
	</label>
</Block>

<style>
	label {
		display: block;
		width: 100%;
	}

	input {
		display: block;
		position: relative;
		outline: none !important;
		box-shadow: var(--input-shadow);
		background: var(--input-background-fill);
		padding: var(--input-padding);
		width: 100%;
		color: var(--body-text-color);
		font-weight: var(--input-text-weight);
		font-size: var(--input-text-size);
		line-height: var(--line-sm);
		border: none;
	}
	.container > input {
		border: var(--input-border-width) solid var(--input-border-color);
		border-radius: var(--input-radius);
	}
	input:disabled {
		-webkit-text-fill-color: var(--body-text-color);
		-webkit-opacity: 1;
		opacity: 1;
	}

	input:focus {
		box-shadow: var(--input-shadow-focus);
		border-color: var(--input-border-color-focus);
	}

	input::placeholder {
		color: var(--input-placeholder-color);
	}
</style>
