import React from 'react';
import p from 'prefix-classname';
import { MenuOutlined } from '@ant-design/icons';
import { CLS_PREFIX } from '../../../config/const';
import { Button, Dropdown, Menu } from 'antd';
import { getSharedFromMain, useSelectedNode } from '../../../utils/get-from-main';
const { api } = getSharedFromMain();
import './style.scss';
import { getDomName } from '../../../utils/dom-utils';
const cn = p('');
const c = p(`${CLS_PREFIX}-more-button`);
const MoreButton = React.memo(({ className, dom }) => {
    const [, setSelectedNode] = useSelectedNode();
    const [paths, setPaths] = React.useState(null);
    const renderMenuItem = ({ mometa: data, dom: _dom }, i = 0) => {
        const isActive = dom.__mometa.getKey() === data.hash;
        return (React.createElement(Menu.Item, { key: `${data.hash}-${i}`, className: c({ '-active': isActive }), disabled: isActive, onClick: () => {
                setSelectedNode(null);
                _dom.__mometa.selectedKey = data.hash;
                setSelectedNode(_dom);
            } },
            React.createElement("span", null,
                !!data.container && '*',
                data.name),
            !!isActive && React.createElement("span", null, " (\u81EA\u5DF1)")));
    };
    return (React.createElement(Dropdown, { className: c('__dropdown'), overlay: React.createElement(Menu, null,
            React.createElement(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.createElement(Menu.ItemGroup, { key: `group-${i}`, title: `${getDomName(groupDom)} (DOM)` }, list.map(renderMenuItem)));
                    }
                    return renderMenuItem(list[0]);
                })),
            React.createElement(Menu.Item, { key: 'cancel-select', onClick: () => {
                    setSelectedNode(null);
                } }, "\u53D6\u6D88\u9009\u4E2D"),
            React.createElement(Menu.Divider, null),
            React.createElement(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.createElement(Button, { type: "text", size: 'small', className: cn(c(), className), icon: React.createElement(MenuOutlined, null) })));
});
MoreButton.defaultProps = {};
export default MoreButton;
//# sourceMappingURL=index.js.map