import { DirectionalLight, Light, PointLight, Scene, SpotLight } from "../scene/index.js";
export class LightCollector {
    collect(scene, options = {}) {
        const maxLights = options.maxLights ?? Number.POSITIVE_INFINITY;
        if (maxLights !== Number.POSITIVE_INFINITY && (!Number.isInteger(maxLights) || maxLights < 0)) {
            throw new RangeError("LightCollector maxLights must be a non-negative integer");
        }
        scene.updateWorldTransforms();
        return scene
            .collectLights()
            .filter((light) => (options.includeDisabled ?? false) || light.visible)
            .filter((light) => ((options.layerMask ?? 0xffffffff) & light.layerMask) !== 0)
            .map((light) => collectLight(light))
            .sort((a, b) => b.intensity - a.intensity)
            .slice(0, maxLights);
    }
}
function collectLight(light) {
    const matrix = matrixElements(light.transform.worldMatrix);
    const position = [matrix[12], matrix[13], matrix[14]];
    const direction = light instanceof DirectionalLight
        ? vectorToTuple(light.getDirection())
        : normalize([-matrix[8], -matrix[9], -matrix[10]]);
    const range = light instanceof PointLight || light instanceof SpotLight ? light.range : 0;
    const spotAngle = light instanceof SpotLight ? light.angle : 0;
    const penumbra = light instanceof SpotLight ? light.penumbra : 0;
    return {
        kind: light.kind,
        color: vectorToTuple(light.color),
        intensity: light.intensity,
        position,
        direction,
        range,
        spotAngle,
        penumbra,
        castsShadow: light.castsShadow,
        layerMask: light.layerMask,
        source: light
    };
}
function vectorToTuple(value) {
    if (Array.isArray(value)) {
        return [value[0] ?? 0, value[1] ?? 0, value[2] ?? 0];
    }
    const vector = value;
    return [vector.x ?? 0, vector.y ?? 0, vector.z ?? 0];
}
function matrixElements(value) {
    const elements = value.elements;
    return elements ?? value;
}
function normalize(value) {
    const length = Math.hypot(value[0], value[1], value[2]);
    return length === 0 ? [0, 0, -1] : [value[0] / length, value[1] / length, value[2] / length];
}
//# sourceMappingURL=LightCollector.js.map