import { system, TicksPerSecond, world } from "@minecraft/server";
export class StructureDebugger {
    constructor(structureIds) {
        this.startLocation = { x: 1000, y: 100, z: 100 };
        this._onReady(structureIds);
    }
    async _onReady(structureIds) {
        const player = world.getPlayers()[0];
        const dimension = world.getDimension("overworld");
        const count = structureIds.length;
        const columns = Math.ceil(Math.sqrt(count));
        let maxRowHeights = [];
        let maxColWidths = [];
        // Pre-fetch structures and their sizes
        const structures = structureIds.map(id => world.structureManager.get(id));
        const sizes = structures.map(s => s?.size ?? { x: 1, y: 1, z: 1 });
        // Calculate max width per column and max depth per row
        for (let i = 0; i < count; i++) {
            const row = Math.floor(i / columns);
            const col = i % columns;
            maxRowHeights[row] = Math.max(maxRowHeights[row] || 0, sizes[i].z);
            maxColWidths[col] = Math.max(maxColWidths[col] || 0, sizes[i].x);
        }
        // Place structures in grid
        for (let i = 0; i < count; i++) {
            const structure = structures[i];
            if (!structure) {
                console.warn(`Structure with ID ${structureIds[i]} not found`);
                continue;
            }
            const row = Math.floor(i / columns);
            const col = i % columns;
            // Calculate X offset (sum of max widths of previous columns)
            let xOffset = 0;
            for (let c = 0; c < col; c++)
                xOffset += maxColWidths[c] + 1;
            // Calculate Z offset (sum of max heights of previous rows)
            let zOffset = 0;
            for (let r = 0; r < row; r++)
                zOffset += maxRowHeights[r] + 1;
            const location = {
                x: this.startLocation.x + xOffset,
                y: this.startLocation.y,
                z: this.startLocation.z + zOffset
            };
            player.teleport(location);
            await system.waitTicks(TicksPerSecond); // Delay between placements
            world.structureManager.place(structure, dimension, location);
        }
    }
}
