import { ArmClient } from '@viamrobotics/sdk';
import { createResourceClient, createResourceQuery, useResourceNames, } from '@viamrobotics/svelte-sdk';
import { getContext, setContext } from 'svelte';
const key = Symbol('arm-kinematics-context');
export const provideArmKinematics = (partID) => {
    const arms = useResourceNames(partID, 'arm');
    // Kinematics are static config data, so fetch once and cache indefinitely
    const options = { staleTime: Infinity, refetchOnMount: false, refetchInterval: false };
    const names = $derived(arms.current.map((arm) => arm.name));
    const clients = $derived(arms.current.map((arm) => createResourceClient(ArmClient, partID, () => arm.name)));
    const kinematicsQueries = $derived(clients.map((client) => [client.current?.name, createResourceQuery(client, 'getKinematics', () => options)]));
    const kinematics = $derived(Object.fromEntries(kinematicsQueries.map(([name, query]) => {
        const data = query.data;
        const joints = data && ('joints' in data ? data.joints : data.kinematicsData.joints);
        return [
            name,
            joints?.map((j) => ({
                id: j.id,
                min: j.min,
                max: j.max,
            })),
        ];
    })));
    setContext(key, {
        get names() {
            return names;
        },
        get kinematics() {
            return kinematics;
        },
    });
};
export const useArmKinematics = () => {
    return getContext(key);
};
