<script lang="ts">
	import { deleteCircles } from '$gql/Mutations';
	import { circlesQuery, fetchCircle } from '$gql/Queries';
	import type { Circle } from '$gql/graphql';
	import { CircleSortLabel } from '$lib/Enums';
	import { BasicFilterContext } from '$lib/Filter.svelte';
	import { quickComicFilter } from '$lib/Navigation';
	import { toastFinally } from '$lib/Toasts';
	import AddButton from '$lib/components/AddButton.svelte';
	import Cardlet from '$lib/components/Cardlet.svelte';
	import Empty from '$lib/components/Empty.svelte';
	import Guard from '$lib/components/Guard.svelte';
	import Head from '$lib/components/Head.svelte';
	import Cardlets from '$lib/containers/Cardlets.svelte';
	import Column from '$lib/containers/Column.svelte';
	import AddCircle from '$lib/dialogs/AddCircle.svelte';
	import EditCircle from '$lib/dialogs/EditCircle.svelte';
	import Pagination from '$lib/pagination/Pagination.svelte';
	import Selectable from '$lib/selection/Selectable.svelte';
	import { initSelectionContext } from '$lib/selection/Selection.svelte';
	import SelectionOverlay from '$lib/selection/SelectionOverlay.svelte';
	import DeleteSelection from '$lib/toolbar/DeleteSelection.svelte';
	import FilterOrphaned from '$lib/toolbar/FilterOrphaned.svelte';
	import Search from '$lib/toolbar/Search.svelte';
	import SelectItems from '$lib/toolbar/SelectItems.svelte';
	import SelectSort from '$lib/toolbar/SelectSort.svelte';
	import SelectionControls from '$lib/toolbar/SelectionControls.svelte';
	import Toolbar from '$lib/toolbar/Toolbar.svelte';
	import { getContextClient } from '@urql/svelte';
	import { modals } from 'svelte-modals';
	import type { PageProps } from './$types';

	let { data }: PageProps = $props();
	let pagination = $derived(data.pagination);
	let sort = $derived(data.sort);

	const client = getContextClient();
	let result = $derived(circlesQuery(client, { ...data }));
	let circles = $derived($result.data?.circles);

	let selection = initSelectionContext<Circle>('Circle', (a) => a.name);
	$effect(() => {
		if (circles) {
			selection.view = circles.edges;
		}
	});

	let filter = $state(new BasicFilterContext(data.filter));
	$effect(() => {
		filter = new BasicFilterContext(data.filter);
	});

	const edit = (id: number) => {
		fetchCircle(client, id)
			.then((circle) => modals.open(EditCircle, { circle }))
			.catch(toastFinally);
	};

	const quickFilter = (id: number) => quickComicFilter(id, 'circles');
</script>

<Head section="circles" />

<Column>
	<Toolbar>
		{#snippet start()}
			<SelectionControls>
				<DeleteSelection mutation={deleteCircles} />
			</SelectionControls>
		{/snippet}
		{#snippet center()}
			<Search name="Circles" {filter} bind:field={filter.include.name.contains} />
			<FilterOrphaned {filter} />
			<SelectSort {sort} labels={CircleSortLabel} />
			<SelectItems {pagination} />
		{/snippet}
		{#snippet end()}
			<AddButton title="Add Circle" onclick={() => modals.open(AddCircle)} />
		{/snippet}
	</Toolbar>
	{#if circles}
		<Pagination {pagination} total={circles.count} />
		<main>
			<Cardlets>
				{#each circles.edges as { id, name }, index (id)}
					<Selectable {index} {id} onclick={edit} onauxclick={quickFilter}>
						{#snippet children({ onclick, onauxclick, selected })}
							<Cardlet {name} {onclick} {onauxclick}>
								{#snippet overlay()}
									<SelectionOverlay position="right" centered {selected} />
								{/snippet}
							</Cardlet>
						{/snippet}
					</Selectable>
				{:else}
					<Empty />
				{/each}
			</Cardlets>
		</main>
		<Pagination {pagination} total={circles.count} />
	{:else}
		<Guard {result} />
	{/if}
</Column>
