"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectChannelModelUC = void 0;
exports.makeSelectChannelModelUC = makeSelectChannelModelUC;
const core_1 = require("@vived/core");
const Dispatcher_1 = require("../../Dispatcher");
const SceneRoot_1 = require("../../SceneRoot");
const Entities_1 = require("../Entities");
const LoadChannelModelUC_1 = require("./LoadChannelModelUC");
class SelectChannelModelUC extends core_1.AppObjectUC {
}
exports.SelectChannelModelUC = SelectChannelModelUC;
SelectChannelModelUC.type = "SelectChannelModelUC";
SelectChannelModelUC.get = (appObjects) => (0, core_1.getSingletonComponent)(SelectChannelModelUC.type, appObjects);
function makeSelectChannelModelUC(appObject) {
    return new SelectChannelModelUCImp(appObject);
}
class SelectChannelModelUCImp extends SelectChannelModelUC {
    get showSelectModel() {
        return this.getCachedSingleton(Dispatcher_1.ShowSelectModelUC.type);
    }
    get loadChannelModel() {
        return this.getCachedSingleton(LoadChannelModelUC_1.LoadChannelModelUC.type)
            ?.loadChannelModel;
    }
    get sceneRoot() {
        return this.getCachedSingleton(SceneRoot_1.SceneRootEntity.type);
    }
    get repo() {
        return this.getCachedSingleton(Entities_1.ChannelModelRepo.type);
    }
    get dispatchState() {
        return this.getCachedSingleton(Dispatcher_1.DispatchStateChangeUC.type)?.doDispatch;
    }
    constructor(appObject) {
        super(appObject, SelectChannelModelUC.type);
        this.selectChannelModel = () => {
            const showSelectModel = this.showSelectModel;
            const loadChannelModel = this.loadChannelModel;
            const sceneRoot = this.sceneRoot;
            const repo = this.repo;
            if (!showSelectModel || !loadChannelModel || !sceneRoot || !repo) {
                return Promise.reject(new Error("Missing Components"));
            }
            return showSelectModel
                .doDispatch()
                .then((resp) => {
                return loadChannelModel(resp.modelId);
            })
                .then((channelModel) => {
                sceneRoot.position = channelModel.defaultPosition;
                sceneRoot.rotation = channelModel.defaultRotation;
                sceneRoot.scale = channelModel.defaultScale;
                repo.activeModel = channelModel.id;
                repo.getAll().forEach((model) => {
                    model.enabled = model.id === channelModel.id;
                });
                if (this.dispatchState) {
                    this.dispatchState();
                }
                return channelModel.id;
            });
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=SelectChannelModelUC.js.map