"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerializeStoredView = void 0;
exports.makeSerializeStoredView = makeSerializeStoredView;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const StateMachineEntity_1 = require("../../StateMachine/Entities/StateMachineEntity");
const StoredView_1 = require("../Entities/StoredView");
const RestoreStoredViewUC_1 = require("./RestoreStoredViewUC");
class SerializeStoredView extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "storedView";
    }
}
exports.SerializeStoredView = SerializeStoredView;
SerializeStoredView.type = "SerializeStoredView";
function makeSerializeStoredView(appObject) {
    return new SerializeStoredViewImp(appObject);
}
class SerializeStoredViewImp extends SerializeStoredView {
    get storedView() {
        return this.getCachedSingleton(StoredView_1.StoredView.type);
    }
    get restoreStoredViewUC() {
        return this.getCachedSingleton(RestoreStoredViewUC_1.RestoreStoredViewUC.type);
    }
    getSystemState() {
        if (!this.storedView) {
            console.warn("[SerializeStoredView] No stored view entity");
            return {};
        }
        const data = {
            position: this.storedView.position.dto,
            rotation: this.storedView.rotation.dto,
            scale: this.storedView.scale,
            hasStoredView: this.storedView.hasStoredView,
        };
        return data;
    }
    applySystemState(serializedState, version) {
        try {
            if (version === 1) {
                const castState = serializedState;
                this.applyV1Data(castState);
            }
            else {
                console.warn(`[SerializeStoredView] Unsupported version number ${version}. Defaulting`);
                this.applyDefaultState();
            }
        }
        catch (e) {
            console.warn(`[SerializeStoredView] Caught when deserializing ${e}. Defaulting`);
            this.applyDefaultState();
        }
    }
    applyDefaultState() {
        if (!this.storedView) {
            console.warn("[SerializeStoredView] No stored view entity");
            return;
        }
        this.storedView.position = core_2.Vector3.Zero();
        this.storedView.rotation = core_2.Quaternion.Identity();
        this.storedView.scale = 1;
        this.storedView.hasStoredView = false;
    }
    applyV1Data(data) {
        if (!this.storedView) {
            console.warn("[SerializeStoredView] No stored view entity");
            return;
        }
        const pos = core_2.Vector3.FromDTO(data.position);
        const rot = core_2.Quaternion.FromDTO(data.rotation);
        this.storedView.position = pos;
        this.storedView.rotation = rot;
        this.storedView.scale = data.scale;
        this.storedView.hasStoredView = data.hasStoredView;
        if (data.hasStoredView) {
            this.restoreStoredViewUC?.restoreStoredView();
        }
    }
    constructor(appObject) {
        super(appObject, SerializeStoredView.type);
        this.seriliziationVersion = 1;
        const stateMachine = StateMachineEntity_1.StateMachineEntity.get(appObject.appObjectRepo);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
//# sourceMappingURL=SerializeStoredView.js.map