import { getUTMUrl as c } from "@arcblock/ux/lib/withTracker/libs/utm";
import { mapRecursive as a, filterRecursive as i, isUrl as u, isMailProtocol as l } from "./utils.js";
const v = window?.blocklet?.groupPrefix || window?.blocklet?.prefix || "/", d = (e) => {
  const r = { ...e }, t = e?.background;
  return typeof t == "string" ? r.background = { header: t, footer: t, default: t } : t && typeof t == "object" && (r.background = {
    header: t.header || t.default,
    footer: t.footer || t.default,
    default: t.default
  }), r;
}, h = (e, r = "en", t = "en") => {
  if (typeof e == "string") {
    if (u(e)) {
      const s = new URL(e);
      return s.searchParams.set("locale", r), s.href;
    }
    if (l(e))
      return e;
    const o = new URL(e, window.location.origin);
    return o.searchParams.set("locale", r), o.pathname + o.search;
  }
  return typeof e == "object" ? e[r] || e[t] || e.en : e;
}, N = ({ navigation: e, locale: r, defaultLocale: t, section: o = "header" }) => {
  if (!e?.length)
    return e;
  const s = (n, f) => n && typeof n == "object" ? n[f] || n[t] || n.en : n;
  return a(
    e,
    (n) => ({
      ...n,
      title: s(n.title, r),
      description: s(n.description, r),
      // 仅对叶结点进行处理
      link: c(n.items?.length ? n.link : h(n.link, r, t), o),
      _rawLink: n.link
    }),
    "items"
  );
}, g = (e) => a(
  e,
  (r) => r.role ? {
    ...r,
    role: Array.isArray(r.role) ? r.role : [r.role]
  } : r,
  "items"
), p = (e) => {
  if (!e?.length)
    return null;
  const r = g(e), t = {
    header: [],
    footer: [],
    // 对应 footer social media
    social: [],
    // 对应 footer 底部 links
    bottom: [],
    // 对应 dashboard#sidenav 导航
    dashboard: [],
    // session manager menus
    sessionManager: [],
    userCenter: []
  };
  return r.forEach((o) => {
    o.section ? Array.isArray(o.section) ? o.section.forEach((s) => {
      t[s]?.push(o);
    }) : typeof o.section == "string" && t[o.section]?.push(o) : t.header.push(o);
  }), t;
}, L = (e) => {
  if (!e)
    return null;
  const r = { ...e };
  return r.theme = d(r.theme), r.navigation = p(b(r.navigation)), r;
}, b = (e = []) => i(e, (r, t) => !!r.link || t.filteredChildren?.length, "items"), P = (e, r) => i(
  e,
  (t, o) => {
    const s = !t.role || r && (t.role.includes(r) || t.role.includes("guest"));
    return o.isLeaf ? s : s && o.filteredChildren?.length;
  },
  "items"
);
export {
  P as filterNavByRole,
  b as filterValidNavItems,
  L as formatBlockletInfo,
  g as formatNavigation,
  d as formatTheme,
  h as getLink,
  N as getLocalizedNavigation,
  p as parseNavigation,
  v as publicPath
};
