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

/** Context shared between all Slider parts. */
export interface SliderContextValue {
  value: Accessor<number>;
  setValue: (v: number) => void;
  min: Accessor<number>;
  max: Accessor<number>;
  step: Accessor<number>;
  orientation: Accessor<"horizontal" | "vertical">;
  disabled: Accessor<boolean>;
}

const SliderContext = createContext<SliderContextValue>();

/**
 * Returns the Slider context. Throws if used outside <Slider>.
 */
export function useSliderContext(): SliderContextValue {
  const ctx = useContext(SliderContext);
  if (!ctx) {
    throw new Error(
      "[PettyUI] Slider parts must be used inside <Slider>.\n" +
        "  Fix: Wrap Slider.Track, Slider.Range, and Slider.Thumb inside <Slider>.",
    );
  }
  return ctx;
}

export const SliderContextProvider = SliderContext.Provider;
