<script lang="ts">
	import { deleteWorlds } from '$gql/Mutations';
	import { fetchWorld, worldsQuery } from '$gql/Queries';
	import type { World } from '$gql/graphql';
	import { WorldSortLabel } 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 AddWorld from '$lib/dialogs/AddWorld.svelte';
	import EditWorld from '$lib/dialogs/EditWorld.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 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(worldsQuery(client, { ...data }));
	let worlds = $derived($result.data?.worlds);

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

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

	const edit = (id: number) => {
		fetchWorld(client, id)
			.then((world) => modals.open(EditWorld, { world }))
			.catch(toastFinally);
	};

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

<Head section="worlds" />

<Column>
	<Toolbar>
		{#snippet start()}
			<SelectionControls>
				<DeleteSelection mutation={deleteWorlds} />
			</SelectionControls>
		{/snippet}
		{#snippet center()}
			<Search name="Worlds" {filter} bind:field={filter.include.name.contains} />
			<SelectSort {sort} labels={WorldSortLabel} />
			<SelectItems {pagination} />
		{/snippet}
		{#snippet end()}
			<AddButton title="Add World" onclick={() => modals.open(AddWorld)} />
		{/snippet}
	</Toolbar>
	{#if worlds}
		<Pagination {pagination} total={worlds.count} />
		<main>
			<Cardlets>
				{#each worlds.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={worlds.count} />
	{:else}
		<Guard {result} />
	{/if}
</Column>
