import '../../../globals.js';
import { alpenglow } from '../imports.js';
export default class RenderableFace {
    constructor(face, renderProgram, bounds) {
        this.face = face;
        this.renderProgram = renderProgram;
        this.bounds = bounds;
    }
    // TODO: FIND A BETTER STRATEGY rather than multiple full iteration for every RenderProgram that gets split!!!!
    split() {
        // We're going to record which programs are fully split. If they are still present in the tree, we'll know we
        // already split them.
        const fullySplitRenderPrograms = new Set();
        let faces = [this];
        const getNextSplittable = (faces) => {
            for (let i = 0; i < faces.length; i++) {
                const face = faces[i];
                let result = null;
                face.renderProgram.depthFirst(subProgram => {
                    // TODO: early exit?
                    if (!fullySplitRenderPrograms.has(subProgram) && subProgram.isSplittable()) {
                        result = subProgram;
                    }
                });
                if (result) {
                    return result;
                }
            }
            return null;
        };
        let splitProgram;
        // eslint-disable-next-line no-cond-assign
        while (splitProgram = getNextSplittable(faces)) {
            const newFaces = [];
            for (let i = 0; i < faces.length; i++) {
                const face = faces[i];
                if (face.renderProgram.containsRenderProgram(splitProgram)) {
                    newFaces.push(...splitProgram.split(face));
                }
                else {
                    newFaces.push(face);
                }
            }
            fullySplitRenderPrograms.add(splitProgram);
            faces = newFaces;
        }
        return faces;
    }
}
alpenglow.register('RenderableFace', RenderableFace);
//# sourceMappingURL=RenderableFace.js.map