"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    SchemaSettingsDefaultValue: ()=>SchemaSettingsDefaultValue
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_style_namespaceObject = require("antd-style");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("../index.js");
const index_js_namespaceObject = require("../block-provider/index.js");
const action_hooks_js_namespaceObject = require("../collection-manager/action-hooks.js");
const external_flag_provider_index_js_namespaceObject = require("../flag-provider/index.js");
const external_variables_index_js_namespaceObject = require("../variables/index.js");
const isVariable_js_namespaceObject = require("../variables/utils/isVariable.js");
const external_SchemaSettings_js_namespaceObject = require("./SchemaSettings.js");
const formatVariableScop_js_namespaceObject = require("./VariableInput/utils/formatVariableScop.js");
const VariableInput_js_namespaceObject = require("./VariableInput/VariableInput.js");
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      & > .tb-form-item {\n        flex: 1;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const useStyles = (0, external_antd_style_namespaceObject.createStyles)((param)=>{
    let { css } = param;
    return {
        defaultInput: css(_templateObject())
    };
});
const SchemaSettingsDefaultValue = function(props) {
    var _actionCtx_fieldSchema;
    const { styles } = useStyles();
    const currentSchema = (0, schema_namespaceObject.useFieldSchema)();
    var _props_fieldSchema;
    const fieldSchema = null !== (_props_fieldSchema = null == props ? void 0 : props.fieldSchema) && void 0 !== _props_fieldSchema ? _props_fieldSchema : currentSchema;
    const field = (0, schema_namespaceObject.useField)();
    const { dn } = (0, external_index_js_namespaceObject.useDesignable)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const actionCtx = (0, external_index_js_namespaceObject.useActionContext)();
    let targetField;
    const { getField } = (0, external_index_js_namespaceObject.useCollection_deprecated)();
    const { getCollectionJoinField, getCollectionFields, getAllCollectionsInheritChain } = (0, external_index_js_namespaceObject.useCollectionManager_deprecated)();
    const variables = (0, external_variables_index_js_namespaceObject.useVariables)();
    const localVariables = (0, external_variables_index_js_namespaceObject.useLocalVariables)();
    const collection = (0, external_index_js_namespaceObject.useCollection_deprecated)();
    const record = (0, external_index_js_namespaceObject.useRecord)();
    const { form } = (0, index_js_namespaceObject.useFormBlockContext)();
    const { getFields } = (0, action_hooks_js_namespaceObject.useCollectionFilterOptionsV2)(collection);
    const { isInSubForm, isInSubTable } = (0, external_flag_provider_index_js_namespaceObject.useFlag)() || {};
    const { name } = collection;
    const collectionField = (0, external_react_namespaceObject.useMemo)(()=>getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']), [
        fieldSchema,
        getCollectionJoinField,
        getField
    ]);
    const fieldSchemaWithoutRequired = external_lodash_default().omit(fieldSchema, 'required');
    if (null == collectionField ? void 0 : collectionField.target) {
        var _fieldSchema_xcomponentprops_fieldNames, _fieldSchema_xcomponentprops;
        targetField = getCollectionJoinField("".concat(collectionField.target, ".").concat((null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : null === (_fieldSchema_xcomponentprops_fieldNames = _fieldSchema_xcomponentprops.fieldNames) || void 0 === _fieldSchema_xcomponentprops_fieldNames ? void 0 : _fieldSchema_xcomponentprops_fieldNames.label) || 'id'));
    }
    const parentFieldSchema = (null == collectionField ? void 0 : collectionField.interface) === 'm2o' && (0, external_SchemaSettings_js_namespaceObject.findParentFieldSchema)(fieldSchema);
    const parentCollectionField = parentFieldSchema && getCollectionJoinField(null == parentFieldSchema ? void 0 : parentFieldSchema['x-collection-field']);
    const tableCtx = (0, index_js_namespaceObject.useTableBlockContext)();
    const isAllowContextVariable = (null == actionCtx ? void 0 : null === (_actionCtx_fieldSchema = actionCtx.fieldSchema) || void 0 === _actionCtx_fieldSchema ? void 0 : _actionCtx_fieldSchema['x-action']) === 'customize:create' && ((null == collectionField ? void 0 : collectionField.interface) === 'm2m' || (null == parentCollectionField ? void 0 : parentCollectionField.type) === 'hasMany' && (null == collectionField ? void 0 : collectionField.interface) === 'm2o');
    const returnScope = (0, external_react_namespaceObject.useCallback)((scope)=>{
        const currentForm = scope.find((item)=>'$nForm' === item.value);
        const fields = getCollectionFields(name);
        if (!(null == fields ? void 0 : fields.length) && currentForm) currentForm.children = (0, formatVariableScop_js_namespaceObject.formatVariableScop)(getFields());
        return scope;
    }, [
        getFields,
        name
    ]);
    const DefaultValueComponent = (0, external_react_namespaceObject.useMemo)(()=>({
            ArrayCollapse: components_namespaceObject.ArrayCollapse,
            FormLayout: components_namespaceObject.FormLayout,
            VariableInput: (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_flag_provider_index_js_namespaceObject.FlagProvider, {
                    isInSubForm: isInSubForm,
                    isInSubTable: isInSubTable,
                    isInSetDefaultValueDialog: true,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(VariableInput_js_namespaceObject.VariableInput, {
                        ...props
                    })
                })
        }), [
        isInSubForm,
        isInSubTable
    ]);
    const schema = (0, external_react_namespaceObject.useMemo)(()=>({
            type: 'object',
            title: t('Set default value'),
            properties: {
                default: {
                    'x-decorator': 'FormItem',
                    'x-component': 'VariableInput',
                    'x-component-props': {
                        ...(null == fieldSchema ? void 0 : fieldSchema['x-component-props']) || {},
                        collectionField,
                        contextCollectionName: isAllowContextVariable && tableCtx.collection,
                        schema: null == collectionField ? void 0 : collectionField.uiSchema,
                        targetFieldSchema: fieldSchema,
                        className: styles.defaultInput,
                        form,
                        record,
                        returnScope,
                        shouldChange: (0, VariableInput_js_namespaceObject.getShouldChange)({
                            collectionField,
                            variables,
                            localVariables,
                            getAllCollectionsInheritChain
                        }),
                        renderSchemaComponent: function(props) {
                            const s = external_lodash_default().cloneDeep(fieldSchemaWithoutRequired) || {};
                            s.title = '';
                            s.name = 'default';
                            s['x-read-pretty'] = false;
                            s['x-disabled'] = false;
                            const defaultValue = (0, external_SchemaSettings_js_namespaceObject.getFieldDefaultValue)(s, collectionField);
                            if (collectionField.target && s['x-component-props']) s['x-component-props'].mode = 'Select';
                            if (null == collectionField ? void 0 : collectionField.uiSchema.type) s.type = collectionField.uiSchema.type;
                            if ((null == collectionField ? void 0 : collectionField.uiSchema['x-component']) === 'Checkbox') {
                                external_lodash_default().set(s, 'x-component-props.defaultChecked', defaultValue);
                                s.type = 'void';
                            }
                            const schema = {
                                ...s || {},
                                'x-decorator': 'FormItem',
                                'x-component-props': {
                                    ...s['x-component-props'],
                                    collectionName: null == collectionField ? void 0 : collectionField.collectionName,
                                    targetField,
                                    onChange: props.onChange,
                                    defaultValue: (0, isVariable_js_namespaceObject.isVariable)(defaultValue) ? '' : defaultValue,
                                    style: {
                                        width: '100%',
                                        verticalAlign: 'top',
                                        minWidth: '200px'
                                    }
                                },
                                default: (0, isVariable_js_namespaceObject.isVariable)(defaultValue) ? '' : defaultValue
                            };
                            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.FormProvider, {
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.SchemaComponent, {
                                    schema: schema
                                })
                            });
                        }
                    },
                    title: t('Default value'),
                    default: (0, external_SchemaSettings_js_namespaceObject.getFieldDefaultValue)(fieldSchema, collectionField)
                }
            }
        }), [
        collectionField,
        fieldSchema,
        fieldSchemaWithoutRequired,
        form,
        getAllCollectionsInheritChain,
        isAllowContextVariable,
        localVariables,
        record,
        returnScope,
        t,
        tableCtx.collection,
        targetField,
        variables,
        styles
    ]);
    const handleSubmit = (0, external_react_namespaceObject.useCallback)((v)=>{
        const schema = {
            ['x-uid']: fieldSchema['x-uid']
        };
        v.default = v.default || null;
        field.value = v.default;
        fieldSchema.default = v.default;
        if (!v.default && 0 !== v.default) field.value = null;
        schema.default = v.default;
        dn.emit('patch', {
            schema,
            currentSchema
        });
    }, [
        currentSchema,
        dn,
        field,
        fieldSchema
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_SchemaSettings_js_namespaceObject.SchemaSettingsModalItem, {
        title: t('Set default value'),
        components: DefaultValueComponent,
        width: 800,
        schema: schema,
        onSubmit: handleSubmit
    });
};
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
});
