"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetEnvironmentIntensityUC = void 0;
exports.makeSetEnvironmentIntensityUC = makeSetEnvironmentIntensityUC;
const core_1 = require("@vived/core");
const Dispatcher_1 = require("../../Dispatcher");
const UndoRedo_1 = require("../../UndoRedo");
const Environment_1 = require("../Entities/Environment");
class SetEnvironmentIntensityUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(SetEnvironmentIntensityUC.type, appObjects);
    }
}
exports.SetEnvironmentIntensityUC = SetEnvironmentIntensityUC;
SetEnvironmentIntensityUC.type = "SetEnvironmentIntensityUC";
function makeSetEnvironmentIntensityUC(appObject) {
    return new SetEnvironmentIntensityUCImp(appObject);
}
class SetEnvironmentIntensityUCImp extends SetEnvironmentIntensityUC {
    get environment() {
        return this.getCachedSingleton(Environment_1.EnvironmentEntity.type);
    }
    get undo() {
        return this.getCachedSingleton(UndoRedo_1.UndoRedoEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(Dispatcher_1.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