'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 type { LpQuestion } from './type';

function noop(): void {
  return undefined;
}

export interface LpQuestionsSelectionContextValue extends Selection {}

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

interface LpQuestionsSelectionProviderProps {
  children: React.ReactNode;
  lessonQuestions: LpQuestion[];
}

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

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

export function useLpQuestionsSelection(): LpQuestionsSelectionContextValue {
  return React.useContext(LpQuestionsSelectionContext);
}
