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

export interface ToggleGroupContextValue {
  isPressed: (value: string) => boolean;
  onItemPress: (value: string) => void;
  disabled: Accessor<boolean>;
}

const ToggleGroupContext = createContext<ToggleGroupContextValue>();

/**
 * Returns the ToggleGroup context. Throws if used outside <ToggleGroup>.
 */
export function useToggleGroupContext(): ToggleGroupContextValue {
  const ctx = useContext(ToggleGroupContext);
  if (!ctx) {
    throw new Error(
      "[PettyUI] ToggleGroup.Item must be used inside <ToggleGroup>.\n" +
        "  Fix: Wrap ToggleGroup.Item elements inside <ToggleGroup>.",
    );
  }
  return ctx;
}

export const ToggleGroupContextProvider = ToggleGroupContext.Provider;
