/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var amisCore = require('amis-core');
var React = require('react');
var ReactDOM = require('react-dom');
var Transition = require('react-transition-group/Transition');
var debounce = require('lodash/debounce');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var Transition__default = /*#__PURE__*/_interopDefaultLegacy(Transition);
var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var _a;
// import {createRoot} from 'react-dom/client';
var fadeStyles = (_a = {},
    _a[Transition.ENTERING] = 'in',
    _a[Transition.ENTERED] = 'in',
    _a[Transition.EXITING] = 'out',
    _a);
var ContextMenu = /** @class */ (function (_super) {
    tslib.__extends(ContextMenu, _super);
    function ContextMenu(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            isOpened: false,
            menus: [],
            x: -99999,
            y: -99999,
            cursorX: -99999,
            cursorY: -99999
        };
        _this.menuRef = React__default["default"].createRef();
        _this.contentRef = React__default["default"].createRef();
        _this.unmount = false;
        _this.menuEntered = false;
        _this.resizeObserver = null;
        _this.originInstance = ContextMenu.instance;
        ContextMenu.instance = _this;
        _this.debounceCalculatePosition = debounce__default["default"](_this.autoCalculatePosition.bind(_this), 200);
        return _this;
    }
    ContextMenu.getInstance = function () {
        return tslib.__awaiter(this, void 0, void 0, function () {
            var container, div;
            return tslib.__generator(this, function (_a) {
                if (!ContextMenu.instance || ContextMenu.instance.unmount) {
                    container = document.body;
                    div = document.createElement('div');
                    container.appendChild(div);
                    // if (parseInt(version.split('.')[0], 10) >= 18) {
                    //   const root = createRoot(div);
                    //   await new Promise<void>(resolve =>
                    //     root.render(<ThemedContextMenu ref={() => resolve()} />)
                    //   );
                    // } else {
                    ReactDOM.render(_J$X_(ThemedContextMenu, null), div);
                    // }
                }
                return [2 /*return*/, ContextMenu.instance];
            });
        });
    };
    ContextMenu.prototype.componentDidMount = function () {
        document.body.addEventListener('click', this.handleOutClick, true);
        document.addEventListener('keydown', this.handleKeyDown);
    };
    ContextMenu.prototype.componentWillUnmount = function () {
        this.unmount = true;
        this.debounceCalculatePosition.cancel();
        ContextMenu.instance = this.originInstance;
        document.body.removeEventListener('click', this.handleOutClick, true);
        document.removeEventListener('keydown', this.handleKeyDown);
        // @ts-ignore
        delete this.originInstance;
    };
    ContextMenu.prototype.openContextMenus = function (info, menus, onClose, options) {
        var _this = this;
        if (this.state.isOpened) {
            var _a = this.state, x = _a.x, y = _a.y;
            var cursorX = x + (info.x - (this.prevInfo && this.prevInfo.x ? this.prevInfo.x : 0));
            var cursorY = y + (info.y - (this.prevInfo && this.prevInfo.y ? this.prevInfo.y : 0));
            // 避免 二次触发未进行智能定位 导致遮挡问题
            this.setState({
                x: cursorX,
                y: cursorY,
                cursorX: cursorX,
                cursorY: cursorY,
                menus: menus,
                onClose: onClose,
                contentClassName: options === null || options === void 0 ? void 0 : options.contentClassName,
                preventClose: options === null || options === void 0 ? void 0 : options.preventClose
            }, function () {
                _this.handleEnter(_this.menuRef.current);
            });
        }
        else {
            this.setState({
                isOpened: true,
                x: info.x,
                y: info.y,
                cursorX: info.x,
                cursorY: info.y,
                menus: menus,
                onClose: onClose,
                contentClassName: options === null || options === void 0 ? void 0 : options.contentClassName,
                preventClose: options === null || options === void 0 ? void 0 : options.preventClose
            });
        }
        this.prevInfo = info;
    };
    ContextMenu.prototype.close = function (e) {
        var _this = this;
        var _a, _b, _c, _d;
        if ((_b = (_a = this.state).preventClose) === null || _b === void 0 ? void 0 : _b.call(_a, e)) {
            return;
        }
        (_c = e === null || e === void 0 ? void 0 : e.preventDefault) === null || _c === void 0 ? void 0 : _c.call(e);
        this.menuEntered = false;
        (_d = this.resizeObserver) === null || _d === void 0 ? void 0 : _d.disconnect();
        var onClose = this.state.onClose;
        this.setState({
            isOpened: false,
            x: -99999,
            y: -99999,
            cursorX: -99999,
            cursorY: -99999,
            menus: [],
            contentClassName: '',
            preventClose: undefined
        }, function () {
            onClose === null || onClose === void 0 ? void 0 : onClose(_this);
        });
    };
    ContextMenu.prototype.handleOutClick = function (e) {
        if (!this.menuEntered ||
            !e.target ||
            !this.menuRef.current ||
            this.menuRef.current.contains(e.target)) {
            return;
        }
        if (this.state.isOpened) {
            this.close(e);
        }
    };
    ContextMenu.prototype.handleClick = function (item) {
        var _this = this;
        var onClose = this.state.onClose;
        item.disabled ||
            (Array.isArray(item.children) && item.children.length) ||
            this.setState({
                isOpened: false,
                x: -99999,
                y: -99999,
                cursorX: -99999,
                cursorY: -99999,
                menus: []
            }, function () {
                var _a;
                (_a = item.onSelect) === null || _a === void 0 ? void 0 : _a.call(item, item.data);
                onClose === null || onClose === void 0 ? void 0 : onClose(_this);
            });
    };
    ContextMenu.prototype.handleKeyDown = function (e) {
        if (e.keyCode === 27 && this.state.isOpened) {
            this.close(e);
        }
    };
    ContextMenu.prototype.handleMouseEnter = function (item) {
        item.disabled || !item.onHighlight || item.onHighlight(true, item.data);
    };
    ContextMenu.prototype.handleMouseLeave = function (item) {
        item.disabled || !item.onHighlight || item.onHighlight(false, item.data);
    };
    ContextMenu.prototype.handleEnter = function (menu) {
        this.autoCalculatePosition(menu);
    };
    ContextMenu.prototype.handleEntered = function (menu) {
        var _this = this;
        this.menuEntered = true;
        if (!this.contentRef.current || !window.ResizeObserver) {
            return;
        }
        // 监听菜单大小变化，并自动重新计算位置
        this.resizeObserver = new ResizeObserver(function (entries) {
            entries.length && _this.debounceCalculatePosition(menu);
        });
        this.resizeObserver.observe(this.contentRef.current);
    };
    ContextMenu.prototype.autoCalculatePosition = function (menu) {
        // 智能定位，选择一个合适的对齐方式。
        var info = amisCore.calculatePosition('asContextMenu', menu.lastChild, menu.children[1], menu.children[0]);
        var align = info.positionLeft + 300 < window.innerWidth ? 'right' : 'left';
        this.setState({
            x: info.positionLeft,
            y: info.positionTop,
            align: align
        });
    };
    ContextMenu.prototype.handleSelfContextMenu = function (e) {
        e.preventDefault();
    };
    ContextMenu.prototype.renderMenus = function (menus) {
        var _this = this;
        var cx = this.props.classnames;
        return menus.map(function (item, index) {
            if (item === '|') {
                return _J$X_("li", { key: index, className: cx('ContextMenu-divider') });
            }
            var hasChildren = Array.isArray(item.children) && item.children.length;
            return (_J$X_("li", { key: "".concat(item.label, "-").concat(index), className: cx('ContextMenu-item', item.className, {
                    'has-child': hasChildren,
                    'is-disabled': item.disabled,
                    'is-active': item.selected
                }) },
                _J$X_("a", { onClick: _this.handleClick.bind(_this, item), onMouseEnter: _this.handleMouseEnter.bind(_this, item), onMouseLeave: _this.handleMouseLeave.bind(_this, item) },
                    item.icon ? (_J$X_("span", { className: cx('ContextMenu-itemIcon', item.icon) })) : null,
                    item.label,
                    hasChildren ? _J$X_("i", { className: "fas fa-chevron-right" }) : null,
                    item.selected ? _J$X_("i", { className: "fas fa-check" }) : null),
                hasChildren ? (_J$X_("ul", { className: cx('ContextMenu-subList') }, _this.renderMenus(item.children))) : null));
        });
    };
    ContextMenu.prototype.render = function () {
        var _this = this;
        var _a = this.props, className = _a.className; _a.container; var cx = _a.classnames;
        return (_J$X_(Transition__default["default"], { mountOnEnter: true, unmountOnExit: true, onEnter: this.handleEnter, in: this.state.isOpened, timeout: 500, onEntered: this.handleEntered }, function (status) { return (_J$X_("div", { ref: _this.menuRef, role: "contextmenu", className: cx('ContextMenu', {
                'ContextMenu--left': _this.state.align === 'left'
            }, className, _this.state.contentClassName), onContextMenu: _this.handleSelfContextMenu },
            _J$X_("div", { className: cx("ContextMenu-overlay", fadeStyles[status]) }),
            _J$X_("div", { className: cx("ContextMenu-cursor"), style: {
                    left: "".concat(_this.state.cursorX, "px"),
                    top: "".concat(_this.state.cursorY, "px")
                } }),
            _J$X_("div", { ref: _this.contentRef, style: { left: "".concat(_this.state.x, "px"), top: "".concat(_this.state.y, "px") }, className: cx("ContextMenu-menu", fadeStyles[status]) }, typeof _this.state.menus === 'function' ? (_this.state.menus()) : (_J$X_("ul", { className: cx('ContextMenu-list') }, _this.renderMenus(_this.state.menus)))))); }));
    };
    ContextMenu.instance = null;
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object, Array, Function, Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ContextMenu.prototype, "openContextMenus", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Event]),
        tslib.__metadata("design:returntype", void 0)
    ], ContextMenu.prototype, "close", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Event]),
        tslib.__metadata("design:returntype", void 0)
    ], ContextMenu.prototype, "handleOutClick", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [KeyboardEvent]),
        tslib.__metadata("design:returntype", void 0)
    ], ContextMenu.prototype, "handleKeyDown", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [HTMLElement]),
        tslib.__metadata("design:returntype", void 0)
    ], ContextMenu.prototype, "handleEnter", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [HTMLElement]),
        tslib.__metadata("design:returntype", void 0)
    ], ContextMenu.prototype, "handleEntered", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [HTMLElement]),
        tslib.__metadata("design:returntype", void 0)
    ], ContextMenu.prototype, "autoCalculatePosition", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ContextMenu.prototype, "handleSelfContextMenu", null);
    return ContextMenu;
}(React__default["default"].Component));
var ThemedContextMenu = amisCore.themeable(ContextMenu);
function openContextMenus(info, menus, onClose, options) {
    return tslib.__awaiter(this, void 0, void 0, function () {
        return tslib.__generator(this, function (_a) {
            return [2 /*return*/, ContextMenu.getInstance().then(function (instance) {
                    return instance.openContextMenus(info, menus, onClose, options);
                })];
        });
    });
}
function closeContextMenus() {
    return tslib.__awaiter(this, void 0, void 0, function () {
        return tslib.__generator(this, function (_a) {
            return [2 /*return*/, ContextMenu.getInstance().then(function (instance) { return instance === null || instance === void 0 ? void 0 : instance.close(); })];
        });
    });
}

exports.ContextMenu = ContextMenu;
exports.ThemedContextMenu = ThemedContextMenu;
exports.closeContextMenus = closeContextMenus;
exports["default"] = ThemedContextMenu;
exports.openContextMenus = openContextMenus;
