import { jsx } from "react/jsx-runtime";
import { useEffect, useMemo } from "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { useMemoizedFn } from "ahooks";
import { useTranslation } from "react-i18next";
import { SchemaComponent, VariableScopeProvider, useDesignable } from "../schema-component/index.mjs";
import { SchemaSettingsModalItem } from "./SchemaSettings.mjs";
import { VariableInput, getShouldChange, useCurrentUserVariable, useDatetimeVariable } from "./VariableInput/index.mjs";
const EditCustomDefaultValue = ()=>{
    const { t } = useTranslation();
    const { dn } = useDesignable();
    const field = useField();
    const fieldSchema = useFieldSchema();
    fieldSchema['collectionName'];
    const title = fieldSchema.title;
    return /*#__PURE__*/ jsx(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 = null } } = 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 } = useCurrentUserVariable({
        collectionField: {
            uiSchema: fieldSchema
        },
        uiSchema: fieldSchema
    });
    const { datetimeSettings } = useDatetimeVariable({
        operator: null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops['filter-operator'],
        schema: fieldSchema,
        noDisabled: true
    });
    const options = 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 = useMemoizedFn(onChange);
    useEffect(()=>{
        if (fieldSchema.default) handleChange({
            value: fieldSchema.default
        });
    }, [
        fieldSchema.default,
        handleChange
    ]);
    return /*#__PURE__*/ jsx(VariableScopeProvider, {
        scope: options,
        children: /*#__PURE__*/ jsx(VariableInput, {
            ...componentProps,
            renderSchemaComponent: ()=>/*#__PURE__*/ jsx(SchemaComponent, {
                    schema: schema
                }),
            fieldNames: {},
            value: null == value ? void 0 : value.value,
            scope: options,
            onChange: (v)=>{
                onChange({
                    value: v
                });
            },
            shouldChange: getShouldChange({})
        })
    });
};
export { EditCustomDefaultValue, FilterCustomVariableInput };
