import { VolumetricLoader } from "../utils/VolumetricLoader";
import { Constants } from "../Constants";
import { DisplayMode } from "../enums";
export class AlVolume {
    static get Object() {
        return {
            schema: {
                srcLoaded: { type: "boolean" },
                src: { type: "string" },
                displayMode: { type: "string" },
                slicesIndex: { type: "number" },
                slicesOrientation: { type: "string" },
                slicesWindowWidth: { type: "number" },
                slicesWindowCenter: { type: "number" },
                volumeSteps: { type: "number" },
                volumeWindowWidth: { type: "number" },
                volumeWindowCenter: { type: "number" }
            },
            bindMethods() {
                this.handleStack = this.handleStack.bind(this);
            },
            init() {
                this.tickFunction = AFRAME.utils.throttle(this.tickFunction, Constants.minFrameMS, this);
                this.loader = new VolumetricLoader();
                this.state = {};
                this.bindMethods();
            },
            handleStack(stack) {
                const state = this.state;
                const el = this.el;
                state.stack = stack;
                switch (this.data.displayMode) {
                    case DisplayMode.SLICES: {
                        state.stackhelper = new AMI.StackHelper(state.stack);
                        state.stackhelper.bbox.visible = false;
                        state.stackhelper.border.color = Constants.colorValues.blue;
                        break;
                    }
                    case DisplayMode.VOLUME: {
                        const lutCanvases = el.sceneEl.parentEl.querySelector("#lut-canvases");
                        state.lutHelper = new AMI.LutHelper(lutCanvases);
                        state.lutHelper.luts = AMI.LutHelper.presetLuts();
                        state.lutHelper.lutsO = AMI.LutHelper.presetLutsO();
                        state.stackhelper = new AMI.VolumeRenderingHelper(state.stack);
                        state.stackhelper.uniforms.uTextureLUT.value =
                            state.lutHelper.texture;
                        state.stackhelper.uniforms.uLut.value = 1;
                        break;
                    }
                }
                el.sceneEl.emit(AlVolumeEvents.LOADED, state.stackhelper, false);
            },
            update(oldData) {
                const state = this.state;
                const el = this.el;
                if (!this.data.src) {
                    return;
                }
                else if (oldData && oldData.src !== this.data.src) {
                    this.loader.load(this.data.src, el).then(stack => {
                        this.handleStack(stack);
                    });
                }
                else if (oldData &&
                    oldData.displayMode !== this.data.displayMode &&
                    state.stack) {
                    this.handleStack(state.stack);
                }
            },
            tickFunction() {
                if (this.state.stackhelper) {
                    this.el.setObject3D("mesh", this.state.stackhelper);
                }
            },
            tick() {
                this.tickFunction();
            },
            remove() {
                this.el.removeObject3D("mesh");
            }
        };
    }
    static get Tag() {
        return "al-volume";
    }
}
export class AlVolumeEvents {
}
AlVolumeEvents.LOADED = "al-volume-loaded";
AlVolumeEvents.ERROR = "al-volume-error";
