import { createContext, createMemo, useContext } from "solid-js";
import { formatNumberValue } from "../internals/format-number.jsx";
import { readOption } from "../internals/maybe-accessor.jsx";
import { mergeRootProps } from "../internals/merge-root-props.jsx";
import { SNAPSHOT_KEYS, snapshotKeys } from "../internals/observability/snapshot.jsx";

/**
 * Options for {@link createProgress}. Every reactive input is a
 * `MaybeAccessor<T>`; pass an accessor for live updates.
 *
 * Headless primitive — no Label/track/value/indicator parts. The wrapper
 * `<Progress.Root>` layers the label-id registry, the NVDA-pump span, and
 * the per-part adapters on top.
 */

/** Slot bag for the `<div role="progressbar">` element. */

/** Public api returned by {@link createProgress}. */

const ProgressContext = createContext(null);

/**
 * Read the nearest {@link ProgressApi} provided by `c.Provider`. Throws if
 * called outside a Provider.
 */
export function useProgress() {
  const ctx = useContext(ProgressContext);
  if (ctx == null) {
    throw new Error("Base UI: useProgress() must be called inside a <c.Provider>.");
  }
  return ctx;
}
function defaultGetAriaValueText(formattedValue, value) {
  if (value == null) return "indeterminate progress";
  return formattedValue || `${value}%`;
}

/**
 * Solid-native headless progress primitive. Pure derivations from the input
 * value/min/max — no internal state. Pair with `<Progress.Root>` for the
 * structural wrapper, or use `c.root()` directly for a fully custom shell.
 */
export function createProgress(options) {
  const value = createMemo(() => readOption(options.value), {
    name: "progress.value"
  });
  const min = createMemo(() => readOption(options.min) ?? 0, {
    name: "progress.min"
  });
  const max = createMemo(() => readOption(options.max) ?? 100, {
    name: "progress.max"
  });
  const status = createMemo(() => {
    const v = value();
    if (v == null || !Number.isFinite(v)) return "indeterminate";
    return v === max() ? "complete" : "progressing";
  }, {
    name: "progress.status"
  });
  const formattedValue = createMemo(() => formatNumberValue(value(), readOption(options.locale), readOption(options.format)), {
    name: "progress.formattedValue"
  });
  const ariaValueText = createMemo(() => {
    const fn = options.getAriaValueText ?? defaultGetAriaValueText;
    return fn(formattedValue(), value());
  }, {
    name: "progress.ariaValueText"
  });
  const rootInternal = {
    role: "progressbar",
    get "aria-valuemin"() {
      return min();
    },
    get "aria-valuemax"() {
      return max();
    },
    get "aria-valuenow"() {
      const v = value();
      return v == null ? undefined : v;
    },
    get "aria-valuetext"() {
      return ariaValueText();
    }
  };
  const root = userProps => mergeRootProps(rootInternal, userProps);
  const api = {
    value,
    min,
    max,
    formattedValue,
    status,
    ariaValueText,
    root,
    Provider: props => <ProgressContext value={api}>{props.children}</ProgressContext>
  };
  api[SNAPSHOT_KEYS] = PROGRESS_SNAPSHOT_KEYS;
  return api;
}
const PROGRESS_SNAPSHOT_KEYS = snapshotKeys("value", "min", "max", "formattedValue", "status", "ariaValueText");
//# sourceMappingURL=create-progress.jsx.map
