"use strict";

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

class SheetManager {
    constructor() {
        this._sheets = new Map, this._activeSheetKey = "";
    }
    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`);
        this._sheets.set(sheet.sheetKey, sheet);
    }
    removeSheet(sheetKey) {
        if (!this._sheets.has(sheetKey)) throw new Error(`Sheet with key '${sheetKey}' not found`);
        if (sheetKey === this._activeSheetKey) {
            let nextSheet = null;
            for (const [key, sheet] of this._sheets.entries()) if (key !== sheetKey) {
                nextSheet = sheet;
                break;
            }
            nextSheet ? (this._activeSheetKey = nextSheet.sheetKey, nextSheet.active = !0) : this._activeSheetKey = "";
        }
        return this._sheets.delete(sheetKey), this._activeSheetKey;
    }
    renameSheet(sheetKey, newTitle) {
        if (!this._sheets.has(sheetKey)) throw new Error(`Sheet with key '${sheetKey}' not found`);
        this._sheets.get(sheetKey).sheetTitle = newTitle;
    }
    getAllSheets() {
        return Array.from(this._sheets.values());
    }
    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);
        }));
    }
}

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