"use strict";

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

const spreadsheet_events_1 = require("../ts-types/spreadsheet-events");

class SheetManager {
    constructor(eventBus) {
        this._sheets = new Map, this._activeSheetKey = "", this.eventBus = eventBus;
    }
    getEventBus() {
        return this.eventBus;
    }
    getActiveSheet() {
        if (!this._activeSheetKey || !this._sheets.has(this._activeSheetKey)) {
            const firstSheet = this._sheets.values().next().value;
            return firstSheet ? (this._activeSheetKey = firstSheet.sheetKey, firstSheet) : null;
        }
        return this._sheets.get(this._activeSheetKey);
    }
    setActiveSheet(sheetKey) {
        if (!this._sheets.has(sheetKey)) throw new Error(`Sheet with key '${sheetKey}' not found`);
        this._activeSheetKey = sheetKey;
        for (const [key, sheet] of this._sheets.entries()) sheet.active = key === sheetKey;
    }
    addSheet(sheet) {
        if (this._sheets.has(sheet.sheetKey)) throw new Error(`Sheet with key '${sheet.sheetKey}' already exists`);
        const index = this._sheets.size;
        this._sheets.set(sheet.sheetKey, sheet);
        const event = {
            sheetKey: sheet.sheetKey,
            sheetTitle: sheet.sheetTitle,
            index: index
        };
        this.eventBus.emit(spreadsheet_events_1.VTableSheetEventType.SHEET_ADDED, event);
    }
    removeSheet(sheetKey) {
        if (!this._sheets.has(sheetKey)) throw new Error(`Sheet with key '${sheetKey}' not found`);
        const sheetToRemove = this._sheets.get(sheetKey), index = Array.from(this._sheets.values()).findIndex((sheet => sheet.sheetKey === sheetKey));
        let willReplaceSheetKey;
        if (sheetKey === this._activeSheetKey) {
            let nextSheet = null;
            for (const [key, sheet] of this._sheets.entries()) if (key !== sheetKey) {
                nextSheet = sheet;
                break;
            }
            nextSheet ? (willReplaceSheetKey = nextSheet.sheetKey, nextSheet.active = !0) : (this._activeSheetKey = "", 
            willReplaceSheetKey = ""), this._activeSheetKey = willReplaceSheetKey;
        }
        this._sheets.delete(sheetKey);
        const event = {
            sheetKey: sheetToRemove.sheetKey,
            sheetTitle: sheetToRemove.sheetTitle,
            index: index
        };
        return this.eventBus.emit(spreadsheet_events_1.VTableSheetEventType.SHEET_REMOVED, event), 
        willReplaceSheetKey;
    }
    renameSheet(sheetKey, newTitle) {
        if (!this._sheets.has(sheetKey)) throw new Error(`Sheet with key '${sheetKey}' not found`);
        const sheet = this._sheets.get(sheetKey), oldTitle = sheet.sheetTitle;
        sheet.sheetTitle = newTitle;
        const event = {
            sheetKey: sheetKey,
            oldTitle: oldTitle,
            newTitle: newTitle
        };
        this.eventBus.emit(spreadsheet_events_1.VTableSheetEventType.SHEET_RENAMED, event);
    }
    getAllSheets() {
        return Array.from(this._sheets.values());
    }
    sortSheets(sheets) {
        const sheetsArray = Array.from(this._sheets.entries());
        sheetsArray.sort((([keyA], [keyB]) => sheets.findIndex((s => s.sheetKey === keyA)) - sheets.findIndex((s => s.sheetKey === keyB)))), 
        this._sheets.clear(), sheetsArray.forEach((([key, sheet]) => {
            this._sheets.set(key, sheet);
        }));
    }
    getSheet(sheetKey) {
        return this._sheets.get(sheetKey) || null;
    }
    getSheetCount() {
        return this._sheets.size;
    }
    getSheetAPI(sheetKey) {
        return null;
    }
    onSheetChange(callback) {}
    offSheetChange(callback) {}
    onActiveSheetChange(callback) {}
    offActiveSheetChange(callback) {}
    reorderSheet(sourceKey, targetKey, position) {
        if (sourceKey === targetKey) return;
        if (!this._sheets.has(sourceKey)) throw new Error(`Source sheet '${sourceKey}' does not exist`);
        if (!this._sheets.has(targetKey)) throw new Error(`Target sheet '${targetKey}' does not exist`);
        const sheetsArray = Array.from(this._sheets.entries()), sourceIndex = sheetsArray.findIndex((([key]) => key === sourceKey)), targetIndex = sheetsArray.findIndex((([key]) => key === targetKey));
        if (-1 === sourceIndex || -1 === targetIndex || sourceIndex === targetIndex) return;
        let insertIndex = "left" === position ? targetIndex : targetIndex + 1;
        sourceIndex < insertIndex && insertIndex--;
        const [movedSheet] = sheetsArray.splice(sourceIndex, 1);
        sheetsArray.splice(insertIndex, 0, movedSheet), this._sheets.clear(), sheetsArray.forEach((([key, sheet]) => {
            this._sheets.set(key, sheet);
        }));
        const event = {
            sheetKey: sourceKey,
            fromIndex: sourceIndex,
            toIndex: insertIndex
        };
        this.eventBus.emit(spreadsheet_events_1.VTableSheetEventType.SHEET_MOVED, event);
    }
}

exports.default = SheetManager;
//# sourceMappingURL=sheet-manager.js.map