import h from "lodash/isEmpty";
import m from "dompurify";
import { toTextList as v, Link as f, getLink as p } from "@abtnode/util/lib/notification-preview/highlight";
import { isSameAddr as d } from "@abtnode/util/lib/notification-preview/func";
import { client as T } from "../libs/client.js";
const b = (t) => {
  if (!t || !t.length)
    return [];
  const e = [];
  let r = null, a = [];
  if (t.forEach((s) => {
    if (!s.activity || !s.activity.type || !r) {
      r && (a.length === 1 ? e.push(a[0]) : (r.items = a, e.push(r))), s.activity && s.activity.type ? (r = s, a = [s]) : (e.push(s), r = null, a = []);
      return;
    }
    const n = a[0].activity, c = n?.type, l = n?.target?.type, i = n?.target?.id, u = s.activity.type, o = s.activity.target?.type, y = s.activity.target?.id;
    if (
      // activity.type 必须相同且不为null或undefined
      c && u && c === u && // 如果都没有target，可以合并
      (!n?.target && !s.activity.target || // 如果都有target，则target.type和target.id都必须相同
      n?.target && s.activity.target && l === o && i === y)
    )
      a.push(s);
    else {
      if (a.length === 1)
        e.push(a[0]);
      else {
        const g = Object.assign({}, r, { items: a });
        e.push(g);
      }
      r = s, a = [s];
    }
  }), r)
    if (a.length === 1)
      e.push(a[0]);
    else {
      const s = Object.assign({}, r, { items: a });
      e.push(s);
    }
  return e;
}, j = (t) => !h(t.activity) && !!t.activity?.type && !!t.activity?.actor, O = (t) => {
  const { severity: e } = t || {};
  return !!e && ["normal", "success", "info"].includes(e);
}, P = (t) => {
  if (!t)
    return null;
  const { meta: e, target: r } = t;
  return {
    metaLink: e?.id,
    targetLink: r?.id
  };
}, L = async (t, e) => {
  try {
    return await T.user.getProfileUrl({ did: t, locale: e });
  } catch (r) {
    console.error(r);
    return;
  }
}, $ = async (t, e) => {
  const { type: r, did: a } = t;
  return d(r, "did") ? await L(a, e) || p(t) : p(t);
}, E = async (t, e, r = !0) => {
  const a = v(t);
  return await Promise.all(
    a.map(async (n) => {
      if (n instanceof f) {
        if (r) {
          const c = await $(n, e), { type: l, chainId: i, did: u } = n;
          return d(l, "dapp") ? `<em class="dapp" data-type="${l}" data-chain-id="${i}" data-did="${u}">${n.text}</em>` : c ? `<a target="_blank" rel="noopener noreferrer" class="link" href="${c}">${n.text}</a>` : `<em class="common" data-type="${l}" data-chain-id="${i}" data-did="${u}">${n.text}</em>`;
        }
        return n.text;
      }
      return n;
    })
  ).then((n) => n.join(""));
}, M = (t) => m.sanitize(t, {
  ALLOWED_ATTR: ["href", "target", "rel", "class", "id", "style"]
});
export {
  O as canAutoRead,
  P as getActivityLink,
  j as isIncludeActivity,
  b as mergeAdjacentNotifications,
  M as sanitize,
  E as toClickableSpan
};
