import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { DispatchStateChangeUC, ShowSelectModelUC } from "../../Dispatcher";
import { SceneRootEntity } from "../../SceneRoot";
import { ChannelModelRepo } from "../Entities";
import { LoadChannelModelUC } from "./LoadChannelModelUC";
export class SelectChannelModelUC extends AppObjectUC {
}
SelectChannelModelUC.type = "SelectChannelModelUC";
SelectChannelModelUC.get = (appObjects) => getSingletonComponent(SelectChannelModelUC.type, appObjects);
export function makeSelectChannelModelUC(appObject) {
    return new SelectChannelModelUCImp(appObject);
}
class SelectChannelModelUCImp extends SelectChannelModelUC {
    get showSelectModel() {
        return this.getCachedSingleton(ShowSelectModelUC.type);
    }
    get loadChannelModel() {
        return this.getCachedSingleton(LoadChannelModelUC.type)
            ?.loadChannelModel;
    }
    get sceneRoot() {
        return this.getCachedSingleton(SceneRootEntity.type);
    }
    get repo() {
        return this.getCachedSingleton(ChannelModelRepo.type);
    }
    get dispatchState() {
        return this.getCachedSingleton(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