import { jsx } from "react/jsx-runtime";
import { useCallback } from "react";
import { useTranslation } from "react-i18next";
import { DatePickerProvider, useCollectionManager_deprecated, useRecord } from "../index.mjs";
import { useFormBlockContext } from "../block-provider/index.mjs";
import { useCollectionFilterOptionsV2 } from "../collection-manager/action-hooks.mjs";
import { FlagProvider, useFlag } from "../flag-provider/index.mjs";
import { useLocalVariables, useVariables } from "../variables/index.mjs";
import { SchemaSettingsModalItem } from "./SchemaSettings.mjs";
import { BaseVariableProvider } from "./VariableInput/hooks/useBaseVariable.mjs";
import { VariableInput, getShouldChange } from "./VariableInput/VariableInput.mjs";
const SchemaSettingsDataScope = function(props) {
    const { t } = useTranslation();
    const { getFields } = useCollectionFilterOptionsV2(props.collectionName);
    const record = useRecord();
    const { form } = useFormBlockContext();
    const variables = useVariables();
    const localVariables = useLocalVariables();
    const { getAllCollectionsInheritChain } = useCollectionManager_deprecated();
    const { isInSubForm, isInSubTable } = useFlag() || {};
    const dynamicComponent = useCallback((props)=>/*#__PURE__*/ jsx(DatePickerProvider, {
            value: {
                utc: false
            },
            children: /*#__PURE__*/ jsx(VariableInput, {
                ...props,
                form: form,
                record: record,
                shouldChange: getShouldChange({
                    collectionField: props.collectionField,
                    variables,
                    localVariables,
                    getAllCollectionsInheritChain
                })
            })
        }), [
        form,
        getAllCollectionsInheritChain,
        localVariables,
        record,
        variables
    ]);
    const getSchema = ()=>({
            type: 'object',
            title: t('Set the data scope'),
            properties: {
                filter: {
                    enum: props.collectionFilterOption || getFields(),
                    'x-decorator': (props)=>/*#__PURE__*/ jsx(BaseVariableProvider, {
                            ...props,
                            children: /*#__PURE__*/ jsx(FlagProvider, {
                                isInSubForm: isInSubForm,
                                isInSubTable: isInSubTable,
                                children: props.children
                            })
                        }),
                    'x-decorator-props': {
                        isDisabled
                    },
                    'x-component': 'Filter',
                    'x-component-props': {
                        collectionName: props.collectionName,
                        dynamicComponent: props.dynamicComponent || dynamicComponent
                    }
                }
            }
        });
    return /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
        title: t('Set the data scope'),
        initialValues: {
            filter: props.defaultFilter
        },
        schema: getSchema,
        onSubmit: props.onSubmit
    });
};
function isDisabled(params) {
    var _option_schema;
    const { option, collectionField, uiSchema } = params;
    if (!uiSchema || !collectionField) return true;
    if ('json' === collectionField.interface) return false;
    if (option.target) return false;
    if ([
        'input',
        'markdown',
        'richText',
        'textarea',
        'username'
    ].includes(collectionField.interface)) {
        var _option_schema1;
        return ![
            'string',
            'number'
        ].includes(null == (_option_schema1 = option.schema) ? void 0 : _option_schema1.type);
    }
    if (collectionField.interface && option.interface) return collectionField.interface !== option.interface;
    if ((null == uiSchema ? void 0 : uiSchema['x-component']) !== (null == (_option_schema = option.schema) ? void 0 : _option_schema['x-component'])) return true;
    return false;
}
export { SchemaSettingsDataScope };
