"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__, {
    ValueDynamicComponent: ()=>ValueDynamicComponent
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const external_antd_namespaceObject = require("antd");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../block-provider/index.js");
const external_record_provider_index_js_namespaceObject = require("../../record-provider/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const useVariableOptions_js_namespaceObject = require("../VariableInput/hooks/useVariableOptions.js");
const VariableInput_js_namespaceObject = require("../VariableInput/VariableInput.js");
const external_DynamicComponent_js_namespaceObject = require("./DynamicComponent.js");
const { Option } = external_antd_namespaceObject.Select;
const ValueDynamicComponent = (props)=>{
    const { fieldValue, schema, setValue, collectionName } = props;
    const [mode, setMode] = (0, external_react_namespaceObject.useState)((null == fieldValue ? void 0 : fieldValue.mode) || 'constant');
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { form } = (0, index_js_namespaceObject.useFormBlockContext)();
    const record = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const scope = (0, useVariableOptions_js_namespaceObject.useVariableOptions)({
        collectionField: {
            uiSchema: schema
        },
        form,
        record,
        uiSchema: schema,
        noDisabled: true
    });
    const { compatOldVariables } = (0, VariableInput_js_namespaceObject.useCompatOldVariables)({
        collectionField: {
            uiSchema: schema,
            collectionName
        },
        uiSchema: schema,
        blockCollectionName: collectionName
    });
    const constantStyle = (0, external_react_namespaceObject.useMemo)(()=>({
            minWidth: 150,
            maxWidth: 430,
            marginLeft: 5
        }), []);
    const handleChangeOfConstant = (0, external_react_namespaceObject.useCallback)((value)=>{
        setValue({
            mode,
            value
        });
    }, [
        mode,
        setValue
    ]);
    const expressStyle = (0, external_react_namespaceObject.useMemo)(()=>({
            minWidth: 150,
            maxWidth: 430,
            fontSize: 13
        }), []);
    const handleChangeOfExpress = (0, external_react_namespaceObject.useCallback)((value)=>{
        const result = value.replaceAll("".concat(collectionName, "."), '').replaceAll('$system.', '').trim();
        setValue({
            mode,
            value,
            result
        });
    }, [
        collectionName,
        mode,
        setValue
    ]);
    const textAreaStyle = (0, external_react_namespaceObject.useMemo)(()=>({
            minWidth: 460,
            marginRight: 15
        }), []);
    const compatScope = (0, external_react_namespaceObject.useMemo)(()=>compatOldVariables(scope, {
            value: null == fieldValue ? void 0 : fieldValue.value
        }), [
        compatOldVariables,
        null == fieldValue ? void 0 : fieldValue.value,
        scope
    ]);
    const modeMap = {
        constant: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
            role: "button",
            "aria-label": "dynamic-component-linkage-rules",
            style: constantStyle,
            children: /*#__PURE__*/ external_react_default().createElement(external_DynamicComponent_js_namespaceObject.DynamicComponent, {
                value: (null == fieldValue ? void 0 : fieldValue.value) || fieldValue,
                schema,
                onChange: handleChangeOfConstant
            })
        }),
        express: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
            role: "button",
            "aria-label": "dynamic-component-linkage-rules",
            style: expressStyle,
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.Variable.TextArea, {
                    value: null == fieldValue ? void 0 : fieldValue.value,
                    onChange: handleChangeOfExpress,
                    scope: compatScope,
                    style: textAreaStyle
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("span", {
                            style: {
                                marginLeft: '.25em'
                            },
                            className: 'ant-formily-item-extra',
                            children: [
                                t('Syntax references'),
                                ":"
                            ]
                        }),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("a", {
                            href: "https://formulajs.info/functions/",
                            target: "_blank",
                            rel: "noreferrer",
                            children: "Formula.js"
                        })
                    ]
                })
            ]
        })
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Space.Compact, {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Select, {
                role: "button",
                "data-testid": "select-linkage-value-type",
                value: mode,
                style: {
                    width: 150
                },
                onChange: (value)=>{
                    setMode(value);
                    setValue({
                        mode: value
                    });
                },
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Option, {
                        value: "constant",
                        children: t('Constant value')
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Option, {
                        value: "express",
                        children: t('Expression')
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Option, {
                        value: "empty",
                        children: t('Empty')
                    })
                ]
            }),
            modeMap[mode]
        ]
    });
};
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
});
