import { AppObjectUC } from "@vived/core";
import { Quaternion, Vector3 } from "@vived/core";
import { StateMachineEntity, } from "../../StateMachine/Entities/StateMachineEntity";
import { StoredView } from "../Entities/StoredView";
import { RestoreStoredViewUC } from "./RestoreStoredViewUC";
export class SerializeStoredView extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "storedView";
    }
}
SerializeStoredView.type = "SerializeStoredView";
export function makeSerializeStoredView(appObject) {
    return new SerializeStoredViewImp(appObject);
}
class SerializeStoredViewImp extends SerializeStoredView {
    get storedView() {
        return this.getCachedSingleton(StoredView.type);
    }
    get restoreStoredViewUC() {
        return this.getCachedSingleton(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 = Vector3.Zero();
        this.storedView.rotation = 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 = Vector3.FromDTO(data.position);
        const rot = 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.get(appObject.appObjectRepo);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
//# sourceMappingURL=SerializeStoredView.js.map