"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const prefix_classname_1 = __importDefault(require("prefix-classname"));
const icons_1 = require("@ant-design/icons");
const const_1 = require("../../../config/const");
const antd_1 = require("antd");
const get_from_main_1 = require("../../../utils/get-from-main");
const { api } = (0, get_from_main_1.getSharedFromMain)();
require("./style.scss");
const dom_utils_1 = require("../../../utils/dom-utils");
const cn = (0, prefix_classname_1.default)('');
const c = (0, prefix_classname_1.default)(`${const_1.CLS_PREFIX}-more-button`);
const MoreButton = react_1.default.memo(({ className, dom, menu }) => {
    const [, setSelectedNode] = (0, get_from_main_1.useSelectedNode)();
    const [paths, setPaths] = react_1.default.useState(null);
    const renderMenuItem = ({ mometa: data, dom: _dom }, i = 0) => {
        const isActive = dom.__mometa.getKey() === data.hash;
        return (react_1.default.createElement(antd_1.Menu.Item, { key: `${data.hash}-${i}`, className: c({ '-active': isActive }), disabled: isActive, onClick: () => {
                setSelectedNode(null);
                _dom.__mometa.selectedKey = data.hash;
                setSelectedNode(_dom);
            } },
            react_1.default.createElement("span", null,
                !!data.container && '*',
                data.name),
            !!isActive && react_1.default.createElement("span", null, " (\u81EA\u5DF1)")));
    };
    return (react_1.default.createElement(antd_1.Dropdown, { className: c('__dropdown'), overlay: react_1.default.createElement(antd_1.Menu, null,
            !!menu && (react_1.default.createElement(react_1.default.Fragment, null,
                menu,
                react_1.default.createElement(antd_1.Menu.Divider, null))),
            react_1.default.createElement(antd_1.Menu.SubMenu, { key: 'level', title: '选中层级', popupOffset: [0, 0] }, !!paths &&
                Array.from(paths.keys()).map((groupDom, i) => {
                    const list = paths.get(groupDom);
                    if (!list.length) {
                        return null;
                    }
                    if (list.length > 1) {
                        return (react_1.default.createElement(antd_1.Menu.ItemGroup, { key: `group-${i}`, title: `${(0, dom_utils_1.getDomName)(groupDom)} (DOM)` }, list.map(renderMenuItem)));
                    }
                    return renderMenuItem(list[0]);
                })),
            react_1.default.createElement(antd_1.Menu.Item, { key: 'cancel-select', onClick: () => {
                    setSelectedNode(null);
                } }, "\u53D6\u6D88\u9009\u4E2D"),
            react_1.default.createElement(antd_1.Menu.Divider, null),
            react_1.default.createElement(antd_1.Menu.Item, { key: 'copy', onClick: () => {
                    return api.handleViewOp('copy', dom);
                } }, "\u91CD\u590D\u4E00\u4EFD")), onVisibleChange: (v) => {
            if (v && !paths) {
                const paths = new Map();
                dom.__mometa.findParents().forEach((data) => {
                    if (data.dom) {
                        const arr = paths.get(data.dom) || [];
                        arr.push(data);
                        paths.set(data.dom, arr);
                    }
                });
                setPaths(paths);
            }
        } },
        react_1.default.createElement(antd_1.Button, { type: "text", size: 'small', className: cn(c(), className), icon: react_1.default.createElement(icons_1.MenuOutlined, null) })));
});
MoreButton.defaultProps = {};
exports.default = MoreButton;
//# sourceMappingURL=index.js.map