import { AppObjectUC } from "@vived/core";
import { StateMachineEntity, } from "../../StateMachine/Entities/StateMachineEntity";
import { CalloutEntity } from "../Entities/CalloutEntity";
import { CalloutGlobals } from "../Entities/CalloutGlobals";
import { CalloutRepo } from "../Entities/CalloutRepo";
export class SerializeCallouts extends AppObjectUC {
    get calloutGlobal() {
        return this.getCachedSingleton(CalloutGlobals.type);
    }
    getSystemState() {
        const repos = this.appObjects.getAllComponents(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.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.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.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.type);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
SerializeCallouts.type = "SerializeCallouts";
//# sourceMappingURL=SerializeCallouts.js.map