import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { Cascader } from "antd";
import classnames from "classnames";
import { useTranslation } from "react-i18next";
import { useToken } from "../__builtins__/index.mjs";
import VariableSelect_style from "./VariableSelect.style.mjs";
import { XButton } from "./XButton.mjs";
function VariableSelect(param) {
    let { options, setOptions, onInsert, changeOnSelect = false, fieldNames = {}, className } = param;
    const { t } = useTranslation();
    const [selectedVar, setSelectedVar] = useState([]);
    const { wrapSSR, componentCls, hashId } = VariableSelect_style();
    const { token } = useToken();
    async function loadData(selectedOptions) {
        var _option_children;
        const option = selectedOptions[selectedOptions.length - 1];
        if (!(null == (_option_children = option.children) ? void 0 : _option_children.length) && !option.isLeaf && option.loadChildren) {
            await option.loadChildren(option);
            setOptions((prev)=>[
                    ...prev
                ]);
        }
    }
    return wrapSSR(/*#__PURE__*/ jsx(XButton, {
        className: classnames('x-button', componentCls, hashId, className),
        children: /*#__PURE__*/ jsx(Cascader, {
            placeholder: t('Select a variable'),
            value: [],
            options: options,
            fieldNames: fieldNames,
            loadData: loadData,
            onChange: function() {
                let keyPaths = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [], selectedOptions = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : [];
                var _option_children;
                setSelectedVar(keyPaths);
                if (!keyPaths.length) return;
                const option = selectedOptions[selectedOptions.length - 1];
                if (!(null == option ? void 0 : null == (_option_children = option.children) ? void 0 : _option_children.length)) onInsert(keyPaths);
            },
            changeOnSelect: changeOnSelect,
            onClick: (e)=>{
                if (2 !== e.detail || !changeOnSelect) return;
                for(let n = e.target; n && n !== e.currentTarget; n = n.parentNode){
                    var _n_classList;
                    if (Array.from(null != (_n_classList = n.classList) ? _n_classList : []).includes('ant-cascader-menu-item')) onInsert(selectedVar);
                }
            },
            popupClassName: 'Cascader-popupClassName',
            dropdownRender: changeOnSelect ? (menu)=>/*#__PURE__*/ jsxs(Fragment, {
                    children: [
                        menu,
                        /*#__PURE__*/ jsx("div", {
                            style: {
                                padding: '0.5em',
                                borderTop: "1px solid ".concat(token.colorBorder),
                                color: token.colorTextDescription
                            },
                            children: t('Double click to choose entire object')
                        })
                    ]
                }) : null
        })
    }));
}
export { VariableSelect };
