import { promiseTextureLoaded } from "./promiseTextureLoaded.js";
import { traverseMaterialTextures } from "../../asset/loaders/material/traverseMaterialTextures.js";

/**
 *
 * @param {Material} material
 * @returns {<Promise<Texture[]>}
 */
export function promiseMaterialLoaded(material) {
    let textures = [];

    function processPrimitiveMaterial(mat) {
        traverseMaterialTextures(mat, Array.prototype.push, textures);
    }

    function processMaterial(mat) {
        if (mat.materials !== undefined) {
            mat.materials.forEach(processMaterial);
        } else {
            processPrimitiveMaterial(mat);
        }
    }

    processMaterial(material);

    const promises = textures.map(promiseTextureLoaded);
    return Promise.all(promises);
}
