import { TABLE_EVENT_TYPE } from "@visactor/vtable";

import { MenuManager } from "./contextmenu/menu-manager";

import { MenuHandler } from "./contextmenu/handle-menu-helper";

import { DEFAULT_BODY_MENU_ITEMS, DEFAULT_COLUMN_SERIES_MENU_ITEMS, DEFAULT_CORNER_SERIES_MENU_ITEMS, DEFAULT_HEADER_MENU_ITEMS, DEFAULT_ROW_SERIES_MENU_ITEMS, MenuKey } from "./contextmenu/types";

export class ContextMenuPlugin {
    constructor(pluginOptions = {}) {
        var _a;
        this.id = "context-menu", this.name = "Context Menu", this.runTime = [ TABLE_EVENT_TYPE.CONTEXTMENU_CELL, TABLE_EVENT_TYPE.PLUGIN_EVENT ], 
        this.handleContextMenuCell = (eventArgs, table) => {
            const {col: col, row: row} = eventArgs, mouseX = eventArgs.event.clientX, mouseY = eventArgs.event.clientY, isSeriesNumberCol = table.isSeriesNumber(col, row);
            let menuItems = [];
            if (menuItems = table.isHeader(col, row) ? isSeriesNumberCol ? this.pluginOptions.columnSeriesNumberMenuItems || [] : this.pluginOptions.headerCellMenuItems || [] : isSeriesNumberCol ? this.pluginOptions.rowSeriesNumberMenuItems || [] : this.pluginOptions.bodyCellMenuItems || [], 
            menuItems.length > 0) {
                const cellRange = table.getCellRange(col, row);
                menuItems = cellRange.start.col !== cellRange.end.col || cellRange.start.row !== cellRange.end.row ? menuItems.filter((item => "string" == typeof item || "merge_cells" !== item.menuKey)) : menuItems.filter((item => "string" == typeof item || "unmerge_cells" !== item.menuKey)), 
                this.pluginOptions.beforeShowAdjustMenuItems && (menuItems = this.pluginOptions.beforeShowAdjustMenuItems(menuItems, table, col, row)), 
                this.showContextMenu(menuItems, mouseX, mouseY, col, row);
            }
        }, this.handlePluginEvent = (eventArgs, table) => {
            const {eventType: eventType, rowIndex: rowIndex, colIndex: colIndex, isCorner: isCorner} = eventArgs.pluginEventInfo, fireEventFromPlugin = eventArgs.plugin, mouseX = eventArgs.event.clientX, mouseY = eventArgs.event.clientY;
            if ("table-series-number" === fireEventFromPlugin.id && "rightclick" === eventType) {
                let menuItems = [];
                isCorner ? menuItems = this.pluginOptions.cornerSeriesNumberMenuItems || [] : void 0 !== rowIndex ? menuItems = this.pluginOptions.rowSeriesNumberMenuItems || [] : void 0 !== colIndex && (menuItems = this.pluginOptions.columnSeriesNumberMenuItems || []), 
                this.pluginOptions.beforeShowAdjustMenuItems && (menuItems = this.pluginOptions.beforeShowAdjustMenuItems(menuItems, table, colIndex, rowIndex)), 
                this.showContextMenu(menuItems, mouseX, mouseY, colIndex, rowIndex);
            }
        }, this.handleMenuClickCallback = (args, table) => {
            "function" == typeof this.pluginOptions.menuClickCallback ? this.pluginOptions.menuClickCallback(args, table) : this.handleMenuClick(args, table);
        }, this.id = null !== (_a = pluginOptions.id) && void 0 !== _a ? _a : this.id, this.pluginOptions = pluginOptions, 
        this.menuManager = new MenuManager, this.menuHandler = new MenuHandler, this.initDefaultMenuItems();
    }
    initDefaultMenuItems() {
        this.pluginOptions.columnSeriesNumberMenuItems || (this.pluginOptions.columnSeriesNumberMenuItems = DEFAULT_COLUMN_SERIES_MENU_ITEMS), 
        this.pluginOptions.rowSeriesNumberMenuItems || (this.pluginOptions.rowSeriesNumberMenuItems = DEFAULT_ROW_SERIES_MENU_ITEMS), 
        this.pluginOptions.cornerSeriesNumberMenuItems || (this.pluginOptions.cornerSeriesNumberMenuItems = DEFAULT_CORNER_SERIES_MENU_ITEMS), 
        this.pluginOptions.headerCellMenuItems || (this.pluginOptions.headerCellMenuItems = DEFAULT_HEADER_MENU_ITEMS), 
        this.pluginOptions.bodyCellMenuItems || (this.pluginOptions.bodyCellMenuItems = DEFAULT_BODY_MENU_ITEMS);
    }
    run(...args) {
        const eventArgs = args[0], runTime = args[1], table = args[2];
        this.table = table, eventArgs.event.preventDefault(), runTime === TABLE_EVENT_TYPE.CONTEXTMENU_CELL ? this.handleContextMenuCell(eventArgs, table) : runTime === TABLE_EVENT_TYPE.PLUGIN_EVENT && this.handlePluginEvent(eventArgs, table);
    }
    showContextMenu(menuItems, x, y, col, row) {
        this.menuManager.showMenu(menuItems, x, y, {
            rowIndex: row,
            colIndex: col
        }, this.table), this.menuManager.setClickCallback(this.handleMenuClickCallback);
    }
    handleMenuClick(args, table) {
        const {menuKey: menuKey, rowIndex: rowIndex, colIndex: colIndex, inputValue: inputValue = 1} = args;
        if ("object" == typeof this.pluginOptions.menuClickCallback && this.pluginOptions.menuClickCallback[menuKey]) this.pluginOptions.menuClickCallback[menuKey](args, table); else switch (menuKey) {
          case MenuKey.COPY:
            this.menuHandler.handleCopy(table);
            break;

          case MenuKey.CUT:
            this.menuHandler.handleCut(table);
            break;

          case MenuKey.PASTE:
            this.menuHandler.handlePaste(table);
            break;

          case MenuKey.INSERT_ROW_ABOVE:
            this.menuHandler.handleInsertRowAbove(table, rowIndex, inputValue);
            break;

          case MenuKey.INSERT_ROW_BELOW:
            this.menuHandler.handleInsertRowBelow(table, rowIndex, inputValue);
            break;

          case MenuKey.INSERT_COLUMN_LEFT:
            this.menuHandler.handleInsertColumnLeft(table, colIndex, inputValue);
            break;

          case MenuKey.INSERT_COLUMN_RIGHT:
            this.menuHandler.handleInsertColumnRight(table, colIndex, inputValue);
            break;

          case MenuKey.DELETE_ROW:
            this.menuHandler.handleDeleteRow(table);
            break;

          case MenuKey.DELETE_COLUMN:
            this.menuHandler.handleDeleteColumn(table, colIndex);
            break;

          case MenuKey.HIDE_COLUMN:
            this.menuHandler.handleHideColumn(table, colIndex);
            break;

          case MenuKey.SORT:
            this.menuHandler.handleSort(table, colIndex);
            break;

          case MenuKey.MERGE_CELLS:
            this.menuHandler.handleMergeCells(table);
            break;

          case MenuKey.UNMERGE_CELLS:
            this.menuHandler.handleUnmergeCells(table);
            break;

          case MenuKey.FREEZE_TO_THIS_ROW:
            this.menuHandler.handleFreezeToRow(table, rowIndex);
            break;

          case MenuKey.FREEZE_TO_THIS_COLUMN:
            this.menuHandler.handleFreezeToColumn(table, colIndex);
            break;

          case MenuKey.FREEZE_TO_THIS_ROW_AND_COLUMN:
            this.menuHandler.handleFreezeToRowAndColumn(table, rowIndex, colIndex);
            break;

          case MenuKey.UNFREEZE:
            this.menuHandler.handleUnfreeze(table);
        }
    }
    release() {
        this.menuManager && (this.menuManager.release(), this.menuManager = null), this.menuHandler && (this.menuHandler.release(), 
        this.menuHandler = null);
    }
}
//# sourceMappingURL=context-menu.js.map