var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
    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) {
            var value;
            result.done ? resolve(result.value) : (value = result.value, value instanceof P ? value : new P((function(resolve) {
                resolve(value);
            }))).then(fulfilled, rejected);
        }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    }));
}, __rest = this && this.__rest || function(s, e) {
    var t = {};
    for (var p in s) Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0 && (t[p] = s[p]);
    if (null != s && "function" == typeof Object.getOwnPropertySymbols) {
        var i = 0;
        for (p = Object.getOwnPropertySymbols(s); i < p.length; i++) e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]) && (t[p[i]] = s[p[i]]);
    }
    return t;
};

import { FormulaManager } from "../managers/formula-manager";

import SheetManager from "../managers/sheet-manager";

import { WorkSheet } from "../core/WorkSheet";

import * as VTable from "@visactor/vtable";

import { getTablePlugins } from "../core/table-plugins";

import { DomEventManager } from "../event/dom-event-manager";

import { showSnackbar } from "../tools/ui/snackbar";

import SheetTabDragManager from "../managers/tab-drag-manager";

import { FormulaAutocomplete } from "../formula/formula-autocomplete";

import { formulaEditor } from "../formula/formula-editor";

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

import { FormulaUIManager } from "../formula/formula-ui-manager";

import { SheetTabEventHandler } from "./sheet-tab-event-handler";

import { TableEventRelay } from "../event/table-event-relay";

import { SpreadSheetEventManager } from "../event/spreadsheet-event-manager";

import { VTableSheetEventBus } from "../event/vtable-sheet-event-bus";

import { pluginIsChanged } from "../sheet-helper";

import { tableThemeIsChanged } from "@visactor/vtable/es/themes";

VTable.register.editor("formula", formulaEditor);

export default class VTableSheet {
    constructor(container, options) {
        this.activeWorkSheet = null, this.workSheetInstances = new Map, this.formulaAutocomplete = null, 
        this.formulaBarElement = null, this.sheetTabElement = null, this.mainMenuElement = null, 
        this.container = container, this.options = this.mergeDefaultOptions(options), this.eventBus = new VTableSheetEventBus, 
        this.sheetManager = new SheetManager(this.eventBus), this.formulaManager = new FormulaManager(this), 
        this.tableEventRelay = new TableEventRelay(this), this.eventManager = new DomEventManager(this), 
        this.dragManager = new SheetTabDragManager(this), this.menuManager = new MenuManager(this), 
        this.formulaUIManager = new FormulaUIManager(this), this.sheetTabEventHandler = new SheetTabEventHandler(this), 
        this.spreadsheetEventManager = new SpreadSheetEventManager(this), this.initUI(), 
        this.initSheets(), this.resize();
    }
    mergeDefaultOptions(options) {
        return Object.assign({
            showFormulaBar: !0,
            showSheetTab: !0,
            defaultRowHeight: 25,
            defaultColWidth: 100
        }, options);
    }
    initFormulaAutocomplete() {
        if (!this.formulaBarElement) return;
        const formulaInput = this.formulaUIManager.formulaInput;
        formulaInput && (this.formulaAutocomplete = new FormulaAutocomplete(this.rootElement, this), 
        this.formulaAutocomplete.attachTo(formulaInput));
    }
    initUI() {
        var _a;
        this.rootElement = document.createElement("div"), this.rootElement.className = "vtable-sheet-container", 
        this.container.appendChild(this.rootElement);
        const topContainer = document.createElement("div");
        topContainer.className = "vtable-sheet-top-container", this.rootElement.appendChild(topContainer), 
        (null === (_a = this.options.mainMenu) || void 0 === _a ? void 0 : _a.show) && (this.mainMenuElement = this.menuManager.createMainMenu(), 
        topContainer.appendChild(this.mainMenuElement)), this.options.showFormulaBar && (this.formulaBarElement = this.formulaUIManager.createFormulaBar(), 
        topContainer.appendChild(this.formulaBarElement), this.initFormulaAutocomplete()), 
        this.contentElement = document.createElement("div"), this.contentElement.className = "vtable-sheet-content", 
        this.rootElement.appendChild(this.contentElement), this.options.showSheetTab && (this.sheetTabElement = this.createSheetTab(), 
        this.rootElement.appendChild(this.sheetTabElement));
    }
    createSheetTab() {
        const sheetTab = document.createElement("div");
        sheetTab.className = "vtable-sheet-tab-bar";
        const fadeLeft = document.createElement("div");
        fadeLeft.className = "vtable-sheet-fade-left", fadeLeft.style.display = "none", 
        sheetTab.appendChild(fadeLeft);
        const tabsContainer = document.createElement("div");
        tabsContainer.className = "vtable-sheet-tabs-container", tabsContainer.addEventListener("scroll", (() => this.sheetTabEventHandler.updateFadeEffects(tabsContainer, fadeLeft, fadeRight))), 
        sheetTab.appendChild(tabsContainer);
        const insertIndicator = document.createElement("div");
        insertIndicator.className = "vtable-sheet-insert-indicator", insertIndicator.style.display = "none", 
        tabsContainer.appendChild(insertIndicator);
        const fadeRight = document.createElement("div");
        fadeRight.className = "vtable-sheet-fade-right", sheetTab.appendChild(fadeRight);
        const addButton = document.createElement("button");
        addButton.className = "vtable-sheet-add-button", addButton.innerHTML = '<svg viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6z"/></svg>', 
        addButton.title = "添加工作表", addButton.addEventListener("click", (() => this._addNewSheet())), 
        sheetTab.appendChild(addButton);
        const navButtons = document.createElement("div");
        navButtons.className = "vtable-sheet-nav-buttons";
        const leftScrollBtn = document.createElement("button");
        leftScrollBtn.className = "vtable-sheet-scroll-button", leftScrollBtn.innerHTML = '<svg viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"/></svg>', 
        leftScrollBtn.title = "向左滚动", leftScrollBtn.addEventListener("click", (() => this.sheetTabEventHandler.scrollSheetTabs("left", tabsContainer))), 
        navButtons.appendChild(leftScrollBtn);
        const rightScrollBtn = document.createElement("button");
        rightScrollBtn.className = "vtable-sheet-scroll-button", rightScrollBtn.innerHTML = '<svg viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"/></svg>', 
        rightScrollBtn.title = "向右滚动", rightScrollBtn.addEventListener("click", (() => this.sheetTabEventHandler.scrollSheetTabs("right", tabsContainer))), 
        navButtons.appendChild(rightScrollBtn);
        const menuButton = document.createElement("button");
        menuButton.className = "vtable-sheet-menu-button", menuButton.innerHTML = '<svg viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M12 8c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"/></svg>', 
        menuButton.title = "工作表选项", menuButton.addEventListener("click", (e => this.sheetTabEventHandler.toggleSheetMenu(e))), 
        navButtons.appendChild(menuButton);
        const menuContainer = document.createElement("ul");
        return menuContainer.className = "vtable-sheet-menu-list", sheetTab.appendChild(menuContainer), 
        sheetTab.appendChild(navButtons), setTimeout((() => {
            this.sheetTabEventHandler.updateFadeEffects(tabsContainer, fadeLeft, fadeRight);
        }), 100), sheetTab;
    }
    _activeSheetTab() {
        !1 !== this.options.showSheetTab && this.sheetTabEventHandler.activeSheetTab();
    }
    updateSheetTabs(tabsContainer) {
        var _a;
        if (void 0 === tabsContainer && (tabsContainer = null === (_a = this.sheetTabElement) || void 0 === _a ? void 0 : _a.querySelector(".vtable-sheet-tabs-container")), 
        !tabsContainer) return;
        tabsContainer.innerHTML = "";
        this.sheetManager.getAllSheets().forEach(((sheet, index) => {
            tabsContainer.appendChild(this.createSheetTabItem(sheet, index));
        })), this._activeSheetTab();
    }
    createSheetTabItem(sheet, _index) {
        const tab = document.createElement("div");
        return tab.className = "vtable-sheet-tab", tab.dataset.key = sheet.sheetKey, tab.textContent = sheet.sheetTitle, 
        tab.title = sheet.sheetTitle, tab.addEventListener("click", (() => this.activateSheet(sheet.sheetKey))), 
        tab.addEventListener("dblclick", (() => this.sheetTabEventHandler.handleSheetTabDblClick(sheet.sheetKey, sheet.sheetTitle))), 
        tab.addEventListener("mousedown", (e => this.dragManager.handleTabMouseDown(e, sheet.sheetKey))), 
        tab;
    }
    updateSheetMenu() {
        !1 !== this.options.showSheetTab && this.sheetTabEventHandler.updateSheetMenu();
    }
    initSheets() {
        if (this.options.sheets && this.options.sheets.length > 0) {
            this.options.sheets.forEach((sheetDefine => {
                this.sheetManager.addSheet(sheetDefine);
            }));
            let activeSheetKey = "";
            const activeSheet = this.options.sheets.find((sheet => sheet.active));
            activeSheetKey = activeSheet ? activeSheet.sheetKey : this.options.sheets[0].sheetKey, 
            this.activateSheet(activeSheetKey);
        } else this._addNewSheet();
    }
    activateSheet(sheetKey) {
        const previousActiveSheet = this.sheetManager.getActiveSheet(), previousSheetKey = null == previousActiveSheet ? void 0 : previousActiveSheet.sheetKey, previousSheetTitle = null == previousActiveSheet ? void 0 : previousActiveSheet.sheetTitle;
        this.sheetManager.setActiveSheet(sheetKey);
        const sheetDefine = this.sheetManager.getSheet(sheetKey);
        if (sheetDefine) {
            if (this.workSheetInstances.forEach((instance => {
                instance.getElement().style.display = "none", instance.tableInstance && this.tableEventRelay.unbindSheetEvents(instance.sheetKey, instance.tableInstance);
            })), this.workSheetInstances.has(sheetKey)) {
                const instance = this.workSheetInstances.get(sheetKey);
                if (!instance) throw new Error(`Worksheet instance for key "${sheetKey}" not found`);
                instance.getElement().style.display = "block", this.activeWorkSheet = instance, 
                instance.tableInstance && this.tableEventRelay.bindSheetEvents(instance.sheetKey, instance.tableInstance), 
                this.formulaManager.setActiveSheet(sheetKey), this._activeSheetTab(), this.sheetTabEventHandler.activeSheetMenuItem(), 
                this.restoreFilterState(instance, sheetDefine);
            } else {
                const instance = this.createWorkSheetInstance(sheetDefine);
                this.workSheetInstances.set(sheetKey, instance), this.activeWorkSheet = instance, 
                this.formulaManager.setActiveSheet(sheetKey), this.updateSheetTabs(), this.updateSheetMenu(), 
                this.restoreFilterState(instance, sheetDefine);
            }
            this.updateFormulaBar(), this.spreadsheetEventManager.emitSheetActivated(sheetKey, sheetDefine.sheetTitle, previousSheetKey, previousSheetTitle), 
            previousSheetKey && previousSheetKey !== sheetKey && this.spreadsheetEventManager.emitSheetDeactivated(previousSheetKey, previousSheetTitle);
        }
    }
    addSheet(sheet) {
        this.sheetManager.addSheet(sheet), this.updateSheetTabs(), this.updateSheetMenu();
    }
    removeSheet(sheetKey) {
        if (this.sheetManager.getSheetCount() <= 1) return void showSnackbar("至少保留一个工作表", 1300);
        const instance = this.workSheetInstances.get(sheetKey);
        instance && (instance.release(), this.workSheetInstances.delete(sheetKey));
        const newActiveSheetKey = this.sheetManager.removeSheet(sheetKey);
        newActiveSheetKey && this.activateSheet(newActiveSheetKey), this.updateSheetTabs(), 
        this.updateSheetMenu();
    }
    getSheetCount() {
        return this.sheetManager.getSheetCount();
    }
    getSheet(sheetKey) {
        return this.sheetManager.getSheet(sheetKey);
    }
    getAllSheets() {
        return this.sheetManager.getAllSheets();
    }
    createWorkSheetInstance(sheetDefine) {
        var _a, _b, _c;
        formulaEditor.setSheet(this);
        const contentWidth = this.contentElement.clientWidth, contentHeight = this.contentElement.clientHeight;
        sheetDefine.dragOrder = null !== (_a = sheetDefine.dragOrder) && void 0 !== _a ? _a : this.options.dragOrder;
        const sheet = new WorkSheet(this, Object.assign(Object.assign({}, sheetDefine), {
            container: this.contentElement,
            width: contentWidth,
            height: contentHeight,
            defaultRowHeight: this.options.defaultRowHeight,
            defaultColWidth: this.options.defaultColWidth,
            dragOrder: sheetDefine.dragOrder,
            plugins: getTablePlugins(sheetDefine, this.options, this),
            headerEditor: "formula",
            editor: "formula",
            select: {
                makeSelectCellVisible: !1
            },
            editCellTrigger: [ "api", "keydown", "doubleclick" ],
            customMergeCell: sheetDefine.cellMerge,
            theme: (null === (_b = sheetDefine.theme) || void 0 === _b ? void 0 : _b.tableTheme) || (null === (_c = this.options.theme) || void 0 === _c ? void 0 : _c.tableTheme)
        }));
        try {
            const normalizedData = sheetDefine.data ? this.formulaManager.normalizeSheetData(sheetDefine.data, sheet.tableInstance) : [];
            this.formulaManager.addSheet(sheetDefine.sheetKey, normalizedData, sheetDefine.sheetTitle), 
            sheetDefine.formulas && Object.keys(sheetDefine.formulas).length > 0 && this.loadFormulas(sheetDefine.sheetKey, sheetDefine.formulas);
        } catch (error) {}
        return sheet;
    }
    loadFormulas(sheetKey, formulas) {
        if (formulas && 0 !== Object.keys(formulas).length) try {
            const sortedFormulas = this.formulaManager.sortFormulasByDependency(sheetKey, formulas);
            for (const [cellRef, formula] of sortedFormulas) {
                const {row: row, col: col} = this.parseCellKey(cellRef);
                this.formulaManager.setCellContent({
                    sheet: sheetKey,
                    row: row,
                    col: col
                }, formula);
            }
        } catch (error) {}
    }
    parseCellKey(cellKey) {
        const match = cellKey.match(/^([A-Za-z]+)(\d+)$/);
        if (!match) throw new Error(`Invalid cell reference: ${cellKey}`);
        const [, colStr, rowStr] = match, row = parseInt(rowStr, 10) - 1;
        let col = 0;
        for (let i = 0; i < colStr.length; i++) {
            col = 26 * col + (colStr.toUpperCase().charCodeAt(i) - 65);
        }
        return {
            row: row,
            col: col
        };
    }
    restoreFilterState(sheet, sheetDefine) {
        sheetDefine.filterState && setTimeout((() => {
            if (sheet.tableInstance && sheet.tableInstance.pluginManager) {
                const filterPlugin = sheet.tableInstance.pluginManager.getPluginByName("Filter");
                filterPlugin && filterPlugin.setFilterState(sheetDefine.filterState);
            }
        }), 0);
    }
    _addNewSheet() {
        const sheetCount = this.sheetManager.getSheetCount();
        let key = `sheet${sheetCount + 1}`, title = `Sheet ${sheetCount + 1}`, index = sheetCount + 1;
        const existingKeys = new Set(this.sheetManager.getAllSheets().map((s => s.sheetKey))), existingTitles = new Set(this.sheetManager.getAllSheets().map((s => s.sheetTitle)));
        for (;existingKeys.has(key) || existingTitles.has(title); ) index += 1, key = `sheet${index}`, 
        title = `Sheet ${index}`;
        const newSheet = {
            sheetKey: key,
            sheetTitle: title,
            columnCount: 20,
            rowCount: 100,
            data: []
        };
        this.sheetManager.addSheet(newSheet), this.activateSheet(key);
    }
    updateFormulaBar() {
        this.formulaUIManager.updateFormulaBar();
    }
    getFormulaManager() {
        return this.formulaManager;
    }
    getSpreadSheetEventManager() {
        return this.spreadsheetEventManager;
    }
    getEventBus() {
        return this.eventBus;
    }
    getSheetManager() {
        return this.sheetManager;
    }
    getActiveSheet() {
        return this.activeWorkSheet;
    }
    onTableEvent(type, callback) {
        this.tableEventRelay.onTableEvent(type, callback);
    }
    offTableEvent(type, callback) {
        this.tableEventRelay.offTableEvent(type, callback);
    }
    onSheetEvent(type, callback) {
        this.spreadsheetEventManager.on(type, callback);
    }
    offSheetEvent(type, callback) {
        this.spreadsheetEventManager.off(type, callback);
    }
    on(type, callback) {
        this.onSheetEvent(type, callback);
    }
    off(type, callback) {
        this.offSheetEvent(type, callback);
    }
    getSheetByName(sheetName) {
        for (const [sheetKey, workSheet] of this.workSheetInstances) {
            const sheetDefine = this.sheetManager.getSheet(sheetKey);
            if (sheetDefine && sheetDefine.sheetTitle === sheetName) return workSheet;
        }
        return null;
    }
    getWorkSheetByKey(sheetKey) {
        return this.workSheetInstances.get(sheetKey) || null;
    }
    saveToConfig() {
        const sheets = [];
        return this.sheetManager.getAllSheets().forEach((sheetDefine => {
            const instance = this.workSheetInstances.get(sheetDefine.sheetKey);
            if (instance) {
                const data = instance.getCopiedData(), columns = instance.getColumns().map((column => {
                    const rest = __rest(column, []);
                    return delete rest.field, rest;
                })), lastTitleIndex = columns.reduce(((lastIndex, column, index) => column.title ? index : lastIndex), -1);
                -1 === lastTitleIndex ? columns.length = 0 : columns.splice(lastTitleIndex + 1);
                const lastDataIndex = data.reduce(((lastIndex, rowData, index) => rowData ? index : lastIndex), -1);
                -1 === lastDataIndex ? data.length = 0 : data.splice(lastDataIndex + 1);
                let filterState = null;
                const filterPlugin = instance.tableInstance.pluginManager.getPluginByName("Filter");
                filterPlugin && (filterState = filterPlugin.getFilterState());
                let currentSortState, sortState = instance.tableInstance.internalProps.sortState;
                sortState && (sortState = Array.isArray(sortState) ? sortState : [ sortState ], 
                currentSortState = sortState.map((item => Object.assign({
                    field: item.field,
                    order: item.order
                }, null !== item.orderFn && void 0 !== item.orderFn && {
                    orderFn: item.orderFn
                }))));
                const formulas = this.formulaManager.exportFormulas(sheetDefine.sheetKey), columnWidthConfig = Array.from(instance.tableInstance.internalProps._widthResizedColMap).map((key => ({
                    key: key,
                    width: instance.tableInstance.getColWidth(key)
                }))), rowHeightConfig = Array.from(instance.tableInstance.internalProps._heightResizedRowMap).map((key => ({
                    key: key,
                    height: instance.tableInstance.getRowHeight(key)
                })));
                sheets.push(Object.assign(Object.assign({}, sheetDefine), {
                    data: data,
                    columns: columns,
                    cellMerge: instance.tableInstance.options.customMergeCell,
                    showHeader: instance.tableInstance.options.showHeader,
                    frozenRowCount: instance.tableInstance.frozenRowCount,
                    frozenColCount: instance.tableInstance.frozenColCount,
                    active: sheetDefine.sheetKey === this.sheetManager.getActiveSheet().sheetKey,
                    filterState: filterState,
                    sortState: currentSortState,
                    formulas: Object.keys(formulas).length > 0 ? formulas : void 0,
                    columnWidthConfig: columnWidthConfig,
                    rowHeightConfig: rowHeightConfig
                }));
            } else sheets.push(sheetDefine);
        })), Object.assign(Object.assign({}, this.options), {
            sheets: sheets
        });
    }
    updateMainMenu(mainMenu) {
        this.options.mainMenu = mainMenu, this.menuManager.updateMainMenu(mainMenu);
    }
    updateOption(options) {
        var _a, _b;
        if (!options) return;
        void 0 !== options.mainMenu && this.updateMainMenu(options.mainMenu);
        const pluginModulesChanged = pluginIsChanged(this.options.VTablePluginModules, options.VTablePluginModules), tableThemeChanged = tableThemeIsChanged(null === (_a = this.options.theme) || void 0 === _a ? void 0 : _a.tableTheme, null === (_b = options.theme) || void 0 === _b ? void 0 : _b.tableTheme), {sheets: sheets} = options;
        __rest(options, [ "sheets" ]);
        this.options = Object.assign(Object.assign({}, this.options), options), (Array.isArray(options.sheets) || pluginModulesChanged || tableThemeChanged) && this.updateSheets(options);
    }
    updateSheets(options) {
        const nextSheets = options.sheets || [], prevSheets = this.sheetManager.getAllSheets(), prevMap = new Map;
        prevSheets.forEach((sheet => {
            prevMap.set(sheet.sheetKey, sheet);
        }));
        const added = [], updated = [];
        nextSheets.forEach((next => {
            const prev = prevMap.get(next.sheetKey);
            prev ? (updated.push({
                prev: prev,
                next: next
            }), prevMap.delete(next.sheetKey)) : added.push(next);
        }));
        Array.from(prevMap.keys()).forEach((sheetKey => {
            const instance = this.workSheetInstances.get(sheetKey);
            instance && (instance.release(), this.workSheetInstances.delete(sheetKey));
            try {
                this.sheetManager.removeSheet(sheetKey), this.formulaManager.removeSheet(sheetKey);
            } catch (error) {}
        })), added.forEach((sheetDefine => {
            this.sheetManager.addSheet(sheetDefine);
            const instance = this.createWorkSheetInstance(sheetDefine);
            this.workSheetInstances.set(sheetDefine.sheetKey, instance);
        })), updated.forEach((({prev: prev, next: next_sheetDefine}) => {
            var _a, _b;
            const instance = this.workSheetInstances.get(next_sheetDefine.sheetKey);
            if (Object.assign(prev, next_sheetDefine), !instance) {
                const newInstance = this.createWorkSheetInstance(next_sheetDefine);
                return void this.workSheetInstances.set(next_sheetDefine.sheetKey, newInstance);
            }
            const sheetOption = {
                sheetTitle: next_sheetDefine.sheetTitle,
                sheetKey: next_sheetDefine.sheetKey,
                showHeader: next_sheetDefine.showHeader,
                frozenRowCount: next_sheetDefine.frozenRowCount,
                frozenColCount: next_sheetDefine.frozenColCount,
                filter: next_sheetDefine.filter,
                filterState: next_sheetDefine.filterState,
                sortState: next_sheetDefine.sortState,
                columnWidthConfig: next_sheetDefine.columnWidthConfig,
                rowHeightConfig: next_sheetDefine.rowHeightConfig,
                defaultRowHeight: this.options.defaultRowHeight,
                defaultColWidth: this.options.defaultColWidth,
                dragOrder: next_sheetDefine.dragOrder,
                plugins: getTablePlugins(next_sheetDefine, this.options, this),
                headerEditor: "formula",
                editor: "formula",
                select: {
                    makeSelectCellVisible: !1
                },
                editCellTrigger: [ "api", "keydown", "doubleclick" ],
                customMergeCell: next_sheetDefine.cellMerge,
                theme: (null === (_a = next_sheetDefine.theme) || void 0 === _a ? void 0 : _a.tableTheme) || (null === (_b = this.options.theme) || void 0 === _b ? void 0 : _b.tableTheme),
                data: next_sheetDefine.data,
                columns: next_sheetDefine.columns
            };
            instance.updateSheetOption(sheetOption);
        }));
        try {
            this.formulaManager.rebuildFormulas(nextSheets), nextSheets.forEach((sheetDefine => {
                const worksheetInstance = this.workSheetInstances.get(sheetDefine.sheetKey);
                if (!worksheetInstance || !worksheetInstance.tableInstance) return;
                const dataToUse = sheetDefine.data || worksheetInstance.getData(), normalizedData = this.formulaManager.normalizeSheetData(dataToUse, worksheetInstance.tableInstance);
                this.formulaManager.formulaEngine.updateSheetData(sheetDefine.sheetKey, normalizedData), 
                sheetDefine.formulas && Object.keys(sheetDefine.formulas).length > 0 && this.loadFormulas(sheetDefine.sheetKey, sheetDefine.formulas);
            })), updated.forEach((({prev: prev, next: next_sheetDefine}) => {
                const hadFormulas = prev.formulas && Object.keys(prev.formulas).length > 0, hasFormulas = next_sheetDefine.formulas && Object.keys(next_sheetDefine.formulas).length > 0;
                if (!hadFormulas || hasFormulas) return;
                const worksheetInstance = this.workSheetInstances.get(next_sheetDefine.sheetKey);
                if (!worksheetInstance) return;
                const data = next_sheetDefine.data || worksheetInstance.getData();
                Object.keys(prev.formulas).forEach((cellRef => {
                    var _a;
                    try {
                        const {row: row, col: col} = this.parseCellKey(cellRef), cellValue = null === (_a = null == data ? void 0 : data[row]) || void 0 === _a ? void 0 : _a[col];
                        void 0 !== cellValue && this.formulaManager.setCellContent({
                            sheet: next_sheetDefine.sheetKey,
                            row: row,
                            col: col
                        }, cellValue);
                    } catch (error) {}
                }));
            }));
        } catch (error) {}
        this.sheetManager.sortSheets(this.options.sheets), this.updateSheetTabs(), this.updateSheetMenu();
        const activeDefine = nextSheets.find((s => s.active)) || nextSheets[0];
        activeDefine && this.activateSheet(activeDefine.sheetKey);
    }
    exportSheetToFile(fileType, allSheets = !0) {
        var _a, _b;
        try {
            this.spreadsheetEventManager.emitExportStart(fileType, allSheets);
            const sheet = this.getActiveSheet();
            if (!sheet) throw new Error("No active sheet available for export");
            let sheetCount = 0;
            if ("csv" === fileType) {
                if (!(null === (_a = sheet.tableInstance) || void 0 === _a ? void 0 : _a.exportToCsv)) throw new Error("TableExportPlugin not configured for CSV export");
                sheet.tableInstance.exportToCsv(), sheetCount = 1;
            } else if (allSheets) this.exportAllSheetsToExcel(), sheetCount = this.sheetManager.getSheetCount(); else {
                if (!(null === (_b = sheet.tableInstance) || void 0 === _b ? void 0 : _b.exportToExcel)) throw new Error("TableExportPlugin not configured for Excel export");
                sheet.tableInstance.exportToExcel(), sheetCount = 1;
            }
            this.spreadsheetEventManager.emitExportCompleted(fileType, allSheets, sheetCount);
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            this.spreadsheetEventManager.emitExportError(fileType, allSheets, errorMessage);
        }
    }
    exportAllSheetsToExcel() {
        var _b;
        this.initAllSheetInstances();
        const tables = this.sheetManager.getAllSheets().map((def => {
            const inst = this.workSheetInstances.get(def.sheetKey);
            if (!inst) throw new Error(`Worksheet instance for key "${def.sheetKey}" not found during export`);
            return {
                table: inst.tableInstance,
                name: def.sheetTitle || def.sheetKey
            };
        }));
        null === (_b = this._exportMutipleTablesToExcel) || void 0 === _b || _b.call(this, tables);
    }
    initAllSheetInstances() {
        this.sheetManager.getAllSheets().forEach((def => {
            if (!this.workSheetInstances.has(def.sheetKey)) {
                const instance = this.createWorkSheetInstance(def);
                this.workSheetInstances.set(def.sheetKey, instance);
            }
        }));
    }
    importFileToSheet(options = {
        clearExisting: !0
    }) {
        var _a, _b;
        return __awaiter(this, void 0, void 0, (function*() {
            try {
                let result;
                if (this.spreadsheetEventManager.emitImportStart("xlsx"), null == this ? void 0 : this._importFile) result = yield this._importFile({
                    clearExisting: !1 !== (null == options ? void 0 : options.clearExisting)
                }); else {
                    const sheet = this.getActiveSheet();
                    if (!sheet) throw new Error("No active sheet available for import");
                    if (!(null === (_a = sheet.tableInstance) || void 0 === _a ? void 0 : _a.importFile)) throw new Error("ExcelImportPlugin not configured");
                    result = yield sheet.tableInstance.importFile({
                        clearExisting: !1 !== (null == options ? void 0 : options.clearExisting)
                    });
                }
                const sheetCount = result && "sheets" in result && (null === (_b = result.sheets) || void 0 === _b ? void 0 : _b.length) || 0;
                return this.spreadsheetEventManager.emitImportCompleted("xlsx", sheetCount), result;
            } catch (error) {
                const errorMessage = error instanceof Error ? error.message : String(error);
                throw this.spreadsheetEventManager.emitImportError("xlsx", errorMessage), error;
            }
        }));
    }
    getContainer() {
        return this.container;
    }
    getRootElement() {
        return this.rootElement;
    }
    getOptions() {
        return this.options;
    }
    getFormulaBarElement() {
        return this.formulaBarElement;
    }
    getSheetTabElement() {
        return this.sheetTabElement;
    }
    getContentElement() {
        return this.contentElement;
    }
    release() {
        this.spreadsheetEventManager.emitDestroyed(), this.tableEventRelay.clearAllListeners(), 
        this.eventManager.release(), this.formulaManager.release(), this.formulaUIManager.release(), 
        this.spreadsheetEventManager.clearAllListeners(), this.menuManager && this.menuManager.release(), 
        this.sheetTabEventHandler.removeClickOutsideListener(), this.workSheetInstances.forEach((instance => {
            instance.release();
        })), this.rootElement && this.rootElement.parentNode && this.rootElement.parentNode.removeChild(this.rootElement), 
        this.formulaAutocomplete && this.formulaAutocomplete.release(), this.formulaManager.cellHighlightManager && this.formulaManager.cellHighlightManager.release();
    }
    exportData(sheetKey) {
        const sheet = this.workSheetInstances.get(sheetKey);
        return sheet ? sheet.getData() : [];
    }
    exportAllData() {
        return Array.from(this.workSheetInstances.values()).map((sheet => sheet.getData()));
    }
    resize() {
        var _a;
        null === (_a = this.getActiveSheet()) || void 0 === _a || _a.resize();
    }
}
//# sourceMappingURL=vtable-sheet.js.map