import { scaleToVec3 } from '@hiberworld/code-kit';
/**
 * Returns the bounding box of a prefab or a group.
 * It's not recursive, and perhaps not yet perfect.
 * @example
 * ```ts
  const art1 = getFramedImage({
    s: pop3(1.0),
    p: y(1),
    src: 'https://cdn.hibervr.com/demo/img/amanda.jpg',
    ratio: 1000 / 560,
  });
  const bb = getBoundingBox(art1);
 * ```
 */
export const getBoundingBox = (target) => {
    let minX = Infinity;
    let minY = Infinity;
    let minZ = Infinity;
    let maxX = -Infinity;
    let maxY = -Infinity;
    let maxZ = -Infinity;
    const calcFn = (position, scale) => {
        minX = Math.min(minX, position.x - scale.x / 2);
        minY = Math.min(minY, position.y - scale.y / 2);
        minZ = Math.min(minZ, position.z - scale.z / 2);
        maxX = Math.max(maxX, position.x + scale.x / 2);
        maxY = Math.max(maxY, position.y + scale.y / 2);
        maxZ = Math.max(maxZ, position.z + scale.z / 2);
    };
    if (target?.transform && target.children) {
        let topScale = target.transform.scale ?? [1, 1, 1];
        if (target.children.length > 0) {
            for (let index = 0; index < target.children.length; index++) {
                const child = target.children[index];
                if (child?.transform) {
                    const scale3 = scaleToVec3(child.transform.scale);
                    const scale = {
                        x: scale3?.[0] ?? 0,
                        y: scale3?.[1] ?? 0,
                        z: scale3?.[2] ?? 0,
                    };
                    const position = {
                        x: child.transform.pos?.[0] ?? 0,
                        y: child.transform.pos?.[1] ?? 0,
                        z: child.transform.pos?.[2] ?? 0,
                    };
                    calcFn(position, scale);
                }
            }
        }
        else {
            topScale = [1, 1, 1];
            const scale3 = scaleToVec3(target.transform.scale);
            const scale = {
                x: scale3?.[0] ?? 0,
                y: scale3?.[1] ?? 0,
                z: scale3?.[2] ?? 0,
            };
            const position = {
                x: target.transform.pos?.[0] ?? 0,
                y: target.transform.pos?.[1] ?? 0,
                z: target.transform.pos?.[2] ?? 0,
            };
            calcFn(position, scale);
        }
        const topScale3 = scaleToVec3(topScale);
        const boundingBox = {
            minX: minX * topScale3[0],
            minY: minY * topScale3[1],
            minZ: minZ * topScale3[2],
            maxX: maxX * topScale3[0],
            maxY: maxY * topScale3[1],
            maxZ: maxZ * topScale3[2],
            width: -1,
            height: -1,
            depth: -1,
        };
        boundingBox.width = boundingBox.maxX - boundingBox.minX;
        boundingBox.height = boundingBox.maxY - boundingBox.minY;
        boundingBox.depth = boundingBox.maxZ - boundingBox.minZ;
        return boundingBox;
    }
};
//# sourceMappingURL=getBoundingBox.js.map