"use strict";

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

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

class ColumnSeriesPlugin {
    constructor(pluginOptions) {
        var _a;
        this.id = "column-series", this.name = "Column Series", this.runTime = [ vtable_1.TABLE_EVENT_TYPE.BEFORE_INIT, vtable_1.TABLE_EVENT_TYPE.BEFORE_KEYDOWN ], 
        this.columns = [], this.id = null !== (_a = pluginOptions.id) && void 0 !== _a ? _a : this.id, 
        this.pluginOptions = Object.assign({
            columnCount: 100
        }, pluginOptions);
    }
    run(...args) {
        var _a;
        if (args[1] === vtable_1.TABLE_EVENT_TYPE.BEFORE_INIT) {
            const eventArgs = args[0], table = args[2];
            this.table = table;
            const options = eventArgs.options;
            this.columns = this.generateColumns(this.pluginOptions.columnCount), options.columns = this.columns;
        } else if (args[1] === vtable_1.TABLE_EVENT_TYPE.BEFORE_KEYDOWN) {
            const e = args[0].event;
            (null === (_a = this.pluginOptions.autoExtendColumnTriggerKeys) || void 0 === _a ? void 0 : _a.includes(e.key)) && this.table.stateManager.select.cellPos.col === this.table.colCount - 1 && this.table.addColumns([ this.generateColumn(this.table.colCount - 1) ]);
        }
    }
    generateColumns(columnCount) {
        const columnFields = [];
        for (let i = 0; i < columnCount; i++) columnFields.push(this.generateColumn(i));
        return columnFields;
    }
    generateColumn(index) {
        return {
            title: this.pluginOptions.generateColumnTitle ? this.pluginOptions.generateColumnTitle(index) : this.generateColumnField(index)
        };
    }
    generateColumnField(index) {
        if (index < 26) return String.fromCharCode(65 + index);
        const title = [];
        for (index++; index > 0; ) index--, title.unshift(String.fromCharCode(65 + index % 26)), 
        index = Math.floor(index / 26);
        return title.join("");
    }
    resetColumnCount(columnCount) {
        this.pluginOptions.columnCount = columnCount, this.columns = this.generateColumns(columnCount), 
        this.table.updateColumns(this.columns);
    }
}

exports.ColumnSeriesPlugin = ColumnSeriesPlugin;
//# sourceMappingURL=column-series.js.map