import { showSnackbar } from "../tools/ui/snackbar";

import { checkTabTitle } from "../tools";

export class SheetTabEventHandler {
    constructor(vTableSheet) {
        this.clickOutsideHandler = event => {
            var _a, _b;
            const menuContainer = null === (_a = this.vTableSheet.getSheetTabElement()) || void 0 === _a ? void 0 : _a.querySelector(".vtable-sheet-menu-list"), menuButton = null === (_b = this.vTableSheet.getSheetTabElement()) || void 0 === _b ? void 0 : _b.querySelector(".vtable-sheet-menu-button");
            menuContainer && menuButton && !menuContainer.contains(event.target) && !menuButton.contains(event.target) && (menuContainer.classList.remove("active"), 
            this.removeClickOutsideListener());
        }, this.vTableSheet = vTableSheet;
    }
    handleSheetTabDblClick(sheetKey, originalTitle) {
        const targetTab = this.getSheetTabElementByKey(sheetKey);
        if (!targetTab) return;
        targetTab.setAttribute("contenteditable", "true"), targetTab.setAttribute("spellcheck", "false"), 
        targetTab.classList.add("editing");
        const range = document.createRange();
        range.selectNodeContents(targetTab);
        const selection = window.getSelection();
        selection.removeAllRanges(), selection.addRange(range);
        const onBlur = () => {
            finishInput(!0);
        }, onKeyDown = e => {
            e.stopPropagation(), "Enter" === e.key ? (e.preventDefault(), finishInput(!0)) : "Escape" === e.key && finishInput(!1);
        }, finishInput = commit => {
            var _a;
            targetTab.removeEventListener("blur", onBlur), targetTab.removeEventListener("keydown", onKeyDown), 
            targetTab.classList.remove("editing"), targetTab.setAttribute("contenteditable", "false");
            const newTitle = null === (_a = targetTab.textContent) || void 0 === _a ? void 0 : _a.trim();
            commit && newTitle !== originalTitle && this.renameSheet(sheetKey, newTitle) || (targetTab.textContent = originalTitle);
        };
        targetTab.addEventListener("blur", onBlur), targetTab.addEventListener("keydown", onKeyDown);
    }
    renameSheet(sheetKey, newTitle) {
        var _a;
        if (!this.vTableSheet.getSheetManager().getSheet(sheetKey)) return !1;
        const error = checkTabTitle(newTitle);
        if (error) return showSnackbar(error, 1300), !1;
        return this.vTableSheet.getSheetManager().getAllSheets().find((s => s.sheetKey !== sheetKey && s.sheetTitle.toLowerCase() === newTitle.toLowerCase())) ? (showSnackbar("工作表名称已存在，请重新输入", 1300), 
        !1) : (this.vTableSheet.getSheetManager().renameSheet(sheetKey, newTitle), null === (_a = this.vTableSheet.workSheetInstances.get(sheetKey)) || void 0 === _a || _a.setTitle(newTitle), 
        this.vTableSheet.getFormulaManager().updateSheetTitle(sheetKey, newTitle), this.vTableSheet.updateSheetTabs(), 
        this.vTableSheet.updateSheetMenu(), !0);
    }
    getSheetTabElementByKey(sheetKey) {
        var _a;
        const tabsContainer = null === (_a = this.vTableSheet.getSheetTabElement()) || void 0 === _a ? void 0 : _a.querySelector(".vtable-sheet-tabs-container");
        return null == tabsContainer ? void 0 : tabsContainer.querySelector(`.vtable-sheet-tab[data-key="${sheetKey}"]`);
    }
    activeSheetTab() {
        var _a;
        const tabs = null === (_a = this.vTableSheet.getSheetTabElement()) || void 0 === _a ? void 0 : _a.querySelectorAll(".vtable-sheet-tab");
        let activeTab = null;
        tabs.forEach((tab => {
            var _a;
            tab.classList.remove("active"), tab.dataset.key === (null === (_a = this.vTableSheet.getActiveSheet()) || void 0 === _a ? void 0 : _a.getKey()) && (tab.classList.add("active"), 
            activeTab = tab);
        })), setTimeout((() => {
            activeTab && activeTab.scrollIntoView({
                behavior: "smooth",
                block: "nearest"
            });
        }), 100);
    }
    updateFadeEffects(tabsContainer, fadeLeft, fadeRight) {
        tabsContainer.scrollLeft > 10 ? fadeLeft.style.display = "block" : fadeLeft.style.display = "none";
        const maxScroll = tabsContainer.scrollWidth - tabsContainer.clientWidth;
        tabsContainer.scrollLeft < maxScroll - 10 ? fadeRight.style.display = "block" : fadeRight.style.display = "none";
    }
    scrollSheetTabs(direction, tabsContainer) {
        const currentScroll = tabsContainer.scrollLeft;
        "left" === direction ? tabsContainer.scrollTo({
            left: Math.max(0, currentScroll - 200),
            behavior: "smooth"
        }) : tabsContainer.scrollTo({
            left: currentScroll + 200,
            behavior: "smooth"
        });
    }
    scrollTabIntoView(tab, container) {
        const tabRect = tab.getBoundingClientRect(), containerRect = container.getBoundingClientRect();
        tabRect.left < containerRect.left ? container.scrollLeft += tabRect.left - containerRect.left - 10 : tabRect.right > containerRect.right && (container.scrollLeft += tabRect.right - containerRect.right + 10);
    }
    toggleSheetMenu(event) {
        var _a;
        const menuContainer = null === (_a = this.vTableSheet.getSheetTabElement()) || void 0 === _a ? void 0 : _a.querySelector(".vtable-sheet-menu-list");
        menuContainer.classList.toggle("active"), menuContainer.classList.contains("active") ? this.addClickOutsideListener() : this.removeClickOutsideListener();
    }
    addClickOutsideListener() {
        document.addEventListener("click", this.clickOutsideHandler);
    }
    removeClickOutsideListener() {
        document.removeEventListener("click", this.clickOutsideHandler);
    }
    updateSheetMenu() {
        var _a;
        const menuContainer = null === (_a = this.vTableSheet.getSheetTabElement()) || void 0 === _a ? void 0 : _a.querySelector(".vtable-sheet-menu-list");
        menuContainer.innerHTML = "";
        this.vTableSheet.getSheetManager().getAllSheets().forEach((sheet => {
            const li = document.createElement("li");
            li.className = "vtable-sheet-menu-item", li.dataset.key = sheet.sheetKey;
            const title = document.createElement("span");
            title.className = "vtable-sheet-menu-item-title", title.innerText = sheet.sheetTitle, 
            li.appendChild(title);
            const div = document.createElement("div");
            div.className = "vtable-sheet-menu-delete-button", div.innerHTML = '<svg class="x-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M18 6L6 18" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M6 6L18 18" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>', 
            div.addEventListener("click", (e => {
                this.vTableSheet.removeSheet(sheet.sheetKey), e.stopPropagation();
            })), li.addEventListener("click", (() => this.vTableSheet.activateSheet(sheet.sheetKey))), 
            li.appendChild(div), menuContainer.appendChild(li);
        })), this.activeSheetMenuItem(), setTimeout((() => {
            const activeItem = menuContainer.querySelector(".vtable-sheet-main-menu-item.active");
            activeItem && activeItem.scrollIntoView({
                behavior: "smooth",
                block: "nearest"
            });
        }), 100);
    }
    activeSheetMenuItem() {
        var _a, _b;
        if (!1 === (null === (_a = this.vTableSheet.getOptions()) || void 0 === _a ? void 0 : _a.showSheetTab)) return;
        const menuItems = null === (_b = this.vTableSheet.getSheetTabElement()) || void 0 === _b ? void 0 : _b.querySelectorAll(".vtable-sheet-main-menu-item");
        let activeItem = null;
        menuItems.forEach((item => {
            var _a;
            item.classList.remove("active"), item.dataset.key === (null === (_a = this.vTableSheet.getActiveSheet()) || void 0 === _a ? void 0 : _a.getKey()) && (item.classList.add("active"), 
            activeItem = item);
        })), setTimeout((() => {
            activeItem && activeItem.scrollIntoView({
                behavior: "smooth",
                block: "nearest"
            });
        }), 100);
    }
}
//# sourceMappingURL=sheet-tab-event-handler.js.map