"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerializeLandmarks = void 0;
const core_1 = require("@vived/core");
const Entities_1 = require("../../StateMachine/Entities");
const LandmarkEntity_1 = require("../Entities/LandmarkEntity");
const LandmarkGlobals_1 = require("../Entities/LandmarkGlobals");
const LandmarkRepo_1 = require("../Entities/LandmarkRepo");
class SerializeLandmarks extends core_1.AppObjectUC {
    get globals() {
        return this.getCachedSingleton(LandmarkGlobals_1.LandmarkGlobals.type);
    }
    getSystemState() {
        const allLandmarks = this.appObjects.getAllComponents(LandmarkEntity_1.LandmarkEntity.type);
        const landmarks = [];
        allLandmarks.forEach((landmark) => {
            if (landmark.parentID) {
                landmarks.push(landmark.data);
            }
        });
        return {
            landmarks,
        };
    }
    applySystemState(state, version) {
        if (version === 1) {
            this.handleStateVersion1(state);
        }
        else {
            this.warn("Unsupported state version. Reverting to default");
            this.applyDefaultState();
        }
    }
    handleStateVersion1(state) {
        const { landmarks } = state;
        if (!landmarks) {
            this.warn("Unable to parse state. Reverting to default");
            this.applyDefaultState();
            return;
        }
        const previousLandmarkLookup = new Map();
        const allLandmarks = this.appObjects.getAllComponents(LandmarkEntity_1.LandmarkEntity.type);
        allLandmarks.forEach((landmark) => {
            if (landmark.parentID) {
                previousLandmarkLookup.set(landmark.appObject.id, landmark);
            }
        });
        landmarks.forEach((landmarkData) => {
            const existing = previousLandmarkLookup.get(landmarkData.id);
            if (existing) {
                existing.data = landmarkData;
                previousLandmarkLookup.delete(landmarkData.id);
                return;
            }
            let repo = this.appObjects.getAppObjectComponent(landmarkData.parentID, LandmarkRepo_1.LandmarkRepo.type);
            if (!repo) {
                repo = this.globals?.landmarkRepoFactory(landmarkData.parentID);
            }
            const landmark = repo.createLandmark(landmarkData.id);
            landmark.data = landmarkData;
        });
        previousLandmarkLookup.forEach((landmark) => {
            this.disposeLandmark(landmark);
        });
    }
    disposeLandmark(landmark) {
        const repo = LandmarkRepo_1.LandmarkRepo.getByID(landmark.parentID, this.appObjects);
        if (repo) {
            repo.deleteLandmark(landmark.appObject.id);
        }
    }
    applyDefaultState() {
        const allLandmarks = this.appObjects.getAllComponents(LandmarkEntity_1.LandmarkEntity.type);
        allLandmarks.forEach((landmark) => {
            if (landmark.parentID) {
                this.disposeLandmark(landmark);
            }
        });
    }
    constructor(appObj) {
        super(appObj, SerializeLandmarks.type);
        this.name = "landmarks";
        this.seriliziationVersion = 1;
        const stateMachine = this.appObjects.getSingleton(Entities_1.StateMachineEntity.type);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
exports.SerializeLandmarks = SerializeLandmarks;
SerializeLandmarks.type = "SerializeLandmarks";
//# sourceMappingURL=SerializeLandmarks.js.map