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__tachybase_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__VariableSelect_mjs__ from "./VariableSelect.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n          display: flex;\n          border: dashed 1px #eee;\n          flex-direction: row;\n        "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n            flex: 1;\n            text-align: center;\n            color: #e5e5e5;\n          "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
function _templateObject2() {
    const data = _tagged_template_literal([
        ""
    ]);
    _templateObject2 = function() {
        return data;
    };
    return data;
}
function _templateObject3() {
    const data = _tagged_template_literal([
        "\n          border: dashed 1px #eee;\n        "
    ]);
    _templateObject3 = function() {
        return data;
    };
    return data;
}
const CodeMirror = (props)=>{
    const { value = '', changeOnSelect, onChange, ...others } = props;
    const scope = 'function' == typeof props.scope ? props.scope() : props.scope;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useTranslation)();
    const [options, setOptions] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(scope ? (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(scope) : []);
    const [codeValue, setCodeValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(value);
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const disabled = props.disabled || form.disabled;
    const onInsert = (selected)=>{
        const variable = "{{".concat(selected.join('.'), "}}");
        const nextValue = "".concat(variable, "\n").concat(codeValue);
        setCodeValue(nextValue);
        null == onChange || onChange(nextValue);
    };
    const handleChange = (nextValue)=>{
        setCodeValue(nextValue);
        null == onChange || onChange(nextValue);
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                className: (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.css)(_templateObject()),
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                        className: (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.css)(_templateObject1()),
                        children: t('Click on the right to select variables →')
                    }),
                    disabled ? null : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__VariableSelect_mjs__.VariableSelect, {
                        className: (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.css)(_templateObject2()),
                        options: options,
                        setOptions: setOptions,
                        onInsert: onInsert,
                        changeOnSelect: changeOnSelect
                    })
                ]
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.CodeMirror, {
                className: (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.css)(_templateObject3()),
                value: codeValue,
                defaultLanguage: "Plain Text",
                onChange: handleChange,
                ...others
            })
        ]
    });
};
export { CodeMirror };
