<script lang="ts">
	import { tick } from 'svelte'
	import EmailTriggerEditorInner from './EmailTriggerEditorInner.svelte'
	import type { EditEmailTrigger, EmailTrigger } from '$lib/gen'

	interface Props {
		onUpdate?: (cfg?: Record<string, any>) => void
		customSaveBehavior?: (cfg: EmailTrigger | EditEmailTrigger) => void
	}

	let { onUpdate = undefined, customSaveBehavior }: Props = $props()

	let open = $state(false)
	export async function openEdit(ePath: string, isFlow: boolean, fixedScriptPath?: string) {
		open = true
		await tick()
		drawer?.openEdit(ePath, isFlow, undefined, fixedScriptPath)
	}

	export async function openNew(
		is_flow: boolean,
		initial_script_path?: string,
		defaultValues?: Record<string, any>
	) {
		open = true
		await tick()
		drawer?.openNew(is_flow, initial_script_path, defaultValues)
	}

	let drawer: EmailTriggerEditorInner | undefined = $state()
</script>

{#if open}
	<EmailTriggerEditorInner {customSaveBehavior} {onUpdate} bind:this={drawer} />
{/if}
