import { AppObjectUC } from "@vived/core";
import { StateMachineEntity } from "../../StateMachine";
import { ChannelModelRepo } from "../Entities";
import { LoadChannelModelUC } from "./LoadChannelModelUC";
import { SelectChannelModelUC } from "./SelectChannelModelUC";
export class SerializeActiveModel extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "active-model";
    }
}
SerializeActiveModel.type = "SerializeDemoState";
export function makeSerializeActiveModel(appObject) {
    return new SerializeActiveModelImp(appObject);
}
export class SerializeActiveModelImp extends SerializeActiveModel {
    get repo() {
        return this.getCachedSingleton(ChannelModelRepo.type);
    }
    get stateMachine() {
        return this.getCachedSingleton(StateMachineEntity.type);
    }
    get loadChannelModel() {
        return this.getCachedSingleton(LoadChannelModelUC.type)
            ?.loadChannelModel;
    }
    get showSelectModel() {
        return this.getCachedSingleton(SelectChannelModelUC.type)?.selectChannelModel;
    }
    getSystemState() {
        if (!this.repo)
            return {};
        const appState = {
            modelID: this.repo.activeModel,
        };
        return appState;
    }
    applySystemState(state, version) {
        if (version === 1) {
            this.applyStateV1(state);
        }
        else {
            // Unsupported version
            this.warn(`Unsupported data version ${version} for serialized system ${this.name}. Reverting to default state`);
            this.applyDefaultState();
        }
    }
    applyStateV1(state) {
        const { modelID } = state;
        if (!modelID) {
            this.applyDefaultState();
            return;
        }
        if (!this.repo || !this.loadChannelModel) {
            return;
        }
        this.repo.activeModel = modelID;
        this.loadChannelModel(modelID);
    }
    applyDefaultState() {
        if (!this.repo || !this.showSelectModel) {
            return;
        }
        this.repo.activeModel = "";
        this.showSelectModel();
    }
    constructor(appObject) {
        super(appObject, SerializeActiveModel.type);
        this.seriliziationVersion = 1;
        this.stateMachine?.registerSerializedSystem(this);
    }
}
//# sourceMappingURL=SerializeActiveModel.js.map