'use client';

import * as React from 'react';

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

import { LessonType } from './lesson-type';

// import { LessonType } from './ILessonType';

function noop(): void {
  return undefined;
}

export interface LessonTypesSelectionContextValue extends Selection {}

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

interface LessonTypesSelectionProviderProps {
  children: React.ReactNode;
  lessonTypes: LessonType[];
}

export function LessonTypesSelectionProvider({
  children,
  lessonTypes = [],
}: LessonTypesSelectionProviderProps): React.JSX.Element {
  const lessonTypeIds = React.useMemo(() => lessonTypes.map((lessonType) => lessonType.id), [lessonTypes]);
  const selection = useSelection(lessonTypeIds);

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

export function useLessonTypesSelection(): LessonTypesSelectionContextValue {
  return React.useContext(LessonTypesSelectionContext);
}
