import { formatUnits as v } from "ethers";
import { getTokenBalanceKey as w } from "../intents/getTokenBalanceKey.js";
const C = (t, e) => e.length === 0 ? !1 : typeof e[0] == "string" ? e.includes(t.assetId) : e.some(
  ([o, c]) => t.blockchain === o && t.symbol.toLowerCase() === c.toLowerCase()
), L = (t, e, o) => {
  if (t.isIntent || e.isIntent || o.length === 0)
    return null;
  const c = (r) => typeof o[0] == "string" ? o.indexOf(r.assetId) : o.findIndex(
    ([n, a]) => r.blockchain === n && r.symbol.toLowerCase() === a.toLowerCase()
  ), s = C(t, o), i = C(e, o);
  if (s && !i)
    return -1;
  if (!s && i)
    return 1;
  if (s && i) {
    const r = c(t), n = c(e);
    if (r !== n)
      return r - n;
  }
  return null;
}, k = (t, e, o) => {
  if (t.isIntent && !e.isIntent)
    return -1;
  if (!t.isIntent && e.isIntent)
    return 1;
  if (!o) {
    const n = (t.symbol ?? "").localeCompare(
      e.symbol ?? "",
      void 0,
      {
        sensitivity: "base"
      }
    );
    return n !== 0 ? n : t.chainName.localeCompare(e.chainName, void 0, {
      sensitivity: "base"
    });
  }
  const c = (n) => n === o ? 0 : n.startsWith(o) ? 1 : n.includes(o) ? 2 : 3, s = c(t.symbol.toLowerCase()), i = c(e.symbol.toLowerCase());
  if (s !== i)
    return s - i;
  const r = t.symbol.toLowerCase().localeCompare(e.symbol.toLowerCase());
  return r !== 0 ? r : t.chainName.toLowerCase().localeCompare(e.chainName.toLowerCase());
}, b = (t, e, o, c, s, i, r, n) => {
  const a = r.isIntent || n.isIntent, f = o !== void 0 && o > 0, m = c !== void 0 && c > 0;
  if (f && !m)
    return -1;
  if (!f && m)
    return 1;
  if (f && m) {
    const l = t.includes(r.blockchain), u = t.includes(n.blockchain);
    return !a && l && !u ? -1 : !a && !l && u ? 1 : o !== c ? (c ?? 0) - (o ?? 0) : k(r, n, s);
  }
  const d = L(r, n, e);
  if (d !== null)
    return d;
  if (!(e.length > 0 && (C(r, e) || C(n, e))) && i && i.size > 0) {
    const l = i.get(r.assetId), u = i.get(n.assetId), h = l !== void 0, y = u !== void 0;
    if (h && !y)
      return -1;
    if (!h && y)
      return 1;
    if (h && y && l !== u)
      return l - u;
  }
  const I = t.includes(r.blockchain), g = t.includes(n.blockchain);
  return !a && I && !g ? -1 : !a && !I && g ? 1 : k(r, n, s);
}, S = (t, e, o = /* @__PURE__ */ new Map(), c, s = []) => (i, r) => {
  const n = t[w(i)], a = t[w(r)], f = n ? v(n, i.decimals) : "0", m = a ? v(a, r.decimals) : "0", d = n !== void 0 ? parseFloat(`${f ?? 0}`) * i.price : void 0, p = a !== void 0 ? parseFloat(`${m ?? 0}`) * r.price : void 0, I = c?.trim().toLowerCase() ?? void 0;
  return b(
    e,
    s,
    d,
    p,
    I,
    o,
    i,
    r
  );
};
export {
  S as createTokenSorter
};
//# sourceMappingURL=sort.js.map
