"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    VariableInput: ()=>VariableInput,
    getShouldChange: ()=>getShouldChange,
    useCompatOldVariables: ()=>useCompatOldVariables
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../schema-component/index.js");
const useValues_js_namespaceObject = require("../../schema-component/antd/filter/useValues.js");
const isVariable_js_namespaceObject = require("../../variables/utils/isVariable.js");
const useBlockCollection_js_namespaceObject = require("./hooks/useBlockCollection.js");
const useContextAssociationFields_js_namespaceObject = require("./hooks/useContextAssociationFields.js");
const useRecordVariable_js_namespaceObject = require("./hooks/useRecordVariable.js");
const useUserVariable_js_namespaceObject = require("./hooks/useUserVariable.js");
const useVariableOptions_js_namespaceObject = require("./hooks/useVariableOptions.js");
const VariableInput = (props)=>{
    const { value, onChange, renderSchemaComponent: RenderSchemaComponent, style, schema, className, contextCollectionName, collectionField, shouldChange, form, record, returnScope = external_lodash_default().identity, targetFieldSchema } = props;
    const { name: blockCollectionName } = (0, useBlockCollection_js_namespaceObject.useBlockCollection)();
    const scope = (0, index_js_namespaceObject.useVariableScope)();
    const { operator, schema: uiSchema = null == collectionField ? void 0 : collectionField.uiSchema } = (0, useValues_js_namespaceObject.useValues)();
    const variableOptions = (0, useVariableOptions_js_namespaceObject.useVariableOptions)({
        collectionField,
        form,
        record,
        operator,
        uiSchema,
        targetFieldSchema
    });
    const contextVariable = (0, useContextAssociationFields_js_namespaceObject.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, external_react_namespaceObject.useCallback)((value, optionPath)=>{
        if (!shouldChange) return onChange(value);
        setTimeout(async ()=>{
            if (await shouldChange(value, optionPath)) onChange(value);
        });
    }, [
        onChange,
        shouldChange
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.Variable.Input, {
        className: className,
        value: value,
        onChange: handleChange,
        scope: returnScope(compatOldVariables(external_lodash_default().isEmpty(scope) ? variableOptions : scope, {
            value
        })),
        style: style,
        changeOnSelect: true,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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 (external_lodash_default().isString(value) && value.includes('$nRole')) return true;
        if (!(0, isVariable_js_namespaceObject.isVariable)(value) || !variables || !collectionField) return true;
        if ('json' === collectionField.interface) return true;
        const lastOption = optionPath[optionPath.length - 1];
        if (lastOption && external_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, external_react_i18next_namespaceObject.useTranslation)();
    const { currentUserSettings } = (0, useUserVariable_js_namespaceObject.useCurrentUserVariable)({
        maxDepth: 1,
        uiSchema: uiSchema,
        collectionField,
        noDisabled,
        targetFieldSchema
    });
    const { currentRecordSettings } = (0, useRecordVariable_js_namespaceObject.useCurrentRecordVariable)({
        schema: uiSchema,
        collectionField,
        noDisabled,
        targetFieldSchema
    });
    const compatOldVariables = (0, external_react_namespaceObject.useCallback)((variables, param)=>{
        let { value } = param;
        if (!(0, isVariable_js_namespaceObject.isVariable)(value)) return variables;
        variables = external_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
    };
}
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
