"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerializeTransparentObjectsImp = exports.SerializeTransparentObjects = void 0;
exports.makeSerializeTransparentObjects = makeSerializeTransparentObjects;
const core_1 = require("@vived/core");
const StateMachine_1 = require("../../StateMachine");
const Entities_1 = require("../Entities");
class SerializeTransparentObjects extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "transparent-objects";
    }
}
exports.SerializeTransparentObjects = SerializeTransparentObjects;
SerializeTransparentObjects.type = "SerializeTransparentObjects";
function makeSerializeTransparentObjects(appObject) {
    return new SerializeTransparentObjectsImp(appObject);
}
class SerializeTransparentObjectsImp extends SerializeTransparentObjects {
    getSystemState() {
        const transparentObjects = [];
        const allTransparents = this.appObjects.getAllComponents(Entities_1.Transparency.type);
        allTransparents.forEach((transparent) => {
            if (transparent.isTransparent)
                transparentObjects.push(transparent.appObject.id);
        });
        const state = {
            transparentObjects,
        };
        return state;
    }
    applySystemState(state, version, durationS) {
        if (version === 1) {
            return this.applyStateV1(state, durationS);
        }
        else {
            this.applyDefaultState();
        }
    }
    applyStateV1(state, durationS) {
        const { transparentObjects } = state;
        let transparentObjectsIds = [];
        if (transparentObjects !== undefined && transparentObjects.length > 0) {
            transparentObjectsIds = [...transparentObjects];
        }
        else {
            this.applyDefaultState();
            return;
        }
        const allTransparents = this.appObjects.getAllComponents(Entities_1.Transparency.type);
        allTransparents.forEach((transparent) => {
            const appObjectId = transparent.appObject.id;
            if (transparentObjectsIds.includes(appObjectId)) {
                transparent.isTransparent = true;
                const index = transparentObjectsIds.indexOf(appObjectId);
                if (index !== -1) {
                    transparentObjectsIds.splice(index, 1);
                }
            }
        });
        transparentObjectsIds.forEach((remainingObjectId) => {
            const appObject = this.appObjects.getOrCreate(remainingObjectId);
            const hideable = Entities_1.Transparency.addIfMissing(appObject);
            hideable.isTransparent = true;
        });
    }
    applyDefaultState() {
        const allTransparents = this.appObjects.getAllComponents(Entities_1.Transparency.type);
        allTransparents.forEach((transparent) => {
            transparent.isTransparent = false;
        });
    }
    constructor(appObj) {
        super(appObj, SerializeTransparentObjects.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.SerializeTransparentObjectsImp = SerializeTransparentObjectsImp;
//# sourceMappingURL=SerializeTransparentObjects.js.map