"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetTexturalEnvironmentUC = void 0;
exports.makeSetTexturalEnvironmentUC = makeSetTexturalEnvironmentUC;
const core_1 = require("@vived/core");
const Dispatcher_1 = require("../../Dispatcher");
const UndoRedo_1 = require("../../UndoRedo");
const Environment_1 = require("../Entities/Environment");
class SetTexturalEnvironmentUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(SetTexturalEnvironmentUC.type, appObjects);
    }
}
exports.SetTexturalEnvironmentUC = SetTexturalEnvironmentUC;
SetTexturalEnvironmentUC.type = "SetTexturalEnvironmentUC";
function makeSetTexturalEnvironmentUC(appObject) {
    return new SetTexturalEnvironmentUCImp(appObject);
}
class SetTexturalEnvironmentUCImp extends SetTexturalEnvironmentUC {
    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;
    }
    constructor(appObject) {
        super(appObject, SetTexturalEnvironmentUC.type);
        this.setEnvironment = (env) => {
            const dispatchStateChange = this.dispatchStateChange;
            const environment = this.environment;
            if (!environment || !dispatchStateChange) {
                this.error("Missing component");
                return;
            }
            if (environment.currentEnvironment === env)
                return;
            const previousEnv = environment.currentEnvironment;
            environment.currentEnvironment = env;
            this.undo?.submitAction({
                undoAction: () => {
                    environment.currentEnvironment = previousEnv;
                    dispatchStateChange();
                },
                redoAction: () => {
                    environment.currentEnvironment = env;
                    dispatchStateChange();
                },
            });
            dispatchStateChange();
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=SetTexturalEnvironmentUC.js.map