"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LayoutCalculator = void 0;
/**
 * LayoutCalculator
 *
 * Generic utility for calculating tile bounds in various layout modes.
 * Provides a pluggable system for different tiling strategies.
 */
class LayoutCalculator {
    /**
     * Calculate bounds for a tile at a given index
     */
    static calculateTileBounds(params) {
        const { workspace, tileIndex, visibleCount, config } = params;
        const { mode, gap = 0, padding = 0, columns = 2, rows = 2 } = config;
        // Apply padding to workspace
        const available = this.applyPadding(workspace, padding);
        // No tiling or no visible items
        if (mode === "none" || visibleCount === 0) {
            return available;
        }
        // Calculate grid dimensions based on mode
        const grid = this.getGridDimensions(mode, visibleCount, rows, columns);
        // Calculate tile bounds in grid
        return this.calculateGridCell(available, tileIndex, grid, gap);
    }
    /**
     * Apply padding to bounds
     */
    static applyPadding(bounds, padding) {
        return {
            x: bounds.x + padding,
            y: bounds.y + padding,
            width: bounds.width - padding * 2,
            height: bounds.height - padding * 2,
        };
    }
    /**
     * Determine grid dimensions based on tiling mode
     */
    static getGridDimensions(mode, visibleCount, configRows, configColumns) {
        switch (mode) {
            case "horizontal":
                // Horizontal = 1 row × N columns
                return { rows: 1, columns: visibleCount };
            case "vertical":
                // Vertical = N rows × 1 column
                return { rows: visibleCount, columns: 1 };
            case "grid":
                // Use configured dimensions
                return { rows: configRows, columns: configColumns };
            default:
                return { rows: 1, columns: 1 };
        }
    }
    /**
     * Calculate bounds for a specific cell in a grid
     */
    static calculateGridCell(available, index, grid, gap) {
        const col = index % grid.columns;
        const row = Math.floor(index / grid.columns);
        const tileWidth = (available.width - gap * (grid.columns - 1)) / grid.columns;
        const tileHeight = (available.height - gap * (grid.rows - 1)) / grid.rows;
        return {
            x: available.x + col * (tileWidth + gap),
            y: available.y + row * (tileHeight + gap),
            width: tileWidth,
            height: tileHeight,
        };
    }
    /**
     * Calculate optimal grid dimensions for a given number of items
     * Useful for auto-layout modes
     */
    static calculateOptimalGrid(itemCount) {
        if (itemCount <= 0)
            return { rows: 1, columns: 1 };
        if (itemCount === 1)
            return { rows: 1, columns: 1 };
        if (itemCount === 2)
            return { rows: 1, columns: 2 };
        if (itemCount === 3)
            return { rows: 1, columns: 3 };
        if (itemCount === 4)
            return { rows: 2, columns: 2 };
        // For larger counts, try to make it roughly square
        const columns = Math.ceil(Math.sqrt(itemCount));
        const rows = Math.ceil(itemCount / columns);
        return { rows, columns };
    }
}
exports.LayoutCalculator = LayoutCalculator;
//# sourceMappingURL=LayoutCalculator.js.map