var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { addClass } from "@matdata/yasgui-utils";
import { getRandomId } from "./";
import { cloneDeep } from "lodash-es";
import "./TabContextMenu.scss";
export default class TabContextMenu {
    constructor(yasgui, rootEl) {
        this.handleContextClick = (event) => {
            if (event.button !== 2) {
                this.closeConfigMenu();
            }
            else if (event.target !== this.contextEl) {
                this.closeConfigMenu();
            }
            else {
                event.stopImmediatePropagation();
            }
        };
        this.closeConfigMenu = () => {
            this.tabRef = undefined;
            if (this.contextEl)
                this.contextEl.remove();
        };
        this.yasgui = yasgui;
        this.rootEl = rootEl;
        document.addEventListener("click", this.handleContextClick);
        document.addEventListener("keyup", this.closeConfigMenu);
    }
    getMenuItemEl(text) {
        const item = document.createElement("li");
        addClass(item, "context-menu-item");
        item.addEventListener("contextmenu", (event) => {
            event.stopPropagation();
        });
        if (text !== undefined)
            item.innerText = text;
        return item;
    }
    draw(rootEl) {
        this.contextEl = document.createElement("div");
        const dropDownList = document.createElement("ul");
        addClass(dropDownList, "context-menu-list");
        this.newTabEl = this.getMenuItemEl("New Tab");
        this.newTabEl.onclick = () => this.yasgui.addTab(true);
        this.renameTabEl = this.getMenuItemEl("Rename Tab");
        this.copyTabEl = this.getMenuItemEl("Copy Tab");
        this.saveManagedQueryEl = this.getMenuItemEl("Save as managed query");
        this.closeTabEl = this.getMenuItemEl("Close Tab");
        this.closeOtherTabsEl = this.getMenuItemEl("Close other tabs");
        this.reOpenOldTab = this.getMenuItemEl("Undo close Tab");
        dropDownList.appendChild(this.newTabEl);
        dropDownList.appendChild(this.renameTabEl);
        dropDownList.appendChild(this.copyTabEl);
        dropDownList.appendChild(this.saveManagedQueryEl);
        dropDownList.appendChild(document.createElement("hr"));
        dropDownList.appendChild(this.closeTabEl);
        dropDownList.appendChild(this.closeOtherTabsEl);
        dropDownList.appendChild(this.reOpenOldTab);
        this.contextEl.appendChild(dropDownList);
        addClass(this.contextEl, "yasgui", "context-menu");
        rootEl.appendChild(this.contextEl);
    }
    redraw() {
        var _a;
        if (this.contextEl && ((_a = this.tabRef) === null || _a === void 0 ? void 0 : _a.tabEl)) {
            const bounding = this.tabRef.tabEl.getBoundingClientRect();
            this.contextEl.style.top = `${window.pageYOffset + bounding.bottom}px`;
        }
    }
    openConfigMenu(currentTabId, currentTabEl, event) {
        if (!currentTabEl.tabEl)
            return;
        this.draw(this.rootEl);
        this.tabRef = currentTabEl;
        const tab = this.yasgui.getTab(currentTabId);
        const bounding = currentTabEl.tabEl.getBoundingClientRect();
        this.contextEl.style.left = `${window.pageXOffset + bounding.left}px`;
        this.contextEl.style.top = `${window.pageYOffset + bounding.bottom}px`;
        event.stopPropagation();
        this.renameTabEl.onclick = () => currentTabEl.startRename();
        this.copyTabEl.onclick = () => {
            if (!tab)
                return;
            const config = cloneDeep(tab.getPersistedJson());
            config.id = getRandomId();
            this.yasgui.addTab(true, config);
        };
        this.saveManagedQueryEl.onclick = () => __awaiter(this, void 0, void 0, function* () {
            if (!tab)
                return;
            yield tab.saveManagedQueryOrSaveAsManagedQuery();
            this.closeConfigMenu();
        });
        this.closeTabEl.onclick = () => tab === null || tab === void 0 ? void 0 : tab.close();
        if (Object.keys(this.yasgui._tabs).length === 1) {
            addClass(this.closeOtherTabsEl, "disabled");
        }
        else {
            this.closeOtherTabsEl.onclick = () => {
                for (const tabId of Object.keys(this.yasgui._tabs)) {
                    if (tabId !== currentTabId)
                        this.yasgui.getTab(tabId).close();
                }
            };
        }
        if (this.yasgui.persistentConfig && this.yasgui.persistentConfig.hasLastClosedTab()) {
            this.reOpenOldTab.onclick = () => this.yasgui.restoreLastTab();
        }
        else {
            addClass(this.reOpenOldTab, "disabled");
        }
    }
    static get(yasgui, rootEl) {
        const instance = new TabContextMenu(yasgui, rootEl);
        return instance;
    }
    unregisterEventListeners() {
        document.removeEventListener("click", this.handleContextClick);
        document.removeEventListener("keyup", this.closeConfigMenu);
    }
    destroy() {
        this.unregisterEventListeners();
    }
}
//# sourceMappingURL=TabContextMenu.js.map