"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerializeCallouts = void 0;
const core_1 = require("@vived/core");
const StateMachineEntity_1 = require("../../StateMachine/Entities/StateMachineEntity");
const CalloutEntity_1 = require("../Entities/CalloutEntity");
const CalloutGlobals_1 = require("../Entities/CalloutGlobals");
const CalloutRepo_1 = require("../Entities/CalloutRepo");
class SerializeCallouts extends core_1.AppObjectUC {
    get calloutGlobal() {
        return this.getCachedSingleton(CalloutGlobals_1.CalloutGlobals.type);
    }
    getSystemState() {
        const repos = this.appObjects.getAllComponents(CalloutRepo_1.CalloutRepo.type);
        const callouts = [];
        repos.forEach((repo) => {
            repo.getAll().forEach((callout) => {
                callouts.push(callout.data);
            });
        });
        return {
            callouts,
        };
    }
    applySystemState(state, version) {
        if (version === 1) {
            this.handleStateVersion1(state);
        }
        else {
            this.warn("Unsupported state version. Reverting to default");
            this.applyDefaultState();
        }
    }
    handleStateVersion1(state) {
        const { callouts } = state;
        if (!callouts) {
            this.warn("Unable to parse state. Reverting to default");
            this.applyDefaultState();
            return;
        }
        const previousCalloutLookup = new Map();
        const allCallouts = this.appObjects.getAllComponents(CalloutEntity_1.CalloutEntity.type);
        allCallouts.forEach((callout) => {
            previousCalloutLookup.set(callout.appObject.id, callout);
        });
        callouts.forEach((calloutData) => {
            let callout = previousCalloutLookup.get(calloutData.id);
            if (callout) {
                previousCalloutLookup.delete(calloutData.id);
            }
            else {
                let repo = CalloutRepo_1.CalloutRepo.getById(calloutData.parentID, this.appObjects);
                if (!repo) {
                    repo = this.calloutGlobal?.calloutRepoFactory(calloutData.parentID);
                }
                callout = repo?.makeCallout(calloutData.id);
            }
            if (!callout) {
                this.warn("Unable to create or find callout");
                return;
            }
            callout.data = calloutData;
        });
        previousCalloutLookup.forEach((callout) => {
            callout.inRepo?.deleteCallout(callout.appObject.id);
        });
    }
    applyDefaultState() {
        const repos = this.appObjects.getAllComponents(CalloutRepo_1.CalloutRepo.type);
        repos.forEach((repo) => {
            const callouts = repo.getAll();
            callouts.forEach((callout) => {
                callout.inRepo?.deleteCallout(callout.appObject.id);
            });
        });
    }
    constructor(appObj) {
        super(appObj, SerializeCallouts.type);
        this.name = "callouts";
        this.seriliziationVersion = 1;
        const stateMachine = this.appObjects.getSingleton(StateMachineEntity_1.StateMachineEntity.type);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
exports.SerializeCallouts = SerializeCallouts;
SerializeCallouts.type = "SerializeCallouts";
//# sourceMappingURL=SerializeCallouts.js.map