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

export interface RadioGroupContextValue {
  value: Accessor<string | undefined>;
  onValueChange: (value: string) => void;
  disabled: Accessor<boolean>;
  name: Accessor<string | undefined>;
}

const RadioGroupContext = createContext<RadioGroupContextValue>();

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

export const RadioGroupContextProvider = RadioGroupContext.Provider;
