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_ahooks__ from "ahooks";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__ from "./SchemaSettings.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__VariableInput_index_mjs__ from "./VariableInput/index.mjs";
const EditCustomDefaultValue = ()=>{
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    fieldSchema['collectionName'];
    const title = fieldSchema.title;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__.SchemaSettingsModalItem, {
        title: t('Set default value'),
        schema: {
            type: 'void',
            title: t('Set default value'),
            properties: {
                default: {
                    title,
                    'x-decorator': 'FormItem',
                    'x-component': FilterCustomVariableInput,
                    'x-component-props': {
                        fieldSchema
                    }
                }
            }
        },
        onSubmit: (param)=>{
            let { default: { value } } = param;
            field.setValue(value);
            fieldSchema['default'] = value;
            fieldSchema['x-component-props'] = {
                ...fieldSchema['x-component-props'],
                defaultValue: value
            };
            dn.emit('patch', {
                schema: {
                    'x-uid': fieldSchema['x-uid'],
                    ['x-component-props']: fieldSchema['x-component-props'],
                    default: fieldSchema.default
                }
            });
            dn.refresh();
        }
    }, "set field default value");
};
const FilterCustomVariableInput = (props)=>{
    var _fieldSchema_xcomponentprops;
    const { value, onChange, fieldSchema } = props;
    const { currentUserSettings } = (0, __WEBPACK_EXTERNAL_MODULE__VariableInput_index_mjs__.useCurrentUserVariable)({
        collectionField: {
            uiSchema: fieldSchema
        },
        uiSchema: fieldSchema
    });
    const { datetimeSettings } = (0, __WEBPACK_EXTERNAL_MODULE__VariableInput_index_mjs__.useDatetimeVariable)({
        operator: null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['filter-operator'],
        schema: fieldSchema,
        noDisabled: true
    });
    const options = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>[
            currentUserSettings,
            datetimeSettings
        ].filter(Boolean), [
        datetimeSettings,
        currentUserSettings
    ]);
    const schema = {
        ...fieldSchema,
        'x-component': fieldSchema['x-component'] || 'Input',
        'x-decorator': '',
        title: '',
        name: 'value'
    };
    const componentProps = fieldSchema['x-component-props'] || {};
    const handleChange = (0, __WEBPACK_EXTERNAL_MODULE_ahooks__.useMemoizedFn)(onChange);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (fieldSchema.default) handleChange({
            value: fieldSchema.default
        });
    }, [
        fieldSchema.default,
        handleChange
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.VariableScopeProvider, {
        scope: options,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__VariableInput_index_mjs__.VariableInput, {
            ...componentProps,
            renderSchemaComponent: ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
                    schema: schema
                }),
            fieldNames: {},
            value: null == value ? void 0 : value.value,
            scope: options,
            onChange: (v)=>{
                onChange({
                    value: v
                });
            },
            shouldChange: (0, __WEBPACK_EXTERNAL_MODULE__VariableInput_index_mjs__.getShouldChange)({})
        })
    });
};
export { EditCustomDefaultValue, FilterCustomVariableInput };
