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_lodash__ from "lodash";
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__schema_component_antd_filter_useValues_mjs__ from "../../schema-component/antd/filter/useValues.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__ from "../../variables/utils/isVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useBlockCollection_mjs__ from "./hooks/useBlockCollection.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useContextAssociationFields_mjs__ from "./hooks/useContextAssociationFields.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useRecordVariable_mjs__ from "./hooks/useRecordVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useUserVariable_mjs__ from "./hooks/useUserVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useVariableOptions_mjs__ from "./hooks/useVariableOptions.mjs";
const VariableInput = (props)=>{
    const { value, onChange, renderSchemaComponent: RenderSchemaComponent, style, schema, className, contextCollectionName, collectionField, shouldChange, form, record, returnScope = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].identity, targetFieldSchema } = props;
    const { name: blockCollectionName } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useBlockCollection_mjs__.useBlockCollection)();
    const scope = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useVariableScope)();
    const { operator, schema: uiSchema = null == collectionField ? void 0 : collectionField.uiSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_filter_useValues_mjs__.useValues)();
    const variableOptions = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useVariableOptions_mjs__.useVariableOptions)({
        collectionField,
        form,
        record,
        operator,
        uiSchema,
        targetFieldSchema
    });
    const contextVariable = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useContextAssociationFields_mjs__.useContextAssociationFields)({
        schema,
        maxDepth: 2,
        contextCollectionName,
        collectionField
    });
    const { compatOldVariables } = useCompatOldVariables({
        collectionField,
        uiSchema,
        targetFieldSchema,
        blockCollectionName
    });
    if (contextCollectionName && variableOptions.every((item)=>item.value !== contextVariable.value)) variableOptions.push(contextVariable);
    const handleChange = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((value, optionPath)=>{
        if (!shouldChange) return onChange(value);
        setTimeout(async ()=>{
            if (await shouldChange(value, optionPath)) onChange(value);
        });
    }, [
        onChange,
        shouldChange
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.Variable.Input, {
        className: className,
        value: value,
        onChange: handleChange,
        scope: returnScope(compatOldVariables(__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEmpty(scope) ? variableOptions : scope, {
            value
        })),
        style: style,
        changeOnSelect: true,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RenderSchemaComponent, {
            value: value,
            onChange: onChange
        })
    });
};
const getShouldChange = (param)=>{
    let { collectionField, variables, localVariables, getAllCollectionsInheritChain } = param;
    const collectionsInheritChain = collectionField ? getAllCollectionsInheritChain(collectionField.target) : [];
    return async (value, optionPath)=>{
        if (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isString(value) && value.includes('$nRole')) return true;
        if (!(0, __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.isVariable)(value) || !variables || !collectionField) return true;
        if ('json' === collectionField.interface) return true;
        const lastOption = optionPath[optionPath.length - 1];
        if (lastOption && __WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEmpty(lastOption.children) && !lastOption.loadChildren) return true;
        const collectionFieldOfVariable = await variables.getCollectionField(value, localVariables);
        if (!collectionField) return false;
        if ([
            'o2o',
            'o2m',
            'oho'
        ].includes(null == collectionFieldOfVariable ? void 0 : collectionFieldOfVariable.interface)) return false;
        if (!collectionField.target && (null == collectionFieldOfVariable ? void 0 : collectionFieldOfVariable.target)) return false;
        if (collectionField.target && !(null == collectionFieldOfVariable ? void 0 : collectionFieldOfVariable.target)) return false;
        if (collectionField.target && (null == collectionFieldOfVariable ? void 0 : collectionFieldOfVariable.target) && !collectionsInheritChain.includes(null == collectionFieldOfVariable ? void 0 : collectionFieldOfVariable.target)) return false;
        return true;
    };
};
function useCompatOldVariables(props) {
    const { uiSchema, collectionField, noDisabled, targetFieldSchema, blockCollectionName } = props;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { currentUserSettings } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useUserVariable_mjs__.useCurrentUserVariable)({
        maxDepth: 1,
        uiSchema: uiSchema,
        collectionField,
        noDisabled,
        targetFieldSchema
    });
    const { currentRecordSettings } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useRecordVariable_mjs__.useCurrentRecordVariable)({
        schema: uiSchema,
        collectionField,
        noDisabled,
        targetFieldSchema
    });
    const compatOldVariables = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((variables, param)=>{
        let { value } = param;
        if (!(0, __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.isVariable)(value)) return variables;
        variables = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].cloneDeep(variables);
        const systemVariable = {
            value: '$system',
            key: '$system',
            label: t('System variables'),
            isLeaf: false,
            children: [
                {
                    value: 'now',
                    key: 'now',
                    label: t('Current time'),
                    isLeaf: true,
                    depth: 1
                }
            ],
            depth: 0
        };
        const currentTime = {
            value: 'currentTime',
            label: t('Current time'),
            children: null
        };
        if (value.includes('$system')) variables.push(systemVariable);
        if (value.includes("".concat(blockCollectionName, "."))) {
            const variable = variables.find((item)=>'$nForm' === item.value || '$nRecord' === item.value);
            if (variable) variable.value = blockCollectionName;
        }
        if (value.includes('$form')) {
            const variable = variables.find((item)=>'$nForm' === item.value);
            if (variable) variable.value = '$form';
        }
        if (value.includes('currentUser')) {
            const userVariable = variables.find((item)=>'$user' === item.value);
            if (userVariable) userVariable.value = 'currentUser';
            else variables.unshift({
                ...currentUserSettings,
                value: 'currentUser'
            });
        }
        if (value.includes('currentRecord')) {
            const formVariable = variables.find((item)=>'$nRecord' === item.value);
            if (formVariable) formVariable.value = 'currentRecord';
            else variables.unshift({
                ...currentRecordSettings,
                value: 'currentRecord'
            });
        }
        if (value.includes('currentTime')) variables.push(currentTime);
        if (value.includes('$date')) {
            const formVariable = variables.find((item)=>'$nDate' === item.value);
            if (formVariable) formVariable.value = '$date';
        }
        return variables;
    }, [
        blockCollectionName
    ]);
    return {
        compatOldVariables
    };
}
export { VariableInput, getShouldChange, useCompatOldVariables };
