<script lang="ts">
	import type { AddNamespaceInput, Namespace } from '$gql/graphql';
	import SubmitButton from '$lib/components/SubmitButton.svelte';
	import { namespacePending, type FormProps } from '$lib/Form';

	let { initial, submit, children }: FormProps<Namespace, AddNamespaceInput> = $props();

	let input = $state(initial);
	let pending = $derived(input.name.length > 0 && namespacePending(initial, input));

	function onsubmit(event: SubmitEvent) {
		event.preventDefault();

		submit({ ...input });
	}
</script>

<form {onsubmit}>
	<div class="grid-labels">
		<label class="self-center" for="name">Name</label>
		<!-- svelte-ignore a11y_autofocus -->
		<input autofocus required id="name" bind:value={input.name} />
		<label class="self-center" for="sort-name">Sort name</label>
		<input id="name" bind:value={input.sortName} />
	</div>
	<div class="flex gap-4">
		{@render children?.()}
		<div class="grow"></div>
		<SubmitButton {pending} />
	</div>
</form>
