import { get, set } from 'idb-keyval';
import { getContext, setContext } from 'svelte';
import { SvelteMap } from 'svelte/reactivity';
const key = Symbol('polling-rate-context');
const refreshRatesKey = 'polling-rate';
const disabledCamerasKey = 'disabled-cameras';
const disabledVisionServicesKey = 'disabled-vision-services-object-pointcloud';
export const RefreshRates = {
    poses: 'poses',
    pointclouds: 'pointclouds',
};
const setFromEntries = (map, entries) => {
    if (entries) {
        for (const [key, value] of entries) {
            map.set(key, value);
        }
    }
};
export const provideMachineSettings = () => {
    const refreshRates = new SvelteMap([
        [RefreshRates.poses, 1000],
        [RefreshRates.pointclouds, -1],
    ]);
    const disabledCameras = new SvelteMap();
    const disabledVisionServices = new SvelteMap();
    get(refreshRatesKey).then((entries) => {
        setFromEntries(refreshRates, entries);
    });
    get(disabledCamerasKey).then((entries) => {
        setFromEntries(disabledCameras, entries);
    });
    get(disabledVisionServicesKey).then((entries) => {
        setFromEntries(disabledVisionServices, entries);
    });
    $effect(() => {
        set(refreshRatesKey, [...refreshRates.entries()]);
    });
    $effect(() => {
        set(disabledCamerasKey, [...disabledCameras.entries()]);
    });
    $effect(() => {
        set(disabledVisionServicesKey, [...disabledVisionServices.entries()]);
    });
    setContext(key, {
        get refreshRates() {
            return refreshRates;
        },
        get disabledCameras() {
            return disabledCameras;
        },
        get disabledVisionServices() {
            return disabledVisionServices;
        },
    });
};
export const useMachineSettings = () => {
    return getContext(key);
};
