import { buildAxesHelper, buildBoundsHelper, buildCameraFrustumHelper, buildDirectionalLightHelper, buildGridHelper, buildSkeletonHelper } from "../../debug/index.js";
import { LineSegmentsCompat, Object3DCompat } from "../core/Object3DCompat.js";
import { BufferGeometryCompat } from "../geometries/index.js";
import { LineBasicMaterialCompat } from "../materials/index.js";
export class HelperLineSegmentsCompat extends LineSegmentsCompat {
    geometry;
    material;
    lines;
    constructor(type, lines) {
        const geometry = lineGeometry(lines);
        const material = new LineBasicMaterialCompat();
        super(geometry, material);
        this.geometry = geometry;
        this.material = material;
        this.type = type;
        this.lines = lines;
    }
}
export class AxesHelperCompat extends HelperLineSegmentsCompat {
    size;
    constructor(size = 1) {
        super("AxesHelper", buildAxesHelper({ size }));
        this.size = size;
    }
}
export class GridHelperCompat extends HelperLineSegmentsCompat {
    size;
    divisions;
    constructor(size = 10, divisions = 10) {
        super("GridHelper", buildGridHelper({ size, divisions }));
        this.size = size;
        this.divisions = divisions;
    }
}
export class BoxHelperCompat extends HelperLineSegmentsCompat {
    object;
    color;
    constructor(object, color = 0xffff00) {
        super("BoxHelper", buildBoundsHelper(resolveObjectBounds(object)));
        this.object = object;
        this.color = color;
    }
}
export class CameraHelperCompat extends HelperLineSegmentsCompat {
    camera;
    constructor(camera) {
        super("CameraHelper", buildCameraFrustumHelper(resolveCameraFrustum(camera)));
        this.camera = camera;
    }
}
export class DirectionalLightHelperCompat extends HelperLineSegmentsCompat {
    light;
    size;
    constructor(light, size = 1) {
        super("DirectionalLightHelper", buildDirectionalLightHelper({
            direction: [0, -1, 0],
            origin: [light.position.x, light.position.y, light.position.z],
            length: size
        }));
        this.light = light;
        this.size = size;
    }
}
export class SkeletonHelperCompat extends HelperLineSegmentsCompat {
    root;
    constructor(root) {
        super("SkeletonHelper", buildSkeletonHelper(collectSkeletonJoints(root)));
        this.root = root;
    }
}
function lineGeometry(lines) {
    const positions = [];
    const colors = [];
    for (const line of lines) {
        positions.push(...line.from, ...line.to);
        colors.push(...line.color, ...line.color);
    }
    return new BufferGeometryCompat()
        .setAttribute("position", { array: positions, itemSize: 3 })
        .setAttribute("color", { array: colors, itemSize: 4 })
        .setDrawRange(0, lines.length * 2);
}
function resolveObjectBounds(object) {
    const explicit = object.userData.bounds;
    if (isVec3(explicit?.min) && isVec3(explicit?.max)) {
        return { min: explicit.min, max: explicit.max };
    }
    const sx = Math.max(Math.abs(object.scale.x), 1e-6) * 0.5;
    const sy = Math.max(Math.abs(object.scale.y), 1e-6) * 0.5;
    const sz = Math.max(Math.abs(object.scale.z), 1e-6) * 0.5;
    return {
        min: [object.position.x - sx, object.position.y - sy, object.position.z - sz],
        max: [object.position.x + sx, object.position.y + sy, object.position.z + sz]
    };
}
function resolveCameraFrustum(camera) {
    if (camera.type === "PerspectiveCamera") {
        const perspective = camera;
        const nearHalfHeight = Math.tan((perspective.fov * Math.PI / 180) * 0.5) * perspective.near;
        const nearHalfWidth = nearHalfHeight * perspective.aspect;
        const farHalfHeight = Math.tan((perspective.fov * Math.PI / 180) * 0.5) * perspective.far;
        const farHalfWidth = farHalfHeight * perspective.aspect;
        return { nearHalfWidth, nearHalfHeight, farHalfWidth, farHalfHeight, nearZ: -perspective.near, farZ: -perspective.far };
    }
    const orthographic = camera;
    return {
        nearHalfWidth: Math.abs(orthographic.right - orthographic.left) * 0.5,
        nearHalfHeight: Math.abs(orthographic.top - orthographic.bottom) * 0.5,
        farHalfWidth: Math.abs(orthographic.right - orthographic.left) * 0.5,
        farHalfHeight: Math.abs(orthographic.top - orthographic.bottom) * 0.5,
        nearZ: -orthographic.near,
        farZ: -orthographic.far
    };
}
function collectSkeletonJoints(root) {
    const joints = [];
    root.traverse((object) => {
        joints.push({
            id: object.uuid,
            parentId: object.parent?.uuid,
            position: [object.position.x, object.position.y, object.position.z]
        });
    });
    return joints;
}
function isVec3(value) {
    return value?.length === 3 && value.every((component) => Number.isFinite(component));
}
//# sourceMappingURL=index.js.map