import { jsx, jsxs } from "react/jsx-runtime";
import { Children } from "react";
import { useDefaultTextProps, flattenChildren, DefaultTextPropsProvider, Text } from "../Accordion/AccordionItem.mjs";
import { Stack } from "../Stack/Stack.mjs";
import { Box } from "../Box/Box.mjs";
import { lineHeightContainer } from "../../css/lineHeightContainer.css.mjs";
import { currentColor, size, minCharacterWidth, trimGutter } from "./List.css.mjs";
function numberToAlpha(inputNumber) {
  let returnValue = "";
  let counter = inputNumber;
  while (counter > 0) {
    const t = (counter - 1) % 26;
    returnValue = String.fromCharCode(97 + t) + returnValue;
    counter = (counter - t) / 26 | 0;
  }
  return returnValue;
}
const romanNumerals = {
  m: 1e3,
  cm: 900,
  d: 500,
  cd: 400,
  c: 100,
  xc: 90,
  l: 50,
  xl: 40,
  x: 10,
  ix: 9,
  v: 5,
  iv: 4,
  i: 1
};
function numberToRomanNumerals(inputNumber) {
  let returnValue = "";
  let counter = inputNumber;
  Object.keys(romanNumerals).forEach(
    (romanNumeral) => {
      const value = romanNumerals[romanNumeral];
      while (counter >= value) {
        returnValue += romanNumeral;
        counter -= value;
      }
    }
  );
  return returnValue;
}
const CharacterBullet = ({ length = 1, children }) => /* @__PURE__ */ jsxs(
  Box,
  {
    display: "inlineBlock",
    className: [
      minCharacterWidth[length - 1] ?? minCharacterWidth[minCharacterWidth.length - 1],
      trimGutter
    ],
    children: [
      children,
      "."
    ]
  }
);
const List = ({
  children,
  size: sizeProp,
  tone: toneProp,
  space = "medium",
  type = "bullet",
  start = 1,
  data,
  ...restProps
}) => {
  const { size: size$1, tone } = useDefaultTextProps({
    size: sizeProp,
    tone: toneProp
  });
  const listItems = flattenChildren(children);
  const lastNumberLength = type === "number" ? (listItems.length + (start - 1)).toString().length : -1;
  return /* @__PURE__ */ jsx(DefaultTextPropsProvider, { size: size$1, tone, children: /* @__PURE__ */ jsx(
    Stack,
    {
      component: /^(bullet|icon)$/.test(type) ? "ul" : "ol",
      space,
      data,
      children: Children.map(listItems, (listItem, index) => {
        const resolvedIndex = index + (start - 1);
        return /* @__PURE__ */ jsxs(Box, { display: "flex", children: [
          /* @__PURE__ */ jsx(Text, { component: "div", size: size$1, tone, children: /* @__PURE__ */ jsx(
            Box,
            {
              display: "flex",
              alignItems: /^(bullet|icon)$/.test(type) ? "center" : void 0,
              className: lineHeightContainer[size$1],
              userSelect: "none",
              "aria-hidden": true,
              children: (() => {
                if (type === "number") {
                  return /* @__PURE__ */ jsx(CharacterBullet, { length: lastNumberLength, children: resolvedIndex + 1 });
                }
                if (type === "alpha") {
                  return /* @__PURE__ */ jsx(CharacterBullet, { children: numberToAlpha(resolvedIndex + 1) });
                }
                if (type === "roman") {
                  return /* @__PURE__ */ jsx(CharacterBullet, { length: 2, children: numberToRomanNumerals(resolvedIndex + 1) });
                }
                if (type === "icon" && "icon" in restProps) {
                  return restProps.icon;
                }
                return /* @__PURE__ */ jsx(
                  Box,
                  {
                    borderRadius: "full",
                    className: [currentColor, size[size$1]]
                  }
                );
              })()
            }
          ) }),
          /* @__PURE__ */ jsx(
            Box,
            {
              minWidth: 0,
              width: "full",
              paddingLeft: size$1 === "xsmall" ? "xsmall" : "small",
              children: listItem
            }
          )
        ] });
      })
    }
  ) });
};
export {
  List
};
