"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerializeCustomLabelsImp = exports.SerializeCustomLabels = void 0;
exports.makeSerializeCustomLabels = makeSerializeCustomLabels;
const core_1 = require("@vived/core");
const StateMachine_1 = require("../../StateMachine");
const Entities_1 = require("../Entities");
class SerializeCustomLabels extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "serialize-custom-labels";
    }
}
exports.SerializeCustomLabels = SerializeCustomLabels;
SerializeCustomLabels.type = "SerializeCustomLabels";
function makeSerializeCustomLabels(appObject) {
    return new SerializeCustomLabelsImp(appObject);
}
class SerializeCustomLabelsImp extends SerializeCustomLabels {
    getSystemState() {
        const customLabels = [];
        const allLabels = this.appObjects.getAllComponents(Entities_1.LabelEntity.type);
        allLabels.forEach((label) => {
            if (label.hasCustomLabel)
                customLabels.push({
                    objectId: label.appObject.id,
                    customLabel: label.custom,
                });
        });
        const state = {
            customLabels,
        };
        return state;
    }
    applySystemState(state, version, durationS) {
        if (version === 1) {
            return this.applyStateV1(state, durationS);
        }
        else {
            this.applyDefaultState();
        }
    }
    applyStateV1(state, durationS) {
        const { customLabels } = state;
        if (!customLabels || customLabels.length === 0) {
            this.applyDefaultState();
            return;
        }
        // Create a Map for faster lookups
        const customLabelsLookup = new Map();
        customLabels.forEach((customLabel) => {
            customLabelsLookup.set(customLabel.objectId, customLabel.customLabel);
        });
        const allLabels = this.appObjects.getAllComponents(Entities_1.LabelEntity.type);
        allLabels.forEach((label) => {
            const appObjectId = label.appObject.id;
            label.custom = customLabelsLookup.get(appObjectId) ?? "";
        });
        customLabelsLookup.forEach((customLabel, objectId) => {
            const appObject = this.appObjects.getOrCreate(objectId);
            const label = Entities_1.LabelEntity.addIfMissing(appObject);
            label.custom = customLabel;
        });
    }
    applyDefaultState() {
        const allLabels = this.appObjects.getAllComponents(Entities_1.LabelEntity.type);
        allLabels.forEach((label) => {
            label.custom = "";
        });
    }
    constructor(appObj) {
        super(appObj, SerializeCustomLabels.type);
        this.seriliziationVersion = 1;
        const stateMachine = this.appObjects.getSingleton(StateMachine_1.StateMachineEntity.type);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
exports.SerializeCustomLabelsImp = SerializeCustomLabelsImp;
//# sourceMappingURL=SerializeCustomLabels.js.map