import { jsx } from "react/jsx-runtime";
import { useCallback, useMemo } from "react";
import { useForm } from "@tachybase/schema";
import { SchemaComponent, useActionContext } from "../../../schema-component/index.mjs";
import { useSchemaInitializerItem } from "../context/index.mjs";
const SchemaInitializerActionModal = (props)=>{
    const { title, schema, buttonText, component, onCancel, onSubmit } = props;
    const useCancelAction = useCallback(()=>{
        const form = useForm();
        const ctx = useActionContext();
        return {
            async run () {
                await (null == onCancel ? void 0 : onCancel());
                ctx.setVisible(false);
                form.reset();
            }
        };
    }, [
        onCancel
    ]);
    const useSubmitAction = useCallback(()=>{
        const form = useForm();
        const ctx = useActionContext();
        return {
            async run () {
                await form.validate();
                await (null == onSubmit ? void 0 : onSubmit(form.values));
                ctx.setVisible(false);
                form.reset();
            }
        };
    }, [
        onSubmit
    ]);
    const defaultSchema = useMemo(()=>({
            type: 'void',
            properties: {
                action1: {
                    type: 'void',
                    'x-component': 'Action',
                    'x-component-props': component ? {
                        component
                    } : {
                        icon: 'PlusOutlined',
                        style: {
                            borderColor: 'var(--colorSettings)',
                            color: 'var(--colorSettings)'
                        },
                        title: buttonText,
                        type: 'dashed'
                    },
                    properties: {
                        drawer1: {
                            'x-decorator': 'Form',
                            'x-component': 'Action.Modal',
                            'x-component-props': {
                                style: {
                                    maxWidth: '520px',
                                    width: '100%'
                                }
                            },
                            type: 'void',
                            title,
                            properties: {
                                ...(null == schema ? void 0 : schema.properties) || schema,
                                footer: {
                                    'x-component': 'Action.Modal.Footer',
                                    type: 'void',
                                    properties: {
                                        cancel: {
                                            title: '{{t("Cancel")}}',
                                            'x-component': 'Action',
                                            'x-component-props': {
                                                useAction: useCancelAction
                                            }
                                        },
                                        submit: {
                                            title: '{{t("Submit")}}',
                                            'x-component': 'Action',
                                            'x-component-props': {
                                                type: 'primary',
                                                useAction: useSubmitAction
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }), [
        buttonText,
        component,
        schema,
        title,
        useCancelAction,
        useSubmitAction
    ]);
    return /*#__PURE__*/ jsx(SchemaComponent, {
        schema: defaultSchema
    });
};
const SchemaInitializerActionModalInternal = ()=>{
    const itemConfig = useSchemaInitializerItem();
    return /*#__PURE__*/ jsx(SchemaInitializerActionModal, {
        ...itemConfig
    });
};
export { SchemaInitializerActionModal, SchemaInitializerActionModalInternal };
