import type { Accessor } from "solid-js";
import { createContext, useContext } from "solid-js";

export interface CalendarContextValue {
  focusedDate: Accessor<Date>;
  selectedDate: Accessor<string | undefined>;
  displayMonth: Accessor<number>;
  displayYear: Accessor<number>;
  locale: Accessor<string>;
  weekStartsOn: Accessor<number>;
  minDate: Accessor<string | undefined>;
  maxDate: Accessor<string | undefined>;
  disabled: Accessor<boolean>;
  selectDate: (date: string) => void;
  focusDate: (date: Date) => void;
  goToPrevMonth: () => void;
  goToNextMonth: () => void;
  isDateDisabled: (date: string) => boolean;
  isDateSelected: (date: string) => boolean;
  isDateToday: (date: string) => boolean;
  getDaysInMonth: () => Date[];
  getWeekDayNames: () => string[];
}

const CalendarContext = createContext<CalendarContextValue>();

/** Returns the Calendar context. Throws if used outside a Calendar root. */
export function useCalendarContext(): CalendarContextValue {
  const ctx = useContext(CalendarContext);
  if (!ctx) {
    throw new Error(
      "[PettyUI] Calendar parts must be used within <Calendar>. Fix: Wrap your Calendar.Grid, Calendar.Cell, etc. inside <Calendar>.",
    );
  }
  return ctx;
}

export const CalendarContextProvider = CalendarContext.Provider;
