import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import { useMemo, forwardRef } from 'react';
import { c as cx } from './index-De1g9FRV.js';
import { S as SrOnly } from './SrOnly-pnf8ajnh.js';
import { T as Tooltip } from './Tooltip-BC8lnFhe.js';
import { F as FieldLabelButton, A as AiMark } from './AiMark-voHPSerQ.js';
import { I as Icon } from './Icon-B4uCvMU9.js';
import { S as SvgInfo } from './info-CYpG6tcI.js';

import './FieldLabel.css';const INLINE_MARKDOWN_REGEX = /\*\*\*(.+?)\*\*\*|\*\*(.+?)\*\*|\*(.+?)\*|==(.+?)==|`(.+?)`/g;

const highlight = "_highlight_1v2if_1";
const code = "_code_1v2if_7";
const styles$1 = {
	highlight: highlight,
	code: code
};

const parseInlineMarkdown = (text) => {
  const parts = [];
  let lastIndex = 0;
  INLINE_MARKDOWN_REGEX.lastIndex = 0;
  let match;
  while ((match = INLINE_MARKDOWN_REGEX.exec(text)) !== null) {
    if (match.index > lastIndex) {
      parts.push(text.substring(lastIndex, match.index));
    }
    const key = match.index;
    const savedIndex = INLINE_MARKDOWN_REGEX.lastIndex;
    if (match[1]) {
      parts.push(
        /* @__PURE__ */ jsx("strong", { children: /* @__PURE__ */ jsx("em", { children: parseInlineMarkdown(match[1]) }) }, key)
      );
    } else if (match[2]) {
      parts.push(/* @__PURE__ */ jsx("strong", { children: parseInlineMarkdown(match[2]) }, key));
    } else if (match[3]) {
      parts.push(/* @__PURE__ */ jsx("em", { children: parseInlineMarkdown(match[3]) }, key));
    } else if (match[4]) {
      parts.push(
        /* @__PURE__ */ jsx("mark", { className: styles$1.highlight, children: parseInlineMarkdown(match[4]) }, key)
      );
    } else if (match[5]) {
      parts.push(
        /* @__PURE__ */ jsx("code", { className: styles$1.code, children: match[5] }, key)
      );
    }
    INLINE_MARKDOWN_REGEX.lastIndex = savedIndex;
    lastIndex = savedIndex;
  }
  if (lastIndex < text.length) {
    parts.push(text.substring(lastIndex));
  }
  return parts;
};

const label = "_label_13ezo_1";
const required = "_required_13ezo_50";
const styles = {
	label: label,
	"top-container": "_top-container_13ezo_30",
	"ai-mark-icon": "_ai-mark-icon_13ezo_34",
	"label-text": "_label-text_13ezo_45",
	required: required,
	"info-container": "_info-container_13ezo_56",
	"info-trigger": "_info-trigger_13ezo_62",
	"info-content": "_info-content_13ezo_68"
};

const FieldLabelMoreInfoIcon = ({
  moreInfo,
  moreInfoId
}) => {
  return /* @__PURE__ */ jsxs("div", { className: styles["info-container"], children: [
    moreInfoId && /* @__PURE__ */ jsx(SrOnly, { id: moreInfoId, children: moreInfo }),
    /* @__PURE__ */ jsxs(
      Tooltip,
      {
        placement: "top",
        offset: 5,
        fallbackPlacements: ["right", "bottom", "left"],
        children: [
          /* @__PURE__ */ jsx(Tooltip.Trigger, { className: styles["info-trigger"], children: /* @__PURE__ */ jsx(FieldLabelButton, { "aria-label": "More information", children: /* @__PURE__ */ jsx(Icon, { svg: SvgInfo }) }) }),
          /* @__PURE__ */ jsx(Tooltip.Content, { className: styles["info-content"], children: moreInfo })
        ]
      }
    )
  ] });
};

const InlineMarkdown = ({ value = "" }) => {
  const content = useMemo(() => parseInlineMarkdown(value), [value]);
  return /* @__PURE__ */ jsx(Fragment, { children: content });
};

const FieldLabelElement = forwardRef(
  ({
    aiMark,
    el: Element = "label",
    children,
    className,
    moreInfo,
    moreInfoId,
    required,
    moreInfoOpen: _moreInfoOpen,
    // deprecated, ignored
    ...props
  }, ref) => {
    const FieldLabelClassNames = cx([styles["label"]], className);
    const labelText = /* @__PURE__ */ jsxs("span", { className: styles["label-text"], children: [
      typeof children === "string" ? /* @__PURE__ */ jsx(InlineMarkdown, { value: children }) : children,
      required ? /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx("span", { "aria-hidden": true, className: styles["required"], children: "*" }),
        /* @__PURE__ */ jsx(SrOnly, { children: "Required" })
      ] }) : null
    ] });
    const hasMoreInfoOrAiMark = moreInfo || aiMark;
    if (Element === "label" && hasMoreInfoOrAiMark) {
      const { htmlFor, id, ...labelProps } = props;
      return /* @__PURE__ */ jsx("div", { className: FieldLabelClassNames, ref, children: /* @__PURE__ */ jsxs("div", { className: styles["top-container"], children: [
        /* @__PURE__ */ jsx(
          "label",
          {
            htmlFor,
            id,
            "data-anv": "label",
            ...labelProps,
            children: labelText
          }
        ),
        moreInfo && /* @__PURE__ */ jsx(
          FieldLabelMoreInfoIcon,
          {
            moreInfo,
            moreInfoId
          }
        ),
        aiMark && /* @__PURE__ */ jsx(
          AiMark,
          {
            className: styles["ai-mark-icon"],
            type: "gradient",
            popoverOrTooltipConfig: typeof aiMark === "object" ? aiMark : void 0
          }
        )
      ] }) });
    }
    return /* @__PURE__ */ jsx(
      Element,
      {
        className: FieldLabelClassNames,
        "data-anv": "label",
        ref,
        ...props,
        children: hasMoreInfoOrAiMark ? /* @__PURE__ */ jsxs("div", { className: styles["top-container"], children: [
          labelText,
          moreInfo && /* @__PURE__ */ jsx(
            FieldLabelMoreInfoIcon,
            {
              moreInfo,
              moreInfoId
            }
          ),
          aiMark && /* @__PURE__ */ jsx(
            AiMark,
            {
              className: styles["ai-mark-icon"],
              type: "gradient",
              popoverOrTooltipConfig: typeof aiMark === "object" ? aiMark : void 0
            }
          )
        ] }) : labelText
      }
    );
  }
);
FieldLabelElement.displayName = "FieldLabel";
const FieldLabel = FieldLabelElement;

export { FieldLabel as F, INLINE_MARKDOWN_REGEX as I };
//# sourceMappingURL=FieldLabel-Cm5A1Jcx.js.map
