"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.AddRowColumnPlugin = void 0;

const vtable_1 = require("@visactor/vtable");

class AddRowColumnPlugin {
    constructor(pluginOptions = {
        addColumnEnable: !0,
        addRowEnable: !0
    }) {
        var _a, _b, _c;
        this.id = "add-row-column", this.name = "Add Row Column", this.runTime = [ vtable_1.TABLE_EVENT_TYPE.MOUSEENTER_CELL, vtable_1.TABLE_EVENT_TYPE.MOUSELEAVE_CELL, vtable_1.TABLE_EVENT_TYPE.MOUSELEAVE_TABLE ], 
        this.id = null !== (_a = pluginOptions.id) && void 0 !== _a ? _a : this.id, this.pluginOptions = pluginOptions, 
        this.pluginOptions.addColumnEnable = null === (_b = this.pluginOptions.addColumnEnable) || void 0 === _b || _b, 
        this.pluginOptions.addRowEnable = null === (_c = this.pluginOptions.addRowEnable) || void 0 === _c || _c, 
        this.pluginOptions.addColumnEnable && (this.initAddColumnDomElement(), this.bindEventForAddColumn()), 
        this.pluginOptions.addRowEnable && (this.initAddRowDomElement(), this.bindEventForAddRow());
    }
    run(...args) {
        const eventArgs = args[0], runTime = args[1], table = args[2];
        if (this.table = table, runTime === vtable_1.TABLE_EVENT_TYPE.MOUSEENTER_CELL) {
            clearTimeout(this.hideAllTimeoutId_addColumn), clearTimeout(this.hideAllTimeoutId_addRow);
            const canvasBounds = table.canvas.getBoundingClientRect(), cell = table.getCellAtRelativePosition(eventArgs.event.clientX - canvasBounds.left, eventArgs.event.clientY - canvasBounds.top);
            this.hoverCell = cell;
            const cellRect = table.getCellRelativeRect(cell.col, cell.row);
            if (this.pluginOptions.addColumnEnable) {
                const isRowSerierNumberCol = table.isSeriesNumber(cell.col, 0);
                this.showDotForAddColumn(canvasBounds.top - 6, cellRect.left + canvasBounds.left, cellRect.right + canvasBounds.left, !isRowSerierNumberCol);
            }
            if (this.pluginOptions.addRowEnable) {
                const isHeader = table.isHeader(cell.col, cell.row);
                this.showDotForAddRow(cellRect.top + canvasBounds.top, canvasBounds.left - 6, cellRect.bottom + canvasBounds.top, !isHeader, !isHeader);
            }
        } else runTime === vtable_1.TABLE_EVENT_TYPE.MOUSELEAVE_CELL || runTime === vtable_1.TABLE_EVENT_TYPE.MOUSELEAVE_TABLE && (this.pluginOptions.addColumnEnable && this.delayHideAllForAddColumn(), 
        this.pluginOptions.addRowEnable && this.delayHideAllForAddRow());
    }
    initAddColumnDomElement() {
        this.leftDotForAddColumn = document.createElement("div"), this.leftDotForAddColumn.style.width = "6px", 
        this.leftDotForAddColumn.style.height = "6px", this.leftDotForAddColumn.style.backgroundColor = "#4A90E2", 
        this.leftDotForAddColumn.style.position = "absolute", this.leftDotForAddColumn.style.cursor = "pointer", 
        this.leftDotForAddColumn.style.zIndex = "1000", this.leftDotForAddColumn.style.borderRadius = "50%", 
        this.leftDotForAddColumn.style.border = "1px solid white", this.leftDotForAddColumn.style.boxShadow = "0 1px 3px rgba(0,0,0,0.2)", 
        document.body.appendChild(this.leftDotForAddColumn), this.rightDotForAddColumn = document.createElement("div"), 
        this.rightDotForAddColumn.style.width = "6px", this.rightDotForAddColumn.style.height = "6px", 
        this.rightDotForAddColumn.style.backgroundColor = "#4A90E2", this.rightDotForAddColumn.style.position = "absolute", 
        this.rightDotForAddColumn.style.cursor = "pointer", this.rightDotForAddColumn.style.zIndex = "1000", 
        this.rightDotForAddColumn.style.borderRadius = "50%", this.rightDotForAddColumn.style.border = "1px solid white", 
        this.rightDotForAddColumn.style.boxShadow = "0 1px 3px rgba(0,0,0,0.2)", document.body.appendChild(this.rightDotForAddColumn), 
        this.addIconForAddColumn = document.createElement("div"), this.addIconForAddColumn.style.width = "18px", 
        this.addIconForAddColumn.style.height = "18px", this.addIconForAddColumn.style.backgroundColor = "#4A90E2", 
        this.addIconForAddColumn.style.position = "absolute", this.addIconForAddColumn.style.zIndex = "1001", 
        this.addIconForAddColumn.style.display = "none", this.addIconForAddColumn.style.borderRadius = "50%", 
        this.addIconForAddColumn.style.boxShadow = "0 2px 5px rgba(0,0,0,0.2)", this.addIconForAddColumn.style.justifyContent = "center", 
        this.addIconForAddColumn.style.alignItems = "center", this.addIconForAddColumn.style.border = "1px solid white", 
        document.body.appendChild(this.addIconForAddColumn);
        const addIconText = document.createElement("div");
        addIconText.textContent = "+", addIconText.style.color = "white", addIconText.style.fontSize = "18px", 
        addIconText.style.fontWeight = "bold", addIconText.style.lineHeight = "15px", addIconText.style.userSelect = "none", 
        addIconText.style.cursor = "pointer", addIconText.style.verticalAlign = "top", addIconText.style.textAlign = "center", 
        this.addIconForAddColumn.appendChild(addIconText), this.addLineForAddColumn = document.createElement("div"), 
        this.addLineForAddColumn.style.width = "2px", this.addLineForAddColumn.style.height = "10px", 
        this.addLineForAddColumn.style.backgroundColor = "#4A90E2", this.addLineForAddColumn.style.position = "absolute", 
        this.addLineForAddColumn.style.zIndex = "1001", this.addLineForAddColumn.style.display = "none", 
        document.body.appendChild(this.addLineForAddColumn);
    }
    bindEventForAddColumn() {
        this.leftDotForAddColumn.addEventListener("mouseenter", (() => {
            clearTimeout(this.hideAllTimeoutId_addColumn), this.addIconForAddColumn.style.display = "block";
            const dotWidth = this.leftDotForAddColumn.offsetWidth, dotHeight = this.leftDotForAddColumn.offsetHeight;
            this.showAddIconForAddColumn(this.leftDotForAddColumn.offsetLeft + dotWidth / 2, this.leftDotForAddColumn.offsetTop + dotHeight / 2, !0), 
            this.showSplitLineForAddColumn(this.leftDotForAddColumn.offsetLeft + dotWidth / 2 - 1, this.leftDotForAddColumn.offsetTop + dotHeight / 2 + 2, this.table.getDrawRange().height);
        })), this.rightDotForAddColumn.addEventListener("mouseenter", (() => {
            clearTimeout(this.hideAllTimeoutId_addColumn), this.addIconForAddColumn.style.display = "block";
            const dotWidth = this.rightDotForAddColumn.offsetWidth, dotHeight = this.rightDotForAddColumn.offsetHeight;
            this.showAddIconForAddColumn(this.rightDotForAddColumn.offsetLeft + dotWidth / 2, this.rightDotForAddColumn.offsetTop + dotHeight / 2, !1), 
            this.showSplitLineForAddColumn(this.rightDotForAddColumn.offsetLeft + dotWidth / 2 - 1, this.rightDotForAddColumn.offsetTop + dotHeight / 2 + 2, this.table.getDrawRange().height);
        })), this.addIconForAddColumn.addEventListener("mouseleave", (() => {
            this.addIconForAddColumn.style.display = "none", this.addLineForAddColumn.style.display = "none", 
            this.delayHideAllForAddColumn();
        })), this.addIconForAddColumn.addEventListener("click", (e => {
            const isLeft = "left" === this.addIconForAddColumn.dataset.addIconType, col = (this.table.options.columns, 
            this.hoverCell.col), addColIndex = isLeft ? col : col + 1;
            this.pluginOptions.addColumnCallback ? this.pluginOptions.addColumnCallback(addColIndex, this.table) : this.table.addColumns([ {
                field: addColIndex,
                title: `New Column ${col}`,
                width: 100
            } ], addColIndex, !0), this.delayHideAllForAddColumn(0);
        }));
    }
    showDotForAddColumn(top, left, right, isShowLeft = !0, isShowRight = !0) {
        const dotWidth = this.leftDotForAddColumn.offsetWidth, dotHeight = this.leftDotForAddColumn.offsetHeight;
        this.leftDotForAddColumn.style.left = left - dotWidth / 2 + "px", this.leftDotForAddColumn.style.top = top - dotHeight / 2 + "px", 
        this.rightDotForAddColumn.style.left = right - dotWidth / 2 + "px", this.rightDotForAddColumn.style.top = top - dotHeight / 2 + "px", 
        this.leftDotForAddColumn.style.display = isShowLeft ? "block" : "none", this.rightDotForAddColumn.style.display = isShowRight ? "block" : "none";
    }
    showAddIconForAddColumn(left, top, isLeft) {
        const iconWidth = this.addIconForAddColumn.offsetWidth, iconHeight = this.addIconForAddColumn.offsetHeight, dotHeight = this.leftDotForAddColumn.offsetHeight;
        this.addIconForAddColumn.style.left = left - iconWidth / 2 + "px", this.addIconForAddColumn.style.top = top - iconHeight / 2 - dotHeight / 2 + "px", 
        this.addIconForAddColumn.dataset.addIconType = isLeft ? "left" : "right";
    }
    showSplitLineForAddColumn(left, top, height) {
        this.addLineForAddColumn.style.left = `${left}px`, this.addLineForAddColumn.style.top = `${top}px`, 
        this.addLineForAddColumn.style.height = `${height}px`, this.addLineForAddColumn.style.display = "block";
    }
    delayHideAllForAddColumn(delay = 1e3) {
        this.hideAllTimeoutId_addColumn = setTimeout((() => {
            this.addIconForAddColumn.style.display = "none", this.addLineForAddColumn.style.display = "none", 
            this.leftDotForAddColumn.style.display = "none", this.rightDotForAddColumn.style.display = "none";
        }), delay);
    }
    initAddRowDomElement() {
        this.topDotForAddRow = document.createElement("div"), this.topDotForAddRow.style.width = "6px", 
        this.topDotForAddRow.style.height = "6px", this.topDotForAddRow.style.backgroundColor = "#4A90E2", 
        this.topDotForAddRow.style.position = "absolute", this.topDotForAddRow.style.cursor = "pointer", 
        this.topDotForAddRow.style.zIndex = "1000", this.topDotForAddRow.style.borderRadius = "50%", 
        this.topDotForAddRow.style.border = "1px solid white", this.topDotForAddRow.style.boxShadow = "0 1px 3px rgba(0,0,0,0.2)", 
        document.body.appendChild(this.topDotForAddRow), this.bottomDotForAddRow = document.createElement("div"), 
        this.bottomDotForAddRow.style.width = "6px", this.bottomDotForAddRow.style.height = "6px", 
        this.bottomDotForAddRow.style.backgroundColor = "#4A90E2", this.bottomDotForAddRow.style.position = "absolute", 
        this.bottomDotForAddRow.style.cursor = "pointer", this.bottomDotForAddRow.style.zIndex = "1000", 
        this.bottomDotForAddRow.style.borderRadius = "50%", this.bottomDotForAddRow.style.border = "1px solid white", 
        this.bottomDotForAddRow.style.boxShadow = "0 1px 3px rgba(0,0,0,0.2)", document.body.appendChild(this.bottomDotForAddRow), 
        this.addIconForAddRow = document.createElement("div"), this.addIconForAddRow.style.width = "18px", 
        this.addIconForAddRow.style.height = "18px", this.addIconForAddRow.style.backgroundColor = "#4A90E2", 
        this.addIconForAddRow.style.position = "absolute", this.addIconForAddRow.style.zIndex = "1001", 
        this.addIconForAddRow.style.display = "none", this.addIconForAddRow.style.borderRadius = "50%", 
        this.addIconForAddRow.style.boxShadow = "0 2px 5px rgba(0,0,0,0.2)", this.addIconForAddRow.style.justifyContent = "center", 
        this.addIconForAddRow.style.alignItems = "center", this.addIconForAddRow.style.border = "1px solid white", 
        document.body.appendChild(this.addIconForAddRow);
        const addIconText = document.createElement("div");
        addIconText.textContent = "+", addIconText.style.color = "white", addIconText.style.fontSize = "18px", 
        addIconText.style.fontWeight = "bold", addIconText.style.lineHeight = "15px", addIconText.style.userSelect = "none", 
        addIconText.style.cursor = "pointer", addIconText.style.verticalAlign = "top", addIconText.style.textAlign = "center", 
        this.addIconForAddRow.appendChild(addIconText), this.addLineForAddRow = document.createElement("div"), 
        this.addLineForAddRow.style.width = "10px", this.addLineForAddRow.style.height = "2px", 
        this.addLineForAddRow.style.backgroundColor = "#4A90E2", this.addLineForAddRow.style.position = "absolute", 
        this.addLineForAddRow.style.zIndex = "1001", this.addLineForAddRow.style.display = "none", 
        document.body.appendChild(this.addLineForAddRow);
    }
    bindEventForAddRow() {
        this.topDotForAddRow.addEventListener("mouseenter", (() => {
            clearTimeout(this.hideAllTimeoutId_addRow), this.addIconForAddRow.style.display = "block";
            const dotWidth = this.topDotForAddRow.offsetWidth, dotHeight = this.topDotForAddRow.offsetHeight;
            this.showAddIconForAddRow(this.topDotForAddRow.offsetLeft + dotWidth / 2, this.topDotForAddRow.offsetTop + dotHeight / 2, !0), 
            this.showSplitLineForAddRow(this.topDotForAddRow.offsetLeft + dotWidth + 2, this.topDotForAddRow.offsetTop + dotHeight / 2 - 1, this.table.getDrawRange().width);
        })), this.bottomDotForAddRow.addEventListener("mouseenter", (() => {
            clearTimeout(this.hideAllTimeoutId_addRow), this.addIconForAddRow.style.display = "block";
            const dotWidth = this.bottomDotForAddRow.offsetWidth, dotHeight = this.bottomDotForAddRow.offsetHeight;
            this.showAddIconForAddRow(this.bottomDotForAddRow.offsetLeft + dotWidth / 2, this.bottomDotForAddRow.offsetTop + dotHeight / 2, !1), 
            this.showSplitLineForAddRow(this.bottomDotForAddRow.offsetLeft + dotWidth + 2, this.bottomDotForAddRow.offsetTop + dotHeight / 2 - 1, this.table.getDrawRange().height);
        })), this.addIconForAddRow.addEventListener("mouseleave", (() => {
            this.addIconForAddRow.style.display = "none", this.addLineForAddRow.style.display = "none", 
            this.delayHideAllForAddRow();
        })), this.addIconForAddRow.addEventListener("click", (e => {
            const isTop = "top" === this.addIconForAddRow.dataset.addIconType, row = this.hoverCell.row, addRowIndex = isTop ? row : row + 1;
            if (this.pluginOptions.addRowCallback) this.pluginOptions.addRowCallback(addRowIndex, this.table); else {
                const recordIndex = this.table.getRecordIndexByCell(0, addRowIndex);
                this.table.addRecord({}, recordIndex);
            }
            this.delayHideAllForAddRow(0);
        }));
    }
    showDotForAddRow(top, left, bottom, isShowTop = !0, isShowBottom = !0) {
        const dotWidth = this.topDotForAddRow.offsetWidth, dotHeight = this.topDotForAddRow.offsetHeight;
        this.topDotForAddRow.style.left = left - dotWidth / 2 + "px", this.topDotForAddRow.style.top = top - dotHeight / 2 + "px", 
        this.bottomDotForAddRow.style.left = left - dotWidth / 2 + "px", this.bottomDotForAddRow.style.top = bottom - dotHeight / 2 + "px", 
        this.topDotForAddRow.style.display = isShowTop ? "block" : "none", this.bottomDotForAddRow.style.display = isShowBottom ? "block" : "none";
    }
    showAddIconForAddRow(left, top, isTop) {
        const iconWidth = this.addIconForAddRow.offsetWidth, iconHeight = this.addIconForAddRow.offsetHeight, dotWidth = this.topDotForAddRow.offsetWidth;
        this.addIconForAddRow.style.left = left - iconWidth / 2 - dotWidth / 2 + "px", this.addIconForAddRow.style.top = top - iconHeight / 2 + "px", 
        this.addIconForAddRow.dataset.addIconType = isTop ? "top" : "bottom";
    }
    showSplitLineForAddRow(left, top, width) {
        this.addLineForAddRow.style.left = `${left}px`, this.addLineForAddRow.style.top = `${top}px`, 
        this.addLineForAddRow.style.width = `${width}px`, this.addLineForAddRow.style.display = "block";
    }
    delayHideAllForAddRow(delay = 1e3) {
        this.hideAllTimeoutId_addRow = setTimeout((() => {
            this.addIconForAddRow.style.display = "none", this.addLineForAddRow.style.display = "none", 
            this.topDotForAddRow.style.display = "none", this.bottomDotForAddRow.style.display = "none";
        }), delay);
    }
    release() {
        this.leftDotForAddColumn.remove(), this.rightDotForAddColumn.remove(), this.addIconForAddColumn.remove(), 
        this.addLineForAddColumn.remove(), this.topDotForAddRow.remove(), this.bottomDotForAddRow.remove(), 
        this.addIconForAddRow.remove(), this.addLineForAddRow.remove();
    }
}

exports.AddRowColumnPlugin = AddRowColumnPlugin;