import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__context_index_mjs__ from "../context/index.mjs";
const SchemaInitializerActionModal = (props)=>{
    const { title, schema, buttonText, component, onCancel, onSubmit } = props;
    const useCancelAction = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
        const ctx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
        return {
            async run () {
                await (null == onCancel ? void 0 : onCancel());
                ctx.setVisible(false);
                form.reset();
            }
        };
    }, [
        onCancel
    ]);
    const useSubmitAction = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
        const ctx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
        return {
            async run () {
                await form.validate();
                await (null == onSubmit ? void 0 : onSubmit(form.values));
                ctx.setVisible(false);
                form.reset();
            }
        };
    }, [
        onSubmit
    ]);
    const defaultSchema = (0, __WEBPACK_EXTERNAL_MODULE_react__.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__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
        schema: defaultSchema
    });
};
const SchemaInitializerActionModalInternal = ()=>{
    const itemConfig = (0, __WEBPACK_EXTERNAL_MODULE__context_index_mjs__.useSchemaInitializerItem)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaInitializerActionModal, {
        ...itemConfig
    });
};
export { SchemaInitializerActionModal, SchemaInitializerActionModalInternal };
