import { createContext, useContext } from "solid-js";
import type { ListNavigationState } from "../../primitives/create-list-navigation";

export interface ListboxContextValue {
  navigation: ListNavigationState;
}

const ListboxContext = createContext<ListboxContextValue>();

/**
 * Returns the Listbox context. Throws if used outside <Listbox>.
 */
export function useListboxContext(): ListboxContextValue {
  const ctx = useContext(ListboxContext);
  if (!ctx) {
    throw new Error(
      "[PettyUI] Listbox.Item must be used inside <Listbox>.\n" +
        "  Fix: <Listbox items={[...]}>\n" +
        '         <Listbox.Item value="a">A</Listbox.Item>\n' +
        "       </Listbox>",
    );
  }
  return ctx;
}

export const ListboxContextProvider = ListboxContext.Provider;
