import { jsx as l, jsxs as L } from "react/jsx-runtime";
import { Fragment as I } from "react";
import { a as h } from "./heading-gqtQNo5n.mjs";
import { a as p } from "./link-DY5WLpyc.mjs";
import { a as H } from "./paragraph-DNtj7OLr.mjs";
import { T as N } from "./text-spacing-BU0mkEf9.mjs";
const m = {
  HYPERLINK: "hyperlink",
  ENTRY_HYPERLINK: "entry-hyperlink",
  ASSET_HYPERLINK: "asset-hyperlink",
  RESOURCE_HYPERLINK: "resource-hyperlink",
  EMBEDDED_ENTRY: "embedded-entry-inline"
}, i = {
  DOCUMENT: "document",
  PARAGRAPH: "paragraph",
  TEXT: "text",
  HYPERLINK: "hyperlink",
  ENTRY_HYPERLINK: "entry-hyperlink",
  HEADER_1: "header-1",
  HEADER_2: "header-2",
  HEADER_3: "header-3",
  HEADER_4: "header-4",
  HEADER_5: "header-5",
  HEADER_6: "header-6",
  HEADING_1: "heading-1",
  HEADING_2: "heading-2",
  HEADING_3: "heading-3",
  HEADING_4: "heading-4",
  HEADING_5: "heading-5",
  HEADING_6: "heading-6",
  TABLE: "table",
  TABLE_ROW: "table-row",
  TABLE_HEADER_CELL: "table-header-cell",
  TABLE_CELL: "table-cell",
  TABLE_BODY: "table-body",
  OL_LIST: "ordered-list",
  UL_LIST: "unordered-list",
  LIST_ITEM: "list-item",
  HR: "horizontal-row",
  QUOTE: "blockquote",
  EMBEDDED_ENTRY: "embedded-entity",
  EMBEDDED_ASSET: "embedded-asset-block",
  IMAGE: "image",
  VIDEO: "video",
  RICHTEXTDOCUMENT: "rich-text-document",
  TEASER: "teaser"
}, _ = {
  BOLD: "bold",
  ITALIC: "italic",
  UNDERLINE: "underline",
  CODE: "code"
}, b = (e) => e.type === i.TABLE_ROW && !!e.content?.every((n) => n.type === i.TABLE_HEADER_CELL), O = (e, n) => {
  if (e.type !== i.TABLE)
    return n;
  const { content: a } = e, [r, t] = (a || []).reduce(
    ([s, E], u) => b(u) ? [[...s, u], E] : [s, [...E, u]],
    [[], []]
  ), o = n.slice(0, r.length), c = n.slice(r.length, n.length);
  return /* @__PURE__ */ l("div", { className: "purpur-rich-text-table-scroll-container", children: /* @__PURE__ */ l("div", { className: "purpur-rich-text-table-inner-scroll-container", children: /* @__PURE__ */ L("table", { children: [
    r.length > 0 && /* @__PURE__ */ l("thead", { children: o }),
    t.length > 0 && /* @__PURE__ */ l("tbody", { children: c })
  ] }) }) });
}, A = (e, n) => Object.values(e).includes(n), y = (e) => e?.type === "text", T = (e) => !D(e) && !y(e) && A(i, e?.type), D = (e) => A(m, e?.type), v = (e) => {
  try {
    return JSON.parse(e);
  } catch {
    return null;
  }
}, B = (e, n = " ") => {
  const a = v(e);
  return !a || !a.content ? "" : a.content.reduce((r, t, o) => {
    let c = "";
    if (y(t))
      c = t.value;
    else if ((T(t) || D(t)) && (c = B(JSON.stringify(t), n), !c.length))
      return r;
    const s = a?.content?.[o + 1], E = T(s) ? n : "";
    return r + c + E;
  }, "");
};
function C(e) {
  return e.type === i.IMAGE && typeof e.value == "object" && e?.value && "image" in e.value && e?.value?.image?.url ? { ...e, value: { ...e.value, url: e.value.image.url } } : e.type === i.VIDEO && typeof e.value == "object" && e?.value && "video" in e.value && e.value.video?.url ? { ...e, value: { ...e.value, url: e.value.video.url } } : e;
}
const x = {
  [_.BOLD]: (e) => /* @__PURE__ */ l("b", { children: e }),
  [_.ITALIC]: (e) => /* @__PURE__ */ l("i", { children: e }),
  [_.UNDERLINE]: (e) => /* @__PURE__ */ l("u", { children: e }),
  [_.CODE]: (e) => /* @__PURE__ */ l("code", { children: e })
}, f = (e) => {
  const n = e.type ?? e.nodeType;
  return n.startsWith("heading-") ? n.replace("heading-", "header-") : n.startsWith("entry-hyperlink") ? n.replace("entry-hyperlink", "hyperlink") : n;
};
function S(e, n, a) {
  return {
    [i.RICHTEXTDOCUMENT]: (r, t) => t,
    [i.DOCUMENT]: (r, t) => t,
    [i.PARAGRAPH]: (r, t) => /* @__PURE__ */ l(H, { variant: "paragraph-200", children: t }),
    [i.TEXT]: (r) => {
      const { value: t, marks: o } = r, c = typeof t == "string" ? t.replace(/\n/g, "<br />") : t, s = /* @__PURE__ */ l("span", { dangerouslySetInnerHTML: { __html: c } });
      return o ? o.reduce((E, u) => x[typeof u == "string" ? u : u.type](
        E
      ), s) : s;
    },
    [i.HYPERLINK]: (r, t) => {
      const o = r.uri ?? r.data?.uri;
      return e ? /* @__PURE__ */ l(p, { variant: "text", children: e(t, o) }) : /* @__PURE__ */ l(p, { variant: "text", children: /* @__PURE__ */ l("a", { href: o, children: t }) });
    },
    [i.HR]: () => /* @__PURE__ */ l("hr", {}),
    [i.HEADER_2]: (r, t) => /* @__PURE__ */ l(h, { tag: "h2", variant: "title-300", children: t }),
    [i.HEADER_3]: (r, t) => /* @__PURE__ */ l(h, { tag: "h3", variant: "title-200", children: t }),
    [i.HEADER_4]: (r, t) => /* @__PURE__ */ l(h, { tag: "h4", variant: "title-100", children: t }),
    [i.HEADER_5]: (r, t) => /* @__PURE__ */ l(h, { tag: "h5", variant: "title-100", children: t }),
    [i.HEADER_6]: (r, t) => /* @__PURE__ */ l(h, { tag: "h6", variant: "title-100", children: t }),
    [i.TABLE]: O,
    [i.TABLE_ROW]: (r, t) => /* @__PURE__ */ l("tr", { children: t }),
    [i.TABLE_HEADER_CELL]: (r, t) => /* @__PURE__ */ l("th", { scope: "col", children: t }),
    [i.TABLE_CELL]: (r, t) => /* @__PURE__ */ l("td", { children: t }),
    [i.TABLE_BODY]: (r, t) => /* @__PURE__ */ l("tbody", { children: t }),
    [i.LIST_ITEM]: (r, t) => /* @__PURE__ */ l("li", { children: t }),
    [i.OL_LIST]: (r, t) => /* @__PURE__ */ l("ol", { children: t }),
    [i.UL_LIST]: (r, t) => /* @__PURE__ */ l("ul", { children: t }),
    [i.QUOTE]: (r, t) => /* @__PURE__ */ L("blockquote", { children: [
      "“",
      t,
      "”"
    ] }),
    [i.EMBEDDED_ENTRY]: (r, t) => t,
    [i.EMBEDDED_ASSET]: (r, t) => t,
    [i.IMAGE]: ({ value: r }) => typeof r == "object" && r && "url" in r && r.url ? n ? n(r.url, r.altText, r.image?.contentType) : /* @__PURE__ */ l("img", { src: r.url, alt: r.altText ?? "", loading: "lazy" }) : null,
    [i.VIDEO]: ({ value: r }) => {
      if (typeof r == "object" && r && "posterImage" in r) {
        const t = r.posterImage?.url;
        return (
          // eslint-disable-next-line jsx-a11y/media-has-caption
          /* @__PURE__ */ l(
            "video",
            {
              "data-testid": "rich text video",
              controls: !0,
              src: r.url,
              poster: a && t ? a(t) : t
            }
          )
        );
      }
      return null;
    }
  };
}
const d = (e) => !e.replace(/\s+/g, ""), M = (e) => {
  const { content: n, value: a } = e;
  if (!n)
    return typeof a == "string" && d(a);
  for (const r of n)
    if (f(r) === i.HYPERLINK)
      return !1;
  return n.length > 0 && typeof n[0].value == "string" && d(n[0].value);
}, R = (e, n, a, r, t) => {
  let o = 0;
  const c = {
    ...S(a, r, t),
    ...n
  }, s = (E) => {
    const u = c[f(E)];
    if (typeof u > "u" || M(E))
      return null;
    const g = E.content ? E.content.map(s) : [];
    return /* @__PURE__ */ l(I, { children: u(C(E), g) }, o++);
  };
  return s(e);
};
function j({
  data: e,
  className: n,
  customNodeTypes: a,
  noTextSpacing: r = !1,
  link: t,
  image: o,
  videoPoster: c,
  element: s = "div",
  ...E
}) {
  return e ? r ? /* @__PURE__ */ l(s, { className: n, ...E, children: R(e, a, t, o, c) }) : /* @__PURE__ */ l(N, { className: n, element: s, ...E, children: R(e, a, t, o, c) }) : null;
}
export {
  j as R,
  B as r
};
//# sourceMappingURL=RichText-DRacgpis.mjs.map
