import "../Progress.css";
import { jsxs, Fragment, jsx } from "react/jsx-runtime";
import { useTheme } from "./Theme/UI.js";
import { Spacing } from "./Theme/themeUtils.js";
import { ShirtSize } from "./Theme/themes.js";
import Tooltip from "./Tooltip.js";
const base = "Progress_base_2f644";
const parts = "Progress_parts_2f644";
const bar = "Progress_bar_2f644";
const flat = "Progress_flat_2f644";
const legendContainer = "Progress_legendContainer_2f644";
const legendItem = "Progress_legendItem_2f644";
const colorButton = "Progress_colorButton_2f644";
const title = "Progress_title_2f644";
const css = {
  base,
  parts,
  bar,
  flat,
  legendContainer,
  legendItem,
  colorButton,
  title
};
const floatToPercent = (num, precision = 2) => {
  if (isNaN(num))
    return "NaN";
  if (num === Infinity)
    return "Infinity";
  if (num === -Infinity)
    return "-Infinity";
  return (num * 100).toFixed(precision) + "%";
};
const partStyle = (part) => ({
  backgroundColor: part.color,
  width: floatToPercent(part.percent, 0)
});
const sizeMap = {
  [ShirtSize.Small]: Spacing.Xs,
  [ShirtSize.Medium]: Spacing.Lg,
  [ShirtSize.Large]: Spacing.Xl3
};
const Progress = ({
  flat: flat2,
  parts: parts2,
  showLegend,
  showTooltips,
  size = ShirtSize.Small,
  title: title2
}) => {
  const {
    themeSettings: { className: themeClass }
  } = useTheme();
  const classes = [css.base, themeClass];
  if (flat2)
    classes.push(css.flat);
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    title2 && /* @__PURE__ */ jsx("h5", { className: css.title, children: title2 }),
    /* @__PURE__ */ jsx("div", { className: classes.join(" "), children: /* @__PURE__ */ jsx(
      "div",
      {
        className: css.bar,
        style: { height: `calc((${sizeMap[size]} + var(--theme-density)) * 1px)` },
        children: /* @__PURE__ */ jsx("div", { className: css.parts, children: parts2.filter((part) => part.percent !== 0 && !isNaN(part.percent)).map((part, idx) => /* @__PURE__ */ jsx(Tooltip, { content: showTooltips && part.label, children: /* @__PURE__ */ jsx(
          "li",
          {
            "aria-label": `${part.label} ${part.percent * 100} percent`,
            role: "progressbar",
            style: {
              ...partStyle(part),
              cursor: showTooltips && part.label ? "pointer" : ""
            }
          }
        ) }, idx)) })
      }
    ) }),
    showLegend && /* @__PURE__ */ jsx("div", { className: css.legendContainer, children: parts2.filter((part) => part.percent !== 0 && !isNaN(part.percent)).map((part, idx) => /* @__PURE__ */ jsxs("li", { className: css.legendItem, children: [
      /* @__PURE__ */ jsx("span", { className: css.colorButton, style: partStyle(part), children: "-" }),
      part.label
    ] }, idx)) })
  ] });
};
export {
  Progress as default
};
