import { AABB3 } from "../../../../core/geom/3d/aabb/AABB3.js";
import { ParentEntitySystem } from "../../../ecs/parent/ParentEntitySystem.js";
import { ShadedGeometry } from "./ShadedGeometry.js";

const scratch_aabb3 = new AABB3();

/**
 *
 * @param {AABB3} result
 * @param {number} root_entity
 * @param {EntityComponentDataset} ecd
 */
export function sg_hierarchy_compute_bounding_box_via_parent_entity(result, root_entity, ecd) {
    function visit(entity, ecd) {

        /**
         * @type {ShadedGeometry}
         */
        const sg = ecd.getComponent(root_entity, ShadedGeometry);

        if (sg !== undefined) {
            sg.getBoundingBox(scratch_aabb3);

            result.union(scratch_aabb3);
        }
    }

    result.setNegativelyInfiniteBounds();

    ParentEntitySystem.traverse(ecd, root_entity, visit);
}
