import { Hideable } from "../Entities/Hideable";
import { StateMachineEntity, } from "../../StateMachine/Entities/StateMachineEntity";
import { AppObjectUC } from "@vived/core";
export class SerializeHideableObjects extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "hideable-objects";
    }
}
SerializeHideableObjects.type = "SerializeHideableObjects";
export function makeSerializeHideableObjects(appObject) {
    return new SerializeHideableObjectsImp(appObject);
}
export class SerializeHideableObjectsImp extends SerializeHideableObjects {
    getSystemState() {
        const hiddenObjects = [];
        const allHideables = this.appObjects.getAllComponents(Hideable.type);
        allHideables.forEach((hideable) => {
            if (hideable.isHidden && !hideable.ignoreSerialization)
                hiddenObjects.push(hideable.appObject.id);
        });
        const state = {
            hiddenObjects,
        };
        return state;
    }
    applySystemState(state, version, durationS) {
        if (version === 1) {
            return this.applyStateV1(state, durationS);
        }
        else {
            this.applyDefaultState();
        }
    }
    applyStateV1(state, durationS) {
        const { hiddenObjects } = state;
        let hiddenObjectsIds = [];
        if (hiddenObjects !== undefined && hiddenObjects.length > 0) {
            hiddenObjectsIds = [...hiddenObjects];
        }
        else {
            this.applyDefaultState();
            return;
        }
        const allHideables = this.appObjects.getAllComponents(Hideable.type);
        allHideables.forEach((hideable) => {
            const appObjectId = hideable.appObject.id;
            if (hiddenObjectsIds.includes(appObjectId)) {
                hideable.isHidden = true;
                const index = hiddenObjectsIds.indexOf(appObjectId);
                if (index !== -1) {
                    hiddenObjectsIds.splice(index, 1);
                }
            }
        });
        hiddenObjectsIds.forEach((remainingObjectId) => {
            const appObject = this.appObjects.getOrCreate(remainingObjectId);
            const hideable = Hideable.addIfMissing(appObject);
            hideable.isHidden = true;
        });
    }
    applyDefaultState() {
        const allHideables = this.appObjects.getAllComponents(Hideable.type);
        allHideables.forEach((hideable) => {
            hideable.isHidden = false;
        });
    }
    constructor(appObj) {
        super(appObj, SerializeHideableObjects.type);
        this.seriliziationVersion = 1;
        this.selectChannelModelUC = () => {
            console.warn("[SerializeHideableObjects] Select Channel Model UC was not setup");
        };
        const stateMachine = this.appObjects.getSingleton(StateMachineEntity.type);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
//# sourceMappingURL=SerializeHideableObjects.js.map