// packages/core/src/components/number-field/number-field-context.ts
import type { Accessor } from "solid-js";
import { createContext, useContext } from "solid-js";

export interface NumberFieldContextValue {
  value: Accessor<number>;
  setValue: (value: number) => void;
  inputId: Accessor<string>;
  descriptionId: Accessor<string | undefined>;
  errorMessageId: Accessor<string | undefined>;
  min: Accessor<number | undefined>;
  max: Accessor<number | undefined>;
  step: Accessor<number>;
  disabled: Accessor<boolean>;
  increment: () => void;
  decrement: () => void;
  setDescriptionId: (id: string | undefined) => void;
  setErrorMessageId: (id: string | undefined) => void;
}

const NumberFieldContext = createContext<NumberFieldContextValue>();

/**
 * Returns the NumberField context. Throws if used outside <NumberField>.
 */
export function useNumberFieldContext(): NumberFieldContextValue {
  const ctx = useContext(NumberFieldContext);
  if (!ctx) {
    throw new Error(
      "[PettyUI] NumberField parts must be used inside <NumberField>.\n" +
        "  Fix: Wrap NumberField.Input, NumberField.Label, etc. inside <NumberField>.",
    );
  }
  return ctx;
}

/** Provider for NumberField context. */
export const NumberFieldContextProvider = NumberFieldContext.Provider;
