import {
  Label
} from "./chunk-ZN7OWLZY.js";
import {
  VisuallyHidden
} from "./chunk-PRZRE6A5.js";

// src/components/Progress/Progress.tsx
import classNames from "classnames/dedupe";
import { useId } from "react";
import "@simplybusiness/mobius/src/components/Progress/Progress.css";
import { jsx, jsxs } from "react/jsx-runtime";
function warnAboutInvalidValues(value, minValue, maxValue) {
  if (minValue > maxValue) {
    console.warn("minValue is greater than maxValue");
  }
  if (value < minValue) {
    console.warn("Progress value is less than minValue");
  }
  if (value > maxValue) {
    console.warn("Progress value is greater than maxValue");
  }
}
var sanitizedValue = (value, defaultValue) => {
  if (value === void 0 || Number.isNaN(value)) {
    return defaultValue;
  }
  return value;
};
var getLabelComponent = (label, progressLabelId, showLabel) => {
  if (showLabel) {
    return /* @__PURE__ */ jsx(
      Label,
      {
        id: progressLabelId,
        className: "mobius-progress__label",
        elementType: "span",
        children: label
      }
    );
  }
  return /* @__PURE__ */ jsx(VisuallyHidden, { children: /* @__PURE__ */ jsx(
    Label,
    {
      id: progressLabelId,
      className: "mobius-progress__label",
      elementType: "span",
      children: label
    }
  ) });
};
var DEFAULT_VALUE = 0;
var DEFAULT_MIN_VALUE = 0;
var DEFAULT_MAX_VALUE = 100;
var Progress = ({ ref, ...props }) => {
  const progressId = useId();
  const progressLabelId = useId();
  const {
    id,
    label,
    showLabel = true,
    showValueLabel = !!label,
    valueFormatter,
    variant = "primary",
    className
  } = props;
  let { value, minValue, maxValue } = props;
  value = sanitizedValue(value, DEFAULT_VALUE);
  minValue = sanitizedValue(minValue, DEFAULT_MIN_VALUE);
  maxValue = sanitizedValue(maxValue, DEFAULT_MAX_VALUE);
  if (value === void 0 || minValue === void 0 || maxValue === void 0) {
    return null;
  }
  warnAboutInvalidValues(value, minValue, maxValue);
  const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
  const percentage = clamp(
    Math.round((value - minValue) / (maxValue - minValue) * 100),
    0,
    100
  );
  const barWidth = `${percentage}%`;
  const classes = classNames("mobius", "mobius-progress", className, {
    "--is-primary": variant === "primary",
    "--is-secondary": variant === "secondary"
  });
  const progressBarProps = {};
  progressBarProps["aria-valuemin"] = minValue.toString();
  progressBarProps["aria-valuemax"] = maxValue.toString();
  progressBarProps["aria-valuenow"] = value.toString();
  progressBarProps["aria-valuetext"] = valueFormatter instanceof Function ? valueFormatter(value, minValue, maxValue) : barWidth;
  const labelComponent = getLabelComponent(label, progressLabelId, showLabel);
  return /* @__PURE__ */ jsxs(
    "div",
    {
      id: id || progressId,
      ...progressBarProps,
      ref,
      className: classes,
      role: "progressbar",
      "aria-labelledby": progressLabelId,
      children: [
        labelComponent,
        /* @__PURE__ */ jsx("div", { className: "mobius-progress__track", children: /* @__PURE__ */ jsx("div", { className: "mobius-progress__bar", style: { width: barWidth } }) }),
        showValueLabel && /* @__PURE__ */ jsx(Label, { "data-testid": "value-label", elementType: "span", children: progressBarProps["aria-valuetext"] })
      ]
    }
  );
};
Progress.displayName = "Progress";

export {
  Progress
};
//# sourceMappingURL=chunk-MOTQYCHH.js.map
