import { MainMenuItemKey } from "../ts-types/base";

export class MenuManager {
    constructor(sheet) {
        this.sheet = sheet, this.createMainMenu();
    }
    createMainMenu() {
        var _a, _b;
        const menu = document.createElement("div");
        menu.className = "vtable-sheet-main-menu", menu.style.position = "relative";
        const menuButton = document.createElement("span");
        menuButton.className = "vtable-sheet-main-menu-button", menuButton.innerHTML = '<svg t="1754379519717" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1496" width="24" height="24"><path d="M510.435876 67.959811c-245.428735 0-444.382996 198.954261-444.382996 444.373787 0 245.420549 198.954261 444.373787 444.382996 444.373787 245.410316 0 444.372763-198.953238 444.372763-444.373787C954.807616 266.914072 755.846192 67.959811 510.435876 67.959811zM510.435876 901.156184c-214.743876 0-388.831796-174.08792-388.831796-388.822586 0-214.743876 174.088944-388.831796 388.831796-388.831796 214.732619 0 388.822586 174.08792 388.822586 388.831796C899.257439 727.068264 725.167472 901.156184 510.435876 901.156184zM666.028561 329.355193 337.411171 329.355193c-15.117302 0-27.384697 15.60235-27.384697 34.844599 0 19.259646 12.267395 34.861996 27.384697 34.861996l328.618413 0c15.124466 0 27.375487-15.60235 27.375487-34.861996C693.404048 344.957543 681.15405 329.355193 666.028561 329.355193zM666.028561 486.191194 337.411171 486.191194c-15.117302 0-27.384697 15.601326-27.384697 34.852786 0 19.25146 12.267395 34.853809 27.384697 34.853809l328.618413 0c15.124466 0 27.375487-15.601326 27.375487-34.853809C693.404048 501.792521 681.15405 486.191194 666.028561 486.191194zM666.028561 625.604384 337.411171 625.604384c-15.117302 0-27.384697 15.60235-27.384697 34.845623 0 19.25146 12.267395 34.861996 27.384697 34.861996l328.618413 0c15.124466 0 27.375487-15.611559 27.375487-34.861996C693.404048 641.206734 681.15405 625.604384 666.028561 625.604384z" fill="#8a8a8a" p-id="1497"></path></svg>', 
        menu.appendChild(menuButton);
        const menuContainer = document.createElement("div");
        menuContainer.className = "vtable-sheet-main-menu-container", menu.appendChild(menuContainer);
        const menuItems = document.createElement("ul");
        return menuItems.className = "vtable-sheet-main-menu-items", menuContainer.appendChild(menuItems), 
        null === (_b = null === (_a = this.sheet.getOptions().mainMenu) || void 0 === _a ? void 0 : _a.items) || void 0 === _b || _b.forEach((item => {
            const li = document.createElement("li");
            li.className = "vtable-sheet-main-menu-item", li.textContent = item.name, li.title = item.description || "", 
            item.items && (li.classList.add("vtable-sheet-main-menu-item-has-children"), li.onmouseenter = () => {
                const subMenu = this.createSubMenu(item.items);
                li.appendChild(subMenu);
            }, li.onmouseleave = () => {
                li.classList.remove("vtable-sheet-main-menu-item-has-children-hover"), li.querySelector(".vtable-sheet-main-menu-sub-menu") && li.removeChild(li.querySelector(".vtable-sheet-main-menu-sub-menu"));
            }), li.addEventListener("click", (() => {
                item.onClick ? item.onClick() : item.menuKey && this.handleMenuClick(item.menuKey);
            })), menuItems.appendChild(li);
        })), menuButton.addEventListener("click", (e => {
            e.stopPropagation(), menuContainer.classList.toggle("active");
        })), this.clickOutsideHandler = e => {
            menu.contains(e.target) || menuContainer.classList.remove("active");
        }, document.addEventListener("click", this.clickOutsideHandler), this.menuContainer = menuContainer, 
        menu;
    }
    createSubMenu(items) {
        const subMenuContainer = document.createElement("div");
        subMenuContainer.className = "vtable-sheet-submenu-container";
        const subMenuList = document.createElement("ul");
        return subMenuList.className = "vtable-sheet-main-menu-items", items.forEach((item => {
            var _a;
            const li = document.createElement("li");
            li.className = "vtable-sheet-submenu-item", li.textContent = item.name, li.title = item.description || "", 
            li.addEventListener("click", (e => {
                e.stopPropagation(), item.onClick ? item.onClick() : item.menuKey && this.handleMenuClick(item.menuKey), 
                this.hideAllSubMenus();
            })), (null === (_a = item.items) || void 0 === _a ? void 0 : _a.length) && (li.classList.add("vtable-sheet-main-menu-item-has-children"), 
            li.addEventListener("mouseenter", (() => {
                this.hideAllSubMenus(li);
                const nestedSubMenu = this.createSubMenu(item.items);
                li.appendChild(nestedSubMenu), this.adjustSubMenuPosition(nestedSubMenu);
            }))), subMenuList.appendChild(li);
        })), subMenuContainer.appendChild(subMenuList), subMenuContainer;
    }
    adjustSubMenuPosition(subMenu) {
        const rect = subMenu.getBoundingClientRect();
        rect.right > window.innerWidth && (subMenu.style.left = "auto", subMenu.style.right = "100%"), 
        rect.bottom > window.innerHeight && (subMenu.style.top = "auto", subMenu.style.bottom = "0");
    }
    hideAllSubMenus(exceptElement) {
        document.querySelectorAll(".vtable-sheet-submenu-container").forEach((menu => {
            menu !== (null == exceptElement ? void 0 : exceptElement.closest(".vtable-sheet-submenu-container")) && menu.remove();
        })), this.menuContainer.classList.remove("active");
    }
    handleMenuClick(menuKey) {
        const tableInstance = this.sheet.getActiveSheet().tableInstance, eventManager = this.sheet.getSpreadSheetEventManager();
        switch (menuKey) {
          case MainMenuItemKey.IMPORT:
            this.sheet.importFileToSheet && this.sheet.importFileToSheet();
            break;

          case MainMenuItemKey.EXPORT_CURRENT_SHEET_CSV:
            try {
                eventManager.emitExportStart("csv", !1), (null == tableInstance ? void 0 : tableInstance.exportToCsv) ? (tableInstance.exportToCsv(), 
                eventManager.emitExportCompleted("csv", !1, 1)) : eventManager.emitExportError("csv", !1, "TableExportPlugin not configured");
            } catch (error) {
                const errorMessage = error instanceof Error ? error.message : String(error);
                eventManager.emitExportError("csv", !1, errorMessage);
            }
            break;

          case MainMenuItemKey.EXPORT_CURRENT_SHEET_XLSX:
            try {
                eventManager.emitExportStart("xlsx", !1), (null == tableInstance ? void 0 : tableInstance.exportToExcel) ? (tableInstance.exportToExcel(), 
                eventManager.emitExportCompleted("xlsx", !1, 1)) : eventManager.emitExportError("xlsx", !1, "TableExportPlugin not configured");
            } catch (error) {
                const errorMessage = error instanceof Error ? error.message : String(error);
                eventManager.emitExportError("xlsx", !1, errorMessage);
            }
            break;

          case MainMenuItemKey.EXPORT_ALL_SHEETS_XLSX:
            try {
                if (eventManager.emitExportStart("xlsx", !0), this.sheet.exportAllSheetsToExcel) {
                    this.sheet.exportAllSheetsToExcel();
                    const sheetCount = this.sheet.getSheetCount();
                    eventManager.emitExportCompleted("xlsx", !0, sheetCount);
                } else eventManager.emitExportError("xlsx", !0, "Export all sheets method not available");
            } catch (error) {
                const errorMessage = error instanceof Error ? error.message : String(error);
                eventManager.emitExportError("xlsx", !0, errorMessage);
            }
        }
    }
    release() {
        this.clickOutsideHandler && (document.removeEventListener("click", this.clickOutsideHandler), 
        this.clickOutsideHandler = null);
    }
    updateMainMenu(mainMenu) {
        this.release(), this.createMainMenu();
    }
}
//# sourceMappingURL=menu-manager.js.map