"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerializeOverlayColoredObjectsImp = exports.SerializeOverlayColoredObjects = void 0;
exports.makeSerializeOverlayColoredObjects = makeSerializeOverlayColoredObjects;
const OverlayColor_1 = require("../Entities/OverlayColor");
const StateMachineEntity_1 = require("../../StateMachine/Entities/StateMachineEntity");
const core_1 = require("@vived/core");
class SerializeOverlayColoredObjects extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "overlaycolored-objects";
    }
}
exports.SerializeOverlayColoredObjects = SerializeOverlayColoredObjects;
SerializeOverlayColoredObjects.type = "SerializeOverlayColoredObjects";
function makeSerializeOverlayColoredObjects(appObject) {
    return new SerializeOverlayColoredObjectsImp(appObject);
}
class SerializeOverlayColoredObjectsImp extends SerializeOverlayColoredObjects {
    getSystemState() {
        const overlayColoredObjects = [];
        const allOverlayColors = this.appObjects.getAllComponents(OverlayColor_1.OverlayColor.type);
        allOverlayColors.forEach((overlayColor) => {
            if (overlayColor.overlayColor !== OverlayColor_1.OverlayColorEnums.NONE)
                overlayColoredObjects.push({
                    id: overlayColor.appObject.id,
                    overlayColor: overlayColor.overlayColor,
                });
        });
        const state = {
            overlayColoredObjects,
        };
        return state;
    }
    applySystemState(state, version, durationS) {
        if (version === 1) {
            return this.applyStateV1(state, durationS);
        }
        else {
            this.applyDefaultState();
        }
    }
    applyStateV1(state, durationS) {
        const { overlayColoredObjects } = state;
        if (!overlayColoredObjects) {
            this.applyDefaultState();
            return;
        }
        // Create a Map for faster lookups
        const overlayLookup = new Map();
        overlayColoredObjects.forEach((overlayColorObj) => {
            overlayLookup.set(overlayColorObj.id, overlayColorObj.overlayColor);
        });
        const allOverlayColors = this.appObjects.getAllComponents(OverlayColor_1.OverlayColor.type);
        allOverlayColors.forEach((overlayColor) => {
            const appObjectId = overlayColor.appObject.id;
            overlayColor.overlayColor =
                overlayLookup.get(appObjectId) ?? OverlayColor_1.OverlayColorEnums.NONE;
        });
        overlayLookup.forEach((desiredColor, remainingObjectId) => {
            const appObject = this.appObjects.getOrCreate(remainingObjectId);
            const overlayColor = OverlayColor_1.OverlayColor.addIfMissing(appObject);
            overlayColor.overlayColor = desiredColor;
        });
    }
    applyDefaultState() {
        const allOverlayColors = this.appObjects.getAllComponents(OverlayColor_1.OverlayColor.type);
        allOverlayColors.forEach((overlayColor) => {
            overlayColor.overlayColor = OverlayColor_1.OverlayColorEnums.NONE;
        });
    }
    constructor(appObj) {
        super(appObj, SerializeOverlayColoredObjects.type);
        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.SerializeOverlayColoredObjectsImp = SerializeOverlayColoredObjectsImp;
//# sourceMappingURL=SerializeOverlayColoredObjects.js.map