import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { CodeMirror } from "@tachybase/components";
import { useForm } from "@tachybase/schema";
import { cloneDeep } from "lodash";
import { css, useTranslation } from "../../../index.mjs";
import { VariableSelect } 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_CodeMirror = (props)=>{
    const { value = '', changeOnSelect, onChange, ...others } = props;
    const scope = 'function' == typeof props.scope ? props.scope() : props.scope;
    const { t } = useTranslation();
    const [options, setOptions] = useState(scope ? cloneDeep(scope) : []);
    const [codeValue, setCodeValue] = useState(value);
    const form = 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__*/ jsxs("div", {
        children: [
            /*#__PURE__*/ jsxs("div", {
                className: css(_templateObject()),
                children: [
                    /*#__PURE__*/ jsx("div", {
                        className: css(_templateObject1()),
                        children: t("Click on the right to select variables \u2192")
                    }),
                    disabled ? null : /*#__PURE__*/ jsx(VariableSelect, {
                        className: css(_templateObject2()),
                        options: options,
                        setOptions: setOptions,
                        onInsert: onInsert,
                        changeOnSelect: changeOnSelect
                    })
                ]
            }),
            /*#__PURE__*/ jsx(CodeMirror, {
                className: css(_templateObject3()),
                value: codeValue,
                defaultLanguage: "Plain Text",
                onChange: handleChange,
                ...others
            })
        ]
    });
};
export { CodeMirror_CodeMirror as CodeMirror };
