"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerializeActiveModelImp = exports.SerializeActiveModel = void 0;
exports.makeSerializeActiveModel = makeSerializeActiveModel;
const core_1 = require("@vived/core");
const StateMachine_1 = require("../../StateMachine");
const Entities_1 = require("../Entities");
const LoadChannelModelUC_1 = require("./LoadChannelModelUC");
const SelectChannelModelUC_1 = require("./SelectChannelModelUC");
class SerializeActiveModel extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "active-model";
    }
}
exports.SerializeActiveModel = SerializeActiveModel;
SerializeActiveModel.type = "SerializeDemoState";
function makeSerializeActiveModel(appObject) {
    return new SerializeActiveModelImp(appObject);
}
class SerializeActiveModelImp extends SerializeActiveModel {
    get repo() {
        return this.getCachedSingleton(Entities_1.ChannelModelRepo.type);
    }
    get stateMachine() {
        return this.getCachedSingleton(StateMachine_1.StateMachineEntity.type);
    }
    get loadChannelModel() {
        return this.getCachedSingleton(LoadChannelModelUC_1.LoadChannelModelUC.type)
            ?.loadChannelModel;
    }
    get showSelectModel() {
        return this.getCachedSingleton(SelectChannelModelUC_1.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);
    }
}
exports.SerializeActiveModelImp = SerializeActiveModelImp;
//# sourceMappingURL=SerializeActiveModel.js.map