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_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_schema_settings_SchemaSettings_mjs__ from "../../../application/schema-settings/SchemaSettings.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_action_Action_Designer_mjs__ from "../../../schema-component/antd/action/Action.Designer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_DataTemplates_hooks_useCollectionState_mjs__ from "../../../schema-settings/DataTemplates/hooks/useCollectionState.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__ from "../../../schema-settings/SchemaSettings.mjs";
const Tree = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)(__WEBPACK_EXTERNAL_MODULE_antd__.Tree, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapProps)((props, field)=>{
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        field.value = props.defaultCheckedKeys || [];
    }, []);
    const [checkedKeys, setCheckedKeys] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(props.defaultCheckedKeys || []);
    const onCheck = (checkedKeys)=>{
        setCheckedKeys(checkedKeys);
        field.value = checkedKeys;
    };
    field.onCheck = onCheck;
    return {
        ...props,
        checkedKeys,
        onCheck
    };
}));
function SaveMode() {
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { name } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { getEnableFieldTree, getOnLoadData } = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_DataTemplates_hooks_useCollectionState_mjs__.useCollectionState)(name);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsModalItem, {
        title: t('Save mode'),
        components: {
            Tree
        },
        scope: {
            getEnableFieldTree,
            name,
            getOnLoadData
        },
        schema: {
            type: 'object',
            title: t('Save mode'),
            properties: {
                saveMode: {
                    'x-decorator': 'FormItem',
                    'x-component': 'Radio.Group',
                    default: field.componentProps.saveMode || 'create',
                    enum: [
                        {
                            value: 'create',
                            label: '{{t("Insert")}}'
                        },
                        {
                            value: 'firstOrCreate',
                            label: '{{t("Insert if not exists")}}'
                        },
                        {
                            value: 'updateOrCreate',
                            label: '{{t("Insert if not exists, or update")}}'
                        }
                    ]
                },
                filterKeys: {
                    type: 'array',
                    title: '{{ t("Determine whether a record exists by the following fields") }}',
                    required: true,
                    default: field.componentProps.filterKeys,
                    'x-decorator': 'FormItem',
                    'x-component': 'Tree',
                    'x-component-props': {
                        treeData: [],
                        checkable: true,
                        checkStrictly: true,
                        selectable: false,
                        loadData: '{{ getOnLoadData($self) }}',
                        defaultCheckedKeys: field.componentProps.filterKeys,
                        rootStyle: {
                            padding: '8px 0',
                            border: '1px solid #d9d9d9',
                            borderRadius: '2px',
                            maxHeight: '30vh',
                            overflow: 'auto',
                            margin: '2px 0'
                        }
                    },
                    'x-reactions': [
                        {
                            dependencies: [
                                '.saveMode'
                            ],
                            fulfill: {
                                state: {
                                    hidden: '{{ $deps[0]==="create"}}',
                                    componentProps: {
                                        treeData: '{{ getEnableFieldTree(name, $self) }}'
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        },
        onSubmit: (param)=>{
            let { saveMode, filterKeys } = param;
            field.componentProps.saveMode = saveMode;
            field.componentProps.filterKeys = filterKeys;
            fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
            fieldSchema['x-component-props'].saveMode = saveMode;
            fieldSchema['x-component-props'].filterKeys = filterKeys;
            dn.emit('patch', {
                schema: {
                    ['x-uid']: fieldSchema['x-uid'],
                    'x-component-props': {
                        ...fieldSchema['x-component-props']
                    }
                }
            });
            dn.refresh();
        }
    });
}
const createSubmitActionSettings = new __WEBPACK_EXTERNAL_MODULE__application_schema_settings_SchemaSettings_mjs__.SchemaSettings({
    name: 'actionSettings:createSubmit',
    items: [
        {
            name: 'editButton',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_component_antd_action_Action_Designer_mjs__.ButtonEditor,
            useComponentProps () {
                const { buttonEditorProps } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaToolbar)();
                return buttonEditorProps;
            }
        },
        {
            name: 'secondConfirmation',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_component_antd_action_Action_Designer_mjs__.SecondConFirm
        },
        {
            name: 'afterSuccessfulSubmission',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_component_antd_action_Action_Designer_mjs__.AfterSuccess
        },
        {
            name: 'jumpDetails',
            type: 'switch',
            useVisible () {
                const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
                const openMode = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_action_Action_Designer_mjs__.findSchema)(fieldSchema);
                return !openMode || 'page' === openMode;
            },
            useComponentProps () {
                var _fieldSchema_xactionsettings;
                const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                const { dn, refresh } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                return {
                    title: t('Jump details'),
                    checked: null == fieldSchema ? void 0 : null === (_fieldSchema_xactionsettings = fieldSchema['x-action-settings']) || void 0 === _fieldSchema_xactionsettings ? void 0 : _fieldSchema_xactionsettings.pageMode,
                    onChange (checked) {
                        const schema = {
                            'x-uid': fieldSchema['x-uid']
                        };
                        field['actionSetting'] = {
                            ...null == field ? void 0 : field['ationSetting'],
                            pageMode: checked
                        };
                        fieldSchema['x-action-settings'] = {
                            ...null == fieldSchema ? void 0 : fieldSchema['x-action-settings'],
                            pageMode: checked
                        };
                        schema['x-action-settings'] = fieldSchema['x-action-settings'];
                        dn.emit('patch', {
                            schema
                        });
                        refresh();
                    }
                };
            }
        },
        {
            name: 'workflowConfig',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_component_antd_action_Action_Designer_mjs__.WorkflowConfig,
            useVisible () {
                var _fieldSchema_xactionsettings;
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                return (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isValid)(null == fieldSchema ? void 0 : null === (_fieldSchema_xactionsettings = fieldSchema['x-action-settings']) || void 0 === _fieldSchema_xactionsettings ? void 0 : _fieldSchema_xactionsettings.triggerWorkflows);
            }
        },
        {
            name: 'saveMode',
            Component: SaveMode
        },
        {
            name: 'remove',
            sort: 100,
            Component: __WEBPACK_EXTERNAL_MODULE__schema_component_antd_action_Action_Designer_mjs__.RemoveButton,
            useComponentProps () {
                const { removeButtonProps } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaToolbar)();
                return removeButtonProps;
            }
        }
    ]
});
export { SaveMode, createSubmitActionSettings };
