import { create } from "zustand";
import { immer } from "zustand/middleware/immer";
import * as THREE from "three";

type SceneStore = {
    limitFps: boolean;
    fps: number;
    camState: {
        position: THREE.Vector3;
        target: THREE.Vector3;
    };

    camType: "orthographic" | "perspective";

    setLimitFps: (limitFps: boolean) => void;
    setFps: (fps: number) => void;
    setCamera: (pos: THREE.Vector3, target: THREE.Vector3) => void;

    setCamType: (type: "orthographic" | "perspective") => void;
};

export const useSceneStore = create<SceneStore>()(
    immer((set) => ({
        limitFps: false,
        fps: 60,
        camState: {
            position: new THREE.Vector3(0, 5, 10),
            target: new THREE.Vector3(0, 0, 0),
        },

        camType: "perspective",

        setLimitFps: (limitFps) => {
            set((state) => {
                state.limitFps = limitFps;
            });
        },

        setFps: (fps) => {
            set((state) => {
                state.fps = fps;
            });
        },

        setCamera: (pos, target) => {
            set((state) => {
                state.camState.position.copy(pos);

                state.camState.target.copy(target);
            });
        },

        setCamType: (type) => {
            set((state) => {
                state.camType = type;
            });
        },
    }))
);
