import { AppObjectUC } from "@vived/core";
import { StateMachineEntity, } from "../../StateMachine/Entities";
import { LandmarkEntity } from "../Entities/LandmarkEntity";
import { LandmarkGlobals } from "../Entities/LandmarkGlobals";
import { LandmarkRepo } from "../Entities/LandmarkRepo";
export class SerializeLandmarks extends AppObjectUC {
    get globals() {
        return this.getCachedSingleton(LandmarkGlobals.type);
    }
    getSystemState() {
        const allLandmarks = this.appObjects.getAllComponents(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.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.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.getByID(landmark.parentID, this.appObjects);
        if (repo) {
            repo.deleteLandmark(landmark.appObject.id);
        }
    }
    applyDefaultState() {
        const allLandmarks = this.appObjects.getAllComponents(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(StateMachineEntity.type);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
SerializeLandmarks.type = "SerializeLandmarks";
//# sourceMappingURL=SerializeLandmarks.js.map