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_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_action_hooks_mjs__ from "../collection-manager/action-hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__ from "../flag-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__ from "../variables/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__ from "../variables/utils/isVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__ from "./SchemaSettings.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__VariableInput_utils_formatVariableScop_mjs__ from "./VariableInput/utils/formatVariableScop.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__VariableInput_VariableInput_mjs__ from "./VariableInput/VariableInput.mjs";
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, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css } = param;
    return {
        defaultInput: css(_templateObject())
    };
});
const SchemaSettingsDefaultValue = function(props) {
    var _actionCtx_fieldSchema;
    const { styles } = useStyles();
    const currentSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const actionCtx = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useActionContext)();
    let targetField;
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
    const { getCollectionJoinField, getCollectionFields, getAllCollectionsInheritChain } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    const variables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useVariables)();
    const localVariables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useLocalVariables)();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecord)();
    const { form } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    const { getFields } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_action_hooks_mjs__.useCollectionFilterOptionsV2)(collection);
    const { isInSubForm, isInSubTable } = (0, __WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__.useFlag)() || {};
    const { name } = collection;
    const collectionField = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']), [
        fieldSchema,
        getCollectionJoinField,
        getField
    ]);
    const fieldSchemaWithoutRequired = __WEBPACK_EXTERNAL_MODULE_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, __WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__.findParentFieldSchema)(fieldSchema);
    const parentCollectionField = parentFieldSchema && getCollectionJoinField(null == parentFieldSchema ? void 0 : parentFieldSchema['x-collection-field']);
    const tableCtx = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__VariableInput_utils_formatVariableScop_mjs__.formatVariableScop)(getFields());
        return scope;
    }, [
        getFields,
        name
    ]);
    const DefaultValueComponent = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            ArrayCollapse: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayCollapse,
            FormLayout: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout,
            VariableInput: (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__.FlagProvider, {
                    isInSubForm: isInSubForm,
                    isInSubTable: isInSubTable,
                    isInSetDefaultValueDialog: true,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__VariableInput_VariableInput_mjs__.VariableInput, {
                        ...props
                    })
                })
        }), [
        isInSubForm,
        isInSubTable
    ]);
    const schema = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__VariableInput_VariableInput_mjs__.getShouldChange)({
                            collectionField,
                            variables,
                            localVariables,
                            getAllCollectionsInheritChain
                        }),
                        renderSchemaComponent: function(props) {
                            const s = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].cloneDeep(fieldSchemaWithoutRequired) || {};
                            s.title = '';
                            s.name = 'default';
                            s['x-read-pretty'] = false;
                            s['x-disabled'] = false;
                            const defaultValue = (0, __WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__.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') {
                                __WEBPACK_EXTERNAL_MODULE_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, __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.isVariable)(defaultValue) ? '' : defaultValue,
                                    style: {
                                        width: '100%',
                                        verticalAlign: 'top',
                                        minWidth: '200px'
                                    }
                                },
                                default: (0, __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.isVariable)(defaultValue) ? '' : defaultValue
                            };
                            return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.FormProvider, {
                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponent, {
                                    schema: schema
                                })
                            });
                        }
                    },
                    title: t('Default value'),
                    default: (0, __WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__.getFieldDefaultValue)(fieldSchema, collectionField)
                }
            }
        }), [
        collectionField,
        fieldSchema,
        fieldSchemaWithoutRequired,
        form,
        getAllCollectionsInheritChain,
        isAllowContextVariable,
        localVariables,
        record,
        returnScope,
        t,
        tableCtx.collection,
        targetField,
        variables,
        styles
    ]);
    const handleSubmit = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__.SchemaSettingsModalItem, {
        title: t('Set default value'),
        components: DefaultValueComponent,
        width: 800,
        schema: schema,
        onSubmit: handleSubmit
    });
};
export { SchemaSettingsDefaultValue };
