import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { DispatchStateChangeUC } from "../../Dispatcher";
import { UndoRedoEntity } from "../../UndoRedo";
import { EnvironmentEntity } from "../Entities/Environment";
export class SetEnvironmentIntensityUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(SetEnvironmentIntensityUC.type, appObjects);
    }
}
SetEnvironmentIntensityUC.type = "SetEnvironmentIntensityUC";
export function makeSetEnvironmentIntensityUC(appObject) {
    return new SetEnvironmentIntensityUCImp(appObject);
}
class SetEnvironmentIntensityUCImp extends SetEnvironmentIntensityUC {
    get environment() {
        return this.getCachedSingleton(EnvironmentEntity.type);
    }
    get undo() {
        return this.getCachedSingleton(UndoRedoEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC.type)?.doDispatch;
    }
    submit() {
        if (!this.initialIntensity)
            return;
        const dispatchStateChange = this.dispatchStateChange;
        const environment = this.environment;
        if (!environment || !dispatchStateChange) {
            this.error("Missing component");
            return;
        }
        const undoIntensity = this.initialIntensity;
        const redoIntensity = environment.intensity;
        if (undoIntensity === redoIntensity)
            return;
        this.undo?.submitAction({
            undoAction: () => {
                environment.intensity = undoIntensity;
                dispatchStateChange();
            },
            redoAction: () => {
                environment.intensity = redoIntensity;
                dispatchStateChange();
            },
        });
        dispatchStateChange();
        this.initialIntensity = undefined;
    }
    constructor(appObject) {
        super(appObject, SetEnvironmentIntensityUC.type);
        this.update = (val) => {
            if (!this.environment)
                return;
            if (!this.initialIntensity) {
                this.initialIntensity = this.environment.intensity;
            }
            this.environment.intensity = val;
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=SetEnvironmentIntensityUC.js.map