import { OverlayColor, OverlayColorEnums } from "../Entities/OverlayColor";
import { StateMachineEntity, } from "../../StateMachine/Entities/StateMachineEntity";
import { AppObjectUC } from "@vived/core";
export class SerializeOverlayColoredObjects extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "overlaycolored-objects";
    }
}
SerializeOverlayColoredObjects.type = "SerializeOverlayColoredObjects";
export function makeSerializeOverlayColoredObjects(appObject) {
    return new SerializeOverlayColoredObjectsImp(appObject);
}
export class SerializeOverlayColoredObjectsImp extends SerializeOverlayColoredObjects {
    getSystemState() {
        const overlayColoredObjects = [];
        const allOverlayColors = this.appObjects.getAllComponents(OverlayColor.type);
        allOverlayColors.forEach((overlayColor) => {
            if (overlayColor.overlayColor !== 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.type);
        allOverlayColors.forEach((overlayColor) => {
            const appObjectId = overlayColor.appObject.id;
            overlayColor.overlayColor =
                overlayLookup.get(appObjectId) ?? OverlayColorEnums.NONE;
        });
        overlayLookup.forEach((desiredColor, remainingObjectId) => {
            const appObject = this.appObjects.getOrCreate(remainingObjectId);
            const overlayColor = OverlayColor.addIfMissing(appObject);
            overlayColor.overlayColor = desiredColor;
        });
    }
    applyDefaultState() {
        const allOverlayColors = this.appObjects.getAllComponents(OverlayColor.type);
        allOverlayColors.forEach((overlayColor) => {
            overlayColor.overlayColor = OverlayColorEnums.NONE;
        });
    }
    constructor(appObj) {
        super(appObj, SerializeOverlayColoredObjects.type);
        this.seriliziationVersion = 1;
        const stateMachine = this.appObjects.getSingleton(StateMachineEntity.type);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
//# sourceMappingURL=SerializeOverlayColoredObjects.js.map