'use client';

import * as React from 'react';

import { useSelection } from '@/hooks/use-selection';
import type { Selection } from '@/hooks/use-selection';
// import type { LessonCategory } from './lesson-categories-table';
import type { LessonCategory } from '@/components/dashboard/lesson_category/interfaces';

function noop(): void {
  return undefined;
}

export interface LessonCategoriesSelectionContextValue extends Selection {}

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

interface LessonCategoriesSelectionProviderProps {
  children: React.ReactNode;
  lessonCategories: LessonCategory[];
}

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

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

export function useLessonCategoriesSelection(): LessonCategoriesSelectionContextValue {
  return React.useContext(LessonCategoriesSelectionContext);
}
