"use strict";

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

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

class PasteAddRowColumnPlugin {
    constructor(pluginOptions) {
        var _a;
        this.id = "paste-add-row-column", this.name = "Paste Add row", this.runTime = [ vtable_1.TABLE_EVENT_TYPE.INITIALIZED, vtable_1.TABLE_EVENT_TYPE.PASTED_DATA ], 
        this.pluginOptions = pluginOptions, this.id = null !== (_a = null == pluginOptions ? void 0 : pluginOptions.id) && void 0 !== _a ? _a : this.id;
    }
    run(...args) {
        const runtime = args[1], table = args[2];
        this.table = table, runtime === vtable_1.TABLE_EVENT_TYPE.PASTED_DATA && (this.pastedData = args[0], 
        this.handlePaste());
    }
    handlePaste() {
        var _a, _b, _c;
        const {pasteData: pasteData, row: row, col: col} = this.pastedData, rowCount = this.table.rowCount, colCount = this.table.colCount, rowsNeeded = row + pasteData.length - rowCount, colsNeeded = col + ((null === (_a = pasteData[0]) || void 0 === _a ? void 0 : _a.length) || 0) - colCount;
        if (rowsNeeded > 0) for (let i = 0; i < rowsNeeded; i++) (null === (_b = this.pluginOptions) || void 0 === _b ? void 0 : _b.addRowCallback) ? this.pluginOptions.addRowCallback(rowCount + i, this.table) : this.table.addRecord([]);
        if (colsNeeded > 0) for (let i = 0; i < colsNeeded; i++) {
            const newColIndex = colCount + i;
            (null === (_c = this.pluginOptions) || void 0 === _c ? void 0 : _c.addColumnCallback) ? this.pluginOptions.addColumnCallback(newColIndex, this.table) : this.table.addColumns([ {
                field: `field_${newColIndex}`,
                title: `New Column ${newColIndex}`,
                width: 100
            } ]);
        }
        this.table.changeCellValues(col, row, pasteData, !0);
    }
    release() {
        this.table.internalProps.handler.clear();
    }
}

exports.PasteAddRowColumnPlugin = PasteAddRowColumnPlugin;
//# sourceMappingURL=paste-add-row-column.js.map