import { get, set } from 'idb-keyval';
import { getContext, setContext } from 'svelte';
const key = Symbol('dashboard-context');
export const RefreshRates = {
    poses: 'poses',
    pointclouds: 'pointclouds',
    vision: 'vision',
};
const defaults = () => ({
    anthropicKey: '',
    cameraMode: 'perspective',
    refreshRates: {
        poses: 1000,
        pointclouds: 5000,
        vision: 1000,
    },
    disabledCameras: {},
    disabledVisionServices: {},
    snapping: false,
    transformMode: 'none',
    grid: true,
    gridCellSize: 0.5,
    gridSectionSize: 10,
    gridFadeDistance: 25,
    pointSize: 0.01,
    pointColor: '#333333',
    lineWidth: 0.005,
    lineDotSize: 0.005,
    interactionMode: 'navigate',
    enableMeasureAxisX: true,
    enableMeasureAxisY: true,
    enableMeasureAxisZ: true,
    enableLabels: false,
    enableArmPositionsWidget: false,
    openCameraWidgets: {},
    openFramePovWidgets: {},
    renderStats: false,
    renderArmModels: 'colliders+model',
    renderSubEntityHoverDetail: false,
    enableXR: false,
    xrMode: 'frame-configure',
    xrController: {
        left: {
            scaleFactor: 1,
            rotationEnabled: true,
        },
        right: {
            scaleFactor: 1,
            rotationEnabled: true,
        },
    },
});
export const provideSettings = () => {
    let isLoaded = $state(false);
    let settings = $state(defaults());
    get('motion-tools-settings')
        .then((response) => {
        if (response) {
            settings = { ...settings, ...response };
        }
    })
        .finally(() => {
        isLoaded = true;
    });
    $effect(() => {
        if (isLoaded) {
            set('motion-tools-settings', $state.snapshot({ ...settings, interactionMode: 'navigate' }));
        }
    });
    const context = {
        get current() {
            return settings;
        },
        set current(value) {
            settings = value;
        },
        get isLoaded() {
            return isLoaded;
        },
        merge(value) {
            settings = { ...settings, ...value };
        },
    };
    setContext(key, context);
    return context;
};
export const useSettings = () => {
    return getContext(key);
};
