import { getContext, setContext } from 'svelte';
const TRANSFORM_CONTROLS_KEY = Symbol('tranform-controls-context');
const CAMERA_CONTROLS_KEY = Symbol('camera-controls-context');
export const provideCameraControls = (initialCameraPose) => {
    let controls = $state.raw();
    const setPose = (pose, animate = false) => {
        const [x, y, z] = pose.position;
        const [lookAtX, lookAtY, lookAtZ] = pose.lookAt;
        if (controls && 'setPosition' in controls) {
            controls.setPosition(x, y, z, animate);
            controls.setLookAt(x, y, z, lookAtX, lookAtY, lookAtZ, animate);
        }
    };
    const setZoom = (zoom) => {
        if (controls && 'zoomTo' in controls)
            controls?.zoomTo(zoom);
    };
    const setInitialPose = () => {
        if (controls && 'setPosition' in controls) {
            const pose = initialCameraPose();
            setPose(pose ?? { position: [3, 3, 3], lookAt: [0, 0, 0] }, true);
        }
        else if (controls) {
            controls.reset();
        }
    };
    $effect(() => {
        const pose = initialCameraPose();
        if (pose) {
            setPose(pose);
        }
    });
    setContext(CAMERA_CONTROLS_KEY, {
        get current() {
            return controls;
        },
        set(current) {
            controls = current;
        },
        setPose,
        setInitialPose,
        setZoom,
    });
};
export const useCameraControls = () => {
    return getContext(CAMERA_CONTROLS_KEY);
};
export const provideTransformControls = () => {
    let active = $state(false);
    setContext(TRANSFORM_CONTROLS_KEY, {
        get active() {
            return active;
        },
        setActive(value) {
            active = value;
        },
    });
};
export const useTransformControls = () => {
    return getContext(TRANSFORM_CONTROLS_KEY);
};
