import { GameAssetType } from "../../../../asset/GameAssetType.js";
import { three_object_to_entity_composition } from "../../../ecs/mesh-v2/three_object_to_entity_composition.js";
import { ShadedGeometry } from "../../../ecs/mesh-v2/ShadedGeometry.js";
import { Transform } from "../../../../ecs/transform/Transform.js";

/**
 *
 * @param {Engine} engine
 * @param {string} url
 * @return {Promise<{mesh:ShadedGeometry,transform:mat4}[]>}
 */
export async function load_mesh_for_bake(engine, url) {

    const gltf_asset = await engine.assetManager.promise(url, GameAssetType.ModelGLTF_JSON);

    const node_hierarchy = three_object_to_entity_composition(gltf_asset.create());

    const objects = [];

    node_hierarchy.traverse(n => {
        const sg = n.entity.getComponent(ShadedGeometry);
        if (sg !== null) {
            objects.push({
                mesh: sg,
                transform: n.entity.getComponent(Transform).matrix
            })
        }
    });

    return objects;
}
