import { WorkbookCommandService } from './workbook-command-service.js';
import { WorkbookFeatureRegistry } from './workbook-feature-registry.js';
import { WorkbookProjectionInterceptorService } from './workbook-projection-interceptors.js';
import { createWorkbookTablesFeaturePlugin, registerWorkbookTablesFeature, WORKBOOK_TABLE_COMMAND_IDS, WORKBOOK_TABLES_FEATURE_ID, } from './workbook-tables-feature.js';
export class WorkbookFacade {
    engine;
    features = new WorkbookFeatureRegistry();
    projectionService;
    commands;
    disposers = [];
    constructor(engine) {
        this.engine = engine;
        this.projectionService = new WorkbookProjectionInterceptorService(engine);
        this.commands = new WorkbookCommandService(engine, this.projectionService);
        this.features.register(createWorkbookTablesFeaturePlugin());
        this.features.activateAll();
        this.disposers.push(registerWorkbookTablesFeature({ commandService: this.commands, projectionService: this.projectionService }));
    }
    table(name) {
        return new WorkbookTableFacade(this.engine, name);
    }
    selection(range) {
        return new WorkbookSelectionFacade(this.commands, range);
    }
    command(request) {
        return new WorkbookCommandFacade(this.commands, request);
    }
    projection() {
        return new WorkbookProjectionFacade(this.projectionService);
    }
    dispose() {
        this.disposers.toReversed().forEach((dispose) => {
            dispose();
        });
        this.features.disposeAll();
    }
}
export class WorkbookTableFacade {
    engine;
    name;
    constructor(engine, name) {
        this.engine = engine;
        this.name = name;
    }
    snapshot() {
        return this.engine.getTable(this.name);
    }
    column(columnName) {
        const table = this.snapshot();
        if (!table) {
            throw new Error(`Table ${this.name} does not exist`);
        }
        const columnIndex = table.columnNames.findIndex((name) => name.trim().toUpperCase() === columnName.trim().toUpperCase());
        if (columnIndex < 0) {
            throw new Error(`Table ${this.name} column ${columnName} does not exist`);
        }
        return {
            tableName: table.name,
            columnName: table.columnNames[columnIndex],
            columnIndex,
        };
    }
}
export class WorkbookSelectionFacade {
    commands;
    range;
    constructor(commands, range) {
        this.commands = commands;
        this.range = range;
    }
    createTable(options = {}) {
        return new WorkbookCommandFacade(this.commands, {
            featureId: WORKBOOK_TABLES_FEATURE_ID,
            commandId: WORKBOOK_TABLE_COMMAND_IDS.createFromSelection,
            category: 'command',
            input: {
                range: {
                    sheetName: this.range.sheetName,
                    startAddress: this.range.startAddress,
                    endAddress: this.range.endAddress,
                },
                ...(options.name !== undefined ? { name: options.name } : {}),
                ...(options.hasHeaders !== undefined ? { hasHeaders: options.hasHeaders } : {}),
            },
        });
    }
}
export class WorkbookCommandFacade {
    commands;
    request;
    constructor(commands, request) {
        this.commands = commands;
        this.request = request;
    }
    preview() {
        return this.commands.preview(this.request);
    }
    apply() {
        return this.commands.apply(this.request);
    }
    applyAndVerify() {
        return this.commands.applyAndVerify(this.request);
    }
}
export class WorkbookProjectionFacade {
    projection;
    constructor(projection) {
        this.projection = projection;
    }
    rangeChrome(range) {
        return this.projection.rangeChrome(range);
    }
}
export function createWorkbookFacade(engine) {
    return new WorkbookFacade(engine);
}
//# sourceMappingURL=workbook-facade.js.map