import { Box3 } from "three";
import { assert } from "../../../core/assert.js";

/**
 *
 * @param {Object3D} object
 * @param {Vector3} size
 * @param {Vector3} result
 */
export function scaleObject3ToBox(object, size, result) {
    assert.notEqual(object, undefined, "Object is undefined");
    assert.notEqual(object, null, "Object is null");

    const boundingBox = new Box3();

    object.traverse(a => {
        if (a.isMesh) {
            /**
             * @type {THREE.BufferGeometry}
             */
            const geometry = a.geometry;

            if (geometry.boundingBox === null) {
                geometry.computeBoundingBox();
            }
        }
    });

    boundingBox.expandByObject(object);

    //box size
    const actualBoxSize = boundingBox.max.clone().sub(boundingBox.min);
    const scale = Math.min(size.x / actualBoxSize.x, size.y / actualBoxSize.y, size.z / actualBoxSize.z);

    result.set(scale, scale, scale);

    return actualBoxSize.multiplyScalar(scale);
}
