'use client';

import * as React from 'react';

// import type { LessonCategory } from '@/types/lesson-type';
import { useSelection } from '@/hooks/use-selection';
import type { Selection } from '@/hooks/use-selection';

import { MfCategory } from './type';

function noop(): void {
  return undefined;
}

export interface MfCategoriesSelectionContextValue extends Selection {}

export const LpCategoriesSelectionContext = React.createContext<MfCategoriesSelectionContextValue>({
  deselectAll: noop,
  deselectOne: noop,
  selectAll: noop,
  selectOne: noop,
  selected: new Set(),
  selectedAny: false,
  selectedAll: false,
});

interface MfCategoriesSelectionProviderProps {
  children: React.ReactNode;
  lessonCategories: MfCategory[];
}

export function LpCategoriesSelectionProvider({
  children,
  lessonCategories = [],
}: MfCategoriesSelectionProviderProps): React.JSX.Element {
  const customerIds = React.useMemo(() => lessonCategories.map((customer) => customer.id), [lessonCategories]);
  const selection = useSelection(customerIds);

  return (
    <LpCategoriesSelectionContext.Provider value={{ ...selection }}>{children}</LpCategoriesSelectionContext.Provider>
  );
}

export function useMfCategoriesSelection(): MfCategoriesSelectionContextValue {
  return React.useContext(LpCategoriesSelectionContext);
}
