import { AppObjectUC } from "@vived/core";
import { StateMachineEntity, } from "../../StateMachine/Entities/StateMachineEntity";
import { LabelDropRepo } from "../Entities/LabelDropRepo";
export class SerializeLabelDrop extends AppObjectUC {
    get labelDrop() {
        return this.getCachedSingleton(LabelDropRepo.type);
    }
    getSystemState() {
        if (!this.labelDrop)
            return {};
        const labelDrops = this.labelDrop
            .getAll()
            .map((dropItem) => {
            return {
                id: dropItem.id,
                label: dropItem.labelEntity.custom,
                correctAppObjectIDs: dropItem.targetIds,
            };
        });
        const state = {
            labelDrops,
        };
        return state;
    }
    applySystemState(state, version) {
        if (version === 1) {
            this.applyStateV1(state);
        }
        else {
            this.applyDefaultState();
        }
    }
    applyStateV1(state) {
        if (!this.labelDrop)
            return;
        if (state.labelDrops === undefined) {
            this.applyDefaultState();
            return;
        }
        this.labelDrop.deleteAllItems();
        state.labelDrops.forEach((dropItem) => {
            const item = this.labelDrop.makeLabelDropItem(dropItem.id);
            item.labelEntity.custom = dropItem.label;
            item.targetIds = dropItem.correctAppObjectIDs;
        });
    }
    applyDefaultState() {
        if (!this.labelDrop)
            return;
        this.labelDrop.deleteAllItems();
    }
    constructor(appObject) {
        super(appObject, SerializeLabelDrop.type);
        this.name = "label-drop";
        this.seriliziationVersion = 1;
        const stateMachine = StateMachineEntity.get(appObject.appObjectRepo);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.error("Unable to find StateMachineEntity");
        }
    }
}
SerializeLabelDrop.type = "SerializeLabelDrop";
//# sourceMappingURL=SerializeLabelDrop.js.map