// src/providers/MasterItemsProvider.tsx
import React, { ReactNode, useMemo } from 'react';
import { MasterItemsContext } from '../contexts/MasterItemsContext';
import type { MasterGroceryItem } from '../types';
import * as apiClient from '../services/apiClient';
import { useApiOnMount } from '../hooks/useApiOnMount';

export const MasterItemsProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const { data, loading, error } = useApiOnMount<MasterGroceryItem[], []>(
    () => apiClient.fetchMasterItems()
  );

  const value = useMemo(() => ({
    masterItems: data || [],
    isLoading: loading,
    error: error?.message || null,
  }), [data, loading, error]);

  return <MasterItemsContext.Provider value={value}>{children}</MasterItemsContext.Provider>;
};