"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitVoxelPrimitive = splitVoxelPrimitive;
exports.splitVoxelBoxClone = splitVoxelBoxClone;
const axisIndex = { x: 0, y: 1, z: 2 };
function splitVoxelBoxInternal(primitive, axis, plane) {
    const axisIdx = axisIndex[axis];
    const extent = Math.round(primitive.size[axisIdx]);
    const splitIndex = Math.round(plane);
    if (splitIndex <= 0 || splitIndex >= extent) {
        throw new Error(`Split plane ${splitIndex} must be within (0, ${extent}) for axis ${axis}`);
    }
    const leftSize = [...primitive.size];
    const rightSize = [...primitive.size];
    leftSize[axisIdx] = splitIndex;
    rightSize[axisIdx] = extent - splitIndex;
    const leftVoxels = new Array(leftSize[0] * leftSize[1] * leftSize[2]).fill(0);
    const rightVoxels = new Array(rightSize[0] * rightSize[1] * rightSize[2]).fill(0);
    const voxels = primitive.voxels ?? [];
    const [sx, sy, sz] = primitive.size;
    const stride = (x, y, z) => x + sx * (y + sy * z);
    for (let z = 0; z < sz; z += 1) {
        for (let y = 0; y < sy; y += 1) {
            for (let x = 0; x < sx; x += 1) {
                const value = voxels[stride(x, y, z)] ?? 0;
                if (value === 0) {
                    continue;
                }
                if ((axis === 'x' && x < splitIndex) || (axis === 'y' && y < splitIndex) || (axis === 'z' && z < splitIndex)) {
                    const li = x + leftSize[0] * (y + leftSize[1] * z);
                    leftVoxels[li] = value;
                }
                else {
                    const rx = axis === 'x' ? x - splitIndex : x;
                    const ry = axis === 'y' ? y - splitIndex : y;
                    const rz = axis === 'z' ? z - splitIndex : z;
                    const ri = rx + rightSize[0] * (ry + rightSize[1] * rz);
                    rightVoxels[ri] = value;
                }
            }
        }
    }
    const paletteClone = primitive.palette ? primitive.palette.slice() : [];
    const left = {
        id: `${primitive.id}_part_a`,
        kind: 'voxel_box',
        size: leftSize,
        voxels: leftVoxels,
        palette: paletteClone,
    };
    const right = {
        id: `${primitive.id}_part_b`,
        kind: 'voxel_box',
        size: rightSize,
        voxels: rightVoxels,
        palette: paletteClone,
    };
    return { result: { first: left, second: right }, splitIndex, extent };
}
function splitVoxelPrimitive(entity, primitiveId, axis, plane) {
    const primitiveIndex = entity.primitives.findIndex(primitive => primitive.id === primitiveId);
    if (primitiveIndex === -1) {
        throw new Error(`Primitive ${primitiveId} not found`);
    }
    const primitive = entity.primitives[primitiveIndex];
    if (primitive.kind !== 'voxel_box') {
        throw new Error(`Primitive ${primitiveId} is not a voxel box`);
    }
    const { result } = splitVoxelBoxInternal(primitive, axis, plane);
    entity.primitives.splice(primitiveIndex, 1, result.first, result.second);
    return { firstId: result.first.id, secondId: result.second.id };
}
function splitVoxelBoxClone(primitive, axis, plane) {
    const copy = JSON.parse(JSON.stringify(primitive));
    const { result } = splitVoxelBoxInternal(copy, axis, plane);
    return {
        first: { ...result.first, id: result.first.id },
        second: { ...result.second, id: result.second.id },
    };
}
//# sourceMappingURL=slice.js.map