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_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__ from "../__builtins__/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__VariableSelect_style_mjs__ from "./VariableSelect.style.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__XButton_mjs__ from "./XButton.mjs";
function VariableSelect(param) {
    let { options, setOptions, onInsert, changeOnSelect = false, fieldNames = {}, className } = param;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const [selectedVar, setSelectedVar] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const { wrapSSR, componentCls, hashId } = (0, __WEBPACK_EXTERNAL_MODULE__VariableSelect_style_mjs__["default"])();
    const { token } = (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.useToken)();
    async function loadData(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
                ]);
        }
    }
    return wrapSSR(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__XButton_mjs__.XButton, {
        className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])('x-button', componentCls, hashId, className),
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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 ? 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) && void 0 !== _n_classList ? _n_classList : []).includes('ant-cascader-menu-item')) onInsert(selectedVar);
                }
            },
            popupClassName: 'Cascader-popupClassName',
            dropdownRender: changeOnSelect ? (menu)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                    children: [
                        menu,
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.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 };
