'use client';

import * as React from 'react';
import { formatNumber } from '../../utils/formatNumber.js';
import { mergeProps } from '../../merge-props/index.js';
import { useLatestRef } from '../../utils/useLatestRef.js';
function formatValue(value, format) {
  if (value == null) {
    return '';
  }
  if (!format) {
    return formatNumber(value / 100, [], {
      style: 'percent'
    });
  }
  return formatNumber(value, [], format);
}
function getDefaultAriaValueText(formattedValue, value) {
  if (value == null) {
    return 'indeterminate progress';
  }
  return formattedValue || `${value}%`;
}
function useProgressRoot(parameters) {
  const {
    'aria-label': ariaLabel,
    'aria-labelledby': ariaLabelledby,
    'aria-valuetext': ariaValuetext,
    format,
    getAriaLabel,
    getAriaValueText,
    max = 100,
    min = 0,
    value
  } = parameters;
  const formatOptionsRef = useLatestRef(format);
  let status = 'indeterminate';
  if (Number.isFinite(value)) {
    status = value === max ? 'complete' : 'progressing';
  }
  const formattedValue = formatValue(value, formatOptionsRef.current);
  const getRootProps = React.useCallback((externalProps = {}) => mergeProps({
    'aria-label': getAriaLabel ? getAriaLabel(value) : ariaLabel,
    'aria-labelledby': ariaLabelledby,
    'aria-valuemax': max,
    'aria-valuemin': min,
    'aria-valuenow': value ?? undefined,
    'aria-valuetext': getAriaValueText ? getAriaValueText(formattedValue, value) : ariaValuetext ?? getDefaultAriaValueText(formattedValue, value),
    role: 'progressbar'
  }, externalProps), [ariaLabel, ariaLabelledby, ariaValuetext, formattedValue, getAriaLabel, getAriaValueText, max, min, value]);
  return {
    getRootProps,
    formattedValue,
    max,
    min,
    value,
    status
  };
}
export { useProgressRoot };