"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    createSubmitActionSettings: ()=>createSubmitActionSettings,
    SaveMode: ()=>SaveMode
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../../application/index.js");
const SchemaSettings_js_namespaceObject = require("../../../application/schema-settings/SchemaSettings.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_schema_component_index_js_namespaceObject = require("../../../schema-component/index.js");
const Action_Designer_js_namespaceObject = require("../../../schema-component/antd/action/Action.Designer.js");
const useCollectionState_js_namespaceObject = require("../../../schema-settings/DataTemplates/hooks/useCollectionState.js");
const external_schema_settings_SchemaSettings_js_namespaceObject = require("../../../schema-settings/SchemaSettings.js");
const Tree = (0, schema_namespaceObject.connect)(external_antd_namespaceObject.Tree, (0, schema_namespaceObject.mapProps)((props, field)=>{
    (0, external_react_namespaceObject.useEffect)(()=>{
        field.value = props.defaultCheckedKeys || [];
    }, []);
    const [checkedKeys, setCheckedKeys] = (0, external_react_namespaceObject.useState)(props.defaultCheckedKeys || []);
    const onCheck = (checkedKeys)=>{
        setCheckedKeys(checkedKeys);
        field.value = checkedKeys;
    };
    field.onCheck = onCheck;
    return {
        ...props,
        checkedKeys,
        onCheck
    };
}));
function SaveMode() {
    const { dn } = (0, external_schema_component_index_js_namespaceObject.useDesignable)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { name } = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const { getEnableFieldTree, getOnLoadData } = (0, useCollectionState_js_namespaceObject.useCollectionState)(name);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_SchemaSettings_js_namespaceObject.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 SchemaSettings_js_namespaceObject.SchemaSettings({
    name: 'actionSettings:createSubmit',
    items: [
        {
            name: 'editButton',
            Component: Action_Designer_js_namespaceObject.ButtonEditor,
            useComponentProps () {
                const { buttonEditorProps } = (0, index_js_namespaceObject.useSchemaToolbar)();
                return buttonEditorProps;
            }
        },
        {
            name: 'secondConfirmation',
            Component: Action_Designer_js_namespaceObject.SecondConFirm
        },
        {
            name: 'afterSuccessfulSubmission',
            Component: Action_Designer_js_namespaceObject.AfterSuccess
        },
        {
            name: 'jumpDetails',
            type: 'switch',
            useVisible () {
                const { fieldSchema } = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
                const openMode = (0, Action_Designer_js_namespaceObject.findSchema)(fieldSchema);
                return !openMode || 'page' === openMode;
            },
            useComponentProps () {
                var _fieldSchema_xactionsettings;
                const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
                const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
                const field = (0, schema_namespaceObject.useField)();
                const { dn, refresh } = (0, external_schema_component_index_js_namespaceObject.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: Action_Designer_js_namespaceObject.WorkflowConfig,
            useVisible () {
                var _fieldSchema_xactionsettings;
                const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
                return (0, schema_namespaceObject.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: Action_Designer_js_namespaceObject.RemoveButton,
            useComponentProps () {
                const { removeButtonProps } = (0, index_js_namespaceObject.useSchemaToolbar)();
                return removeButtonProps;
            }
        }
    ]
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
