import {getTreeMenu} from '@apis';
import {useContext} from '@hooks';
import {useQuery} from '@tanstack/react-query';
import {context} from '../context';

export function useMenuTree() {
  const {data} = useQuery(
    [getTreeMenu.name],
    async function() {
      const data = await getTreeMenu();

      if (data.msg === '200')
        return data.data;

      throw new Error(data.errMsg);
    },
    {
      suspense: true,
      cacheTime: 0,
    },
  );

  return data;
}

export function useCheck() {
  const [list, setList] = useContext(context);

  function onCheck(keys: string[]) {
    setList(keys);
  }

  return [list, {onCheck}] as const;
}
