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 createMeter}. Every reactive input is `MaybeAccessor<T>`.
 *
 * Headless primitive — `<Meter.Root>` layers the label-id registry and the
 * NVDA-pump span on top.
 */

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

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

const MeterContext = createContext(null);

/**
 * Read the nearest {@link MeterApi} provided by `c.Provider`. Throws if
 * called outside a Provider.
 */
export function useMeter() {
  const ctx = useContext(MeterContext);
  if (ctx == null) {
    throw new Error("Base UI: useMeter() must be called inside a <c.Provider>.");
  }
  return ctx;
}

/**
 * Solid-native headless meter primitive — pure derivations from
 * value/min/max + format options. No internal state.
 */
export function createMeter(options) {
  const value = createMemo(() => readOption(options.value), {
    name: "meter.value"
  });
  const min = createMemo(() => readOption(options.min) ?? 0, {
    name: "meter.min"
  });
  const max = createMemo(() => readOption(options.max) ?? 100, {
    name: "meter.max"
  });
  const formattedValue = createMemo(() => formatNumberValue(value(), readOption(options.locale), readOption(options.format)), {
    name: "meter.formattedValue"
  });
  const ariaValueText = createMemo(() => {
    const fn = options.getAriaValueText;
    if (fn) return fn(formattedValue(), value());
    if (readOption(options.format)) return formattedValue();
    return `${value()}%`;
  }, {
    name: "meter.ariaValueText"
  });
  const rootInternal = {
    role: "meter",
    get "aria-valuemin"() {
      return min();
    },
    get "aria-valuemax"() {
      return max();
    },
    get "aria-valuenow"() {
      return value();
    },
    get "aria-valuetext"() {
      return ariaValueText();
    }
  };
  const root = userProps => mergeRootProps(rootInternal, userProps);
  const api = {
    value,
    min,
    max,
    formattedValue,
    ariaValueText,
    root,
    Provider: props => <MeterContext value={api}>{props.children}</MeterContext>
  };
  api[SNAPSHOT_KEYS] = METER_SNAPSHOT_KEYS;
  return api;
}
const METER_SNAPSHOT_KEYS = snapshotKeys("value", "min", "max", "formattedValue", "ariaValueText");
//# sourceMappingURL=create-meter.jsx.map
