"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerializeHideableObjectsImp = exports.SerializeHideableObjects = void 0;
exports.makeSerializeHideableObjects = makeSerializeHideableObjects;
const Hideable_1 = require("../Entities/Hideable");
const StateMachineEntity_1 = require("../../StateMachine/Entities/StateMachineEntity");
const core_1 = require("@vived/core");
class SerializeHideableObjects extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "hideable-objects";
    }
}
exports.SerializeHideableObjects = SerializeHideableObjects;
SerializeHideableObjects.type = "SerializeHideableObjects";
function makeSerializeHideableObjects(appObject) {
    return new SerializeHideableObjectsImp(appObject);
}
class SerializeHideableObjectsImp extends SerializeHideableObjects {
    getSystemState() {
        const hiddenObjects = [];
        const allHideables = this.appObjects.getAllComponents(Hideable_1.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_1.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_1.Hideable.addIfMissing(appObject);
            hideable.isHidden = true;
        });
    }
    applyDefaultState() {
        const allHideables = this.appObjects.getAllComponents(Hideable_1.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_1.StateMachineEntity.type);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
exports.SerializeHideableObjectsImp = SerializeHideableObjectsImp;
//# sourceMappingURL=SerializeHideableObjects.js.map