import '../../../assert/js/assert.js';
import '../../../globals.js';
// Copyright 2023-2024, University of Colorado Boulder
/**
 * RenderProgram for alpha (an opacity) applied to a RenderProgram
 *
 * @author Jonathan Olson <jonathan.olson@colorado.edu>
 */
import { alpenglow, RenderColor, RenderInstructionMultiplyScalar, RenderPathBoolean, RenderProgram } from '../imports.js';
export default class RenderAlpha extends RenderProgram {
    constructor(program, alpha) {
        super([program], alpha === 0 || program.isFullyTransparent, alpha === 1 && program.isFullyOpaque);
        this.program = program;
        this.alpha = alpha;
    }
    getName() {
        return 'RenderAlpha';
    }
    withChildren(children) {
        assert && assert(children.length === 1);
        return new RenderAlpha(children[0], this.alpha);
    }
    equalsTyped(other) {
        return this.alpha === other.alpha;
    }
    getSimplified(children) {
        const program = children[0];
        if (program.isFullyTransparent || this.alpha === 0) {
            return RenderColor.TRANSPARENT;
        }
        else if (this.alpha === 1) {
            return program;
        }
        else if (program instanceof RenderColor) {
            return new RenderColor(program.color.timesScalar(this.alpha));
        }
        // Move our path-booleans "up" to the top level (so we can combine other things AND improve path-boolean replacement performance)
        else if (program instanceof RenderPathBoolean && program.isOneSided()) {
            return program.withOneSide(this.withChildren([program.getOneSide()])).simplified();
        }
        else {
            return null;
        }
    }
    evaluate(context) {
        const source = this.program.evaluate(context);
        return source.timesScalar(this.alpha);
    }
    writeInstructions(instructions) {
        this.program.writeInstructions(instructions);
        instructions.push(new RenderInstructionMultiplyScalar(this.alpha));
    }
    getExtraDebugString() {
        return `${this.alpha}`;
    }
    serialize() {
        return {
            type: 'RenderAlpha',
            program: this.program.serialize(),
            alpha: this.alpha
        };
    }
    static deserialize(obj) {
        return new RenderAlpha(RenderProgram.deserialize(obj.program), obj.alpha);
    }
}
alpenglow.register('RenderAlpha', RenderAlpha);
//# sourceMappingURL=RenderAlpha.js.map