<script lang="ts">
	import { page } from "$app/state";
	import { resolve } from "$app/paths";
	import RepoCard from "$lib/components/repo/RepoCard.svelte";
	import Section from "$lib/components/ui/Section.svelte";
	import type { RepoCardData } from "../types";
	import Search from "$icon/search";
	import X from "$icon/x";

	let { repos }: { repos: RepoCardData[] } = $props();
	let searchQuery = $derived(page.url.searchParams.get("q") ?? "");
</script>

<Section
	empty={repos.length === 0}
	emptyMessage={searchQuery ? `No repositories match "${searchQuery}".` : "No repositories found."}
>
	{#snippet header()}
		<div class="mb-4">
			<form class="relative flex" method="GET">
				<input type="hidden" name="tab" value="repos" />
				<div class="relative flex flex-1">
					<input
						class="peer flex-1 rounded-l border border-border-default bg-background-default py-1.5 pr-10 pl-3 outline-none focus:border-border-strong focus:ring-1 focus:ring-border-strong"
						type="text"
						name="q"
						value={searchQuery}
						placeholder="Search repositories..."
					/>
					<a
						href={resolve(`${page.url.pathname}?tab=repos` as "/")}
						class="absolute top-1/2 right-3 hidden -translate-y-1/2 text-foreground-subtle peer-[&:not(:placeholder-shown)]:block hover:text-foreground-default"
					>
						<X class="size-4" />
					</a>
				</div>
				<button
					type="submit"
					class="rounded-r border border-l-0 border-border-default bg-background-default p-2 text-foreground-subtle hover:bg-background-subtle hover:text-foreground-default"
				>
					<Search class="size-4" />
				</button>
			</form>
		</div>
	{/snippet}
	{#each repos as repo (repo.repoDid)}
		<RepoCard {repo} showOwner={false} />
	{/each}
</Section>
