<script>
    (() => {
        const ctx = window.factoryCrud;
        if (!ctx) {
            return;
        }

        ctx.hooks.afterBoot.push(() => {
            const params = new URL(window.location.href).searchParams;
            if (params.get('open_modal') !== 'create') {
                return;
            }

            ctx.form.reset();
            ctx.clearValidationState();
            ctx.setModalMode({ mode: 'create' });
            ctx.setReturnFlowFields({
                returnEntity: params.get('return_entity') || '',
                returnUrl: params.get('return_url') || '',
                restoreDraftKey: params.get('restore_draft_key') || '',
            });
            ctx.fillFormValues(ctx.readStoredDraft(params.get('restore_draft_key') || '') || {});
            ctx.cleanupFlowQueryParams('open_modal', 'return_entity', 'return_url', 'restore_draft_key');
            ctx.modal.show();
        });
    })();
</script>