"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__, {
    Input: ()=>Input
});
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 schema_namespaceObject = require("@tachybase/schema");
const client_namespaceObject = require("@tachybase/utils/client");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_antd_style_namespaceObject = require("antd-style");
const external_classnames_namespaceObject = require("classnames");
var external_classnames_default = /*#__PURE__*/ __webpack_require__.n(external_classnames_namespaceObject);
const external_dayjs_namespaceObject = require("dayjs");
var external_dayjs_default = /*#__PURE__*/ __webpack_require__.n(external_dayjs_namespaceObject);
const external_lodash_namespaceObject = require("lodash");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../hooks/index.js");
const external_style_js_namespaceObject = require("./style.js");
const external_XButton_js_namespaceObject = require("./XButton.js");
const JT_VALUE_RE = /^\s*{{\s*([^{}]+)\s*}}\s*$/;
function parseValue(value) {
    if (null == value) return 'null';
    const type = typeof value;
    if ('string' === type) {
        const matched = value.match(JT_VALUE_RE);
        if (matched) return matched[1].split('.');
    }
    return 'object' === type && value instanceof Date ? 'date' : type;
}
const ConstantTypes = {
    string: {
        label: '{{t("String")}}',
        value: 'string',
        component: function(param) {
            let { onChange, value, ...otherProps } = param;
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Input, {
                value: value,
                onChange: (ev)=>onChange(ev.target.value),
                ...otherProps
            });
        },
        default: ''
    },
    number: {
        label: '{{t("Number")}}',
        value: 'number',
        component: function(param) {
            let { onChange, value, ...otherProps } = param;
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.InputNumber, {
                value: value,
                onChange: onChange,
                ...otherProps
            });
        },
        default: 0
    },
    boolean: {
        label: '{{t("Boolean")}}',
        value: 'boolean',
        component: function(param) {
            let { onChange, value, ...otherProps } = param;
            const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
                value: value,
                onChange: onChange,
                placeholder: t('Select'),
                options: [
                    {
                        value: true,
                        label: t('True')
                    },
                    {
                        value: false,
                        label: t('False')
                    }
                ],
                ...otherProps
            });
        },
        default: false
    },
    date: {
        label: '{{t("Date")}}',
        value: 'date',
        component: function(param) {
            let { onChange, value, ...otherProps } = param;
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.DatePicker, {
                value: external_dayjs_default()(value),
                onChange: (d)=>d ? onChange(d.toDate()) : null,
                allowClear: false,
                showTime: true,
                ...otherProps
            });
        },
        default: (()=>{
            const now = new Date();
            return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0);
        })()
    },
    null: {
        label: '{{t("Null")}}',
        value: 'null',
        component: function() {
            const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Input, {
                style: {
                    width: '100%'
                },
                readOnly: true,
                placeholder: t('Null'),
                className: "null-value"
            });
        },
        default: null
    }
};
function getTypedConstantOption(type, types, fieldNames) {
    var _ConstantTypes_type;
    const allTypes = Object.values(ConstantTypes);
    const children = (types ? allTypes.filter((item)=>Array.isArray(types) && types.includes(item.value) || true === types) : allTypes).map((item)=>Object.keys(item).reduce((result, key)=>fieldNames[key] in item ? result : Object.assign(result, {
                [fieldNames[key]]: item[key]
            }), item));
    return {
        value: '',
        label: '{{t("Constant")}}',
        children,
        [fieldNames.value]: '',
        [fieldNames.label]: '{{t("Constant")}}',
        [fieldNames.children]: children,
        component: null === (_ConstantTypes_type = ConstantTypes[type]) || void 0 === _ConstantTypes_type ? void 0 : _ConstantTypes_type.component
    };
}
(0, external_antd_style_namespaceObject.createStyles)((param, param1)=>{
    let { css } = param, { disabled } = param1;
    return {};
});
function Input(props) {
    var _constantOption_children;
    const { value = '', onChange, children, button, useTypedConstant, style, className, changeOnSelect, fieldNames } = props;
    const scope = 'function' == typeof props.scope ? props.scope() : props.scope;
    const compile = (0, index_js_namespaceObject.useCompile)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const form = (0, schema_namespaceObject.useForm)();
    const [options, setOptions] = external_react_default().useState([]);
    const [variableText, setVariableText] = external_react_default().useState('');
    const parsed = (0, external_react_namespaceObject.useMemo)(()=>parseValue(value), [
        value
    ]);
    const isConstant = 'string' == typeof parsed;
    const type = isConstant ? parsed : '';
    const variable = isConstant ? null : parsed;
    const names = Object.assign({
        label: 'label',
        value: 'value',
        children: 'children'
    }, null != fieldNames ? fieldNames : {});
    const { component: ConstantComponent, ...constantOption } = (0, external_react_namespaceObject.useMemo)(()=>{
        if (children) return {
            value: '',
            label: t('Constant'),
            [names.value]: '',
            [names.label]: t('Constant')
        };
        if (useTypedConstant) return getTypedConstantOption(type, useTypedConstant, names);
        return {
            value: '',
            label: t('Null'),
            [names.value]: '',
            [names.label]: t('Null'),
            component: ConstantTypes.null.component
        };
    }, [
        type,
        useTypedConstant
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        setOptions([
            compile(constantOption),
            ...scope ? (0, external_lodash_namespaceObject.cloneDeep)(scope) : []
        ]);
    }, [
        scope
    ]);
    const loadData = async (selectedOptions)=>{
        var _option_children;
        const option = selectedOptions[selectedOptions.length - 1];
        if (!(null === (_option_children = option.children) || void 0 === _option_children ? void 0 : _option_children.length) && !option.isLeaf && option.loadChildren) {
            await option.loadChildren(option);
            setOptions((prev)=>[
                    ...prev
                ]);
        }
    };
    const onSwitch = (0, external_react_namespaceObject.useCallback)((next, optionPath)=>{
        if ('' === next[0]) {
            if (next[1]) {
                if (next[1] !== type) {
                    var _ConstantTypes_next_;
                    var _ConstantTypes_next__default;
                    onChange(null !== (_ConstantTypes_next__default = null === (_ConstantTypes_next_ = ConstantTypes[next[1]]) || void 0 === _ConstantTypes_next_ ? void 0 : _ConstantTypes_next_.default) && void 0 !== _ConstantTypes_next__default ? _ConstantTypes_next__default : null, optionPath);
                }
            } else if (variable) onChange(null, optionPath);
            return;
        }
        onChange("{{".concat(next.join('.'), "}}"), optionPath);
    }, [
        type,
        variable,
        onChange
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const run = async ()=>{
            if (!variable || options.length <= 1) return;
            let prevOption = null;
            const labels = [];
            for(let i = 0; i < variable.length; i++){
                const key = variable[i];
                try {
                    if (0 === i) prevOption = options.find((item)=>item[names.value] === key);
                    else {
                        var _prevOption_children;
                        if (prevOption.loadChildren && !(null === (_prevOption_children = prevOption.children) || void 0 === _prevOption_children ? void 0 : _prevOption_children.length)) await prevOption.loadChildren(prevOption);
                        prevOption = prevOption.children.find((item)=>item[names.value] === key);
                    }
                    if (!prevOption) return;
                    labels.push(prevOption[names.label]);
                } catch (err) {
                    (0, client_namespaceObject.error)(err);
                }
            }
            setOptions([
                ...options
            ]);
            setVariableText(labels.join(' / '));
        };
        run();
    }, [
        variable,
        options.length
    ]);
    const disabled = props.disabled || form.disabled;
    const { styles } = (0, external_style_js_namespaceObject.useStyles)({
        disabled
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Space.Compact, {
        style: style,
        className: external_classnames_default()(styles.outerContainer, styles.fixInput, className),
        children: [
            variable ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                className: external_classnames_default()('variable', styles.container2),
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                        role: "button",
                        "aria-label": "variable-tag",
                        onInput: (e)=>e.preventDefault(),
                        onKeyDown: (e)=>{
                            if ('Backspace' !== e.key) {
                                e.preventDefault();
                                return;
                            }
                            onChange(null);
                        },
                        className: external_classnames_default()('ant-input', {
                            'ant-input-disabled': disabled
                        }),
                        contentEditable: !disabled,
                        suppressContentEditableWarning: true,
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                            contentEditable: false,
                            color: "blue",
                            children: variableText
                        })
                    }),
                    disabled ? null : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                        role: "button",
                        "aria-label": "icon-close",
                        className: external_classnames_default()('clear-button'),
                        unselectable: "on",
                        onClick: ()=>onChange(null),
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.CloseCircleFilled, {})
                    })
                ]
            }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                style: {
                    flex: 1
                },
                children: null != children ? children : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ConstantComponent, {
                    role: "button",
                    "aria-label": "variable-constant",
                    value: value,
                    onChange: onChange
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Cascader, {
                options: options,
                value: null != variable ? variable : [
                    '',
                    ...!children && (null === (_constantOption_children = constantOption.children) || void 0 === _constantOption_children ? void 0 : _constantOption_children.length) ? [
                        type
                    ] : []
                ],
                onChange: onSwitch,
                loadData: loadData,
                changeOnSelect: changeOnSelect,
                fieldNames: fieldNames,
                disabled: disabled,
                children: null != button ? button : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_XButton_js_namespaceObject.XButton, {
                    className: styles.button2,
                    type: variable ? 'primary' : 'default'
                })
            })
        ]
    });
}
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
});
