import { confirmDialog, type ConfirmOptions } from '$lib/stores/confirm.svelte';

export type { ConfirmOptions };

export { confirmDialog };

/** Tahan submit form sampai user mengonfirmasi di dialog. */
export async function confirmBeforeSubmit(
	e: Event,
	options: ConfirmOptions
): Promise<void> {
	e.preventDefault();

	const target = e.currentTarget as HTMLButtonElement | HTMLFormElement;
	const form = target instanceof HTMLFormElement ? target : target.form;
	if (!form) return;

	if (await confirmDialog(options)) {
		form.requestSubmit();
	}
}
