"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TilingManager = void 0;
const LayoutCalculator_1 = require("./LayoutCalculator");
const tsyringe_1 = require("tsyringe");
/**
 * TilingManager
 *
 * Handles tiling calculations and view positioning for tiled window mode.
 * Extracted from ViewManager for better separation of concerns.
 */
let TilingManager = class TilingManager {
    config;
    workspaceBounds;
    constructor(config = { mode: "none", gap: 0, padding: 0 }, workspaceBounds = { x: 0, y: 0, width: 800, height: 600 }) {
        this.config = config;
        this.workspaceBounds = workspaceBounds;
    }
    /**
     * Update the workspace bounds used for tile calculations
     */
    setWorkspaceBounds(bounds) {
        this.workspaceBounds = bounds;
    }
    /**
     * Get current workspace bounds
     */
    getWorkspaceBounds() {
        return this.workspaceBounds;
    }
    /**
     * Update tiling configuration
     */
    setConfig(config) {
        this.config = config;
    }
    /**
     * Get current tiling configuration
     */
    getConfig() {
        return this.config;
    }
    /**
     * Check if tiling is enabled
     */
    isEnabled() {
        return this.config.mode !== "none";
    }
    /**
     * Calculate bounds for a tile based on tiling configuration
     * @param tileIndex - Index of the tile to calculate
     * @param visibleCount - Total number of visible tiled views
     */
    calculateTileBounds(tileIndex, visibleCount) {
        return LayoutCalculator_1.LayoutCalculator.calculateTileBounds({
            workspace: this.workspaceBounds,
            tileIndex,
            visibleCount,
            config: this.config,
        });
    }
    /**
     * Get the next available tile index from a collection of views
     * @param views - Iterable of view information
     */
    getNextTileIndex(views) {
        const indices = Array.from(views)
            .filter((v) => v.visible && v.mode === "tiled" && v.tileIndex !== undefined)
            .map((v) => v.tileIndex);
        if (indices.length === 0)
            return 0;
        return Math.max(...indices) + 1;
    }
    /**
     * Get count of visible tiled views
     * @param views - Iterable of view information
     */
    getVisibleTiledCount(views) {
        return Array.from(views).filter((v) => v.visible && v.mode === "tiled")
            .length;
    }
    /**
     * Recalculate bounds for all tiled views
     * Returns updated views with new bounds applied
     * @param views - Map of viewId to ViewInfo
     */
    recalculateTiledViews(views) {
        if (!this.isEnabled())
            return;
        // Get visible tiled views sorted by tile index
        const visibleViews = Array.from(views.entries())
            .filter(([_, info]) => info.visible && info.mode === "tiled")
            .sort((a, b) => (a[1].tileIndex || 0) - (b[1].tileIndex || 0));
        const visibleCount = visibleViews.length;
        visibleViews.forEach(([viewId, info], index) => {
            const bounds = this.calculateTileBounds(index, visibleCount);
            info.tileIndex = index;
            info.bounds = bounds;
            // Only set bounds if view is not destroyed
            if (!info.view.webContents.isDestroyed()) {
                info.view.setBounds(bounds);
            }
        });
    }
    /**
     * Determine view mode based on manifest window config and tiling state
     */
    determineViewMode(windowMode) {
        // If no window config mode specified
        if (!windowMode) {
            return this.isEnabled() ? "tiled" : "floating";
        }
        // Check what the app supports
        switch (windowMode) {
            case "floating":
                return "floating";
            case "tiled":
                return "tiled";
            case "both":
                // If app supports both, prefer tiled if tiling is enabled
                return this.isEnabled() ? "tiled" : "floating";
            default:
                return this.isEnabled() ? "tiled" : "floating";
        }
    }
};
exports.TilingManager = TilingManager;
exports.TilingManager = TilingManager = __decorate([
    (0, tsyringe_1.singleton)(),
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [Object, Object])
], TilingManager);
//# sourceMappingURL=TilingManager.js.map