var N = (r, a, c) => new Promise((i, s) => {
  var n = (t) => {
    try {
      l(c.next(t));
    } catch (d) {
      s(d);
    }
  }, h = (t) => {
    try {
      l(c.throw(t));
    } catch (d) {
      s(d);
    }
  }, l = (t) => t.done ? i(t.value) : Promise.resolve(t.value).then(n, h);
  l((c = c.apply(r, a)).next());
});
import { jsx as S, jsxs as k, Fragment as B } from "react/jsx-runtime";
import { useRef as F, useEffect as I } from "react";
import { g as x, h as _, V as G, ac as y, aa as L, aC as M, x as w, e as j, ff as O, fg as E, ai as R, fh as v, cT as W, aF as q, fi as z, a9 as Q } from "./main-CxoAwMiG.js";
import { u as V, J as $ } from "./JoinThreadDialog-N2K5Qro0.js";
function H() {
  const { handleToggle: r } = V(), a = x(), c = _((s) => s.threads.threads);
  function i() {
    const s = c.find((n) => n.type === G.SELF);
    if (s) {
      a(y(s)), a(L(!1)), r(!0);
      return;
    }
    a(M()).unwrap().then(({ thread: n }) => {
      a(y(n)), r(!0), a(L(!1));
    }).catch((n) => {
      console.error("Create self thread failed:", n);
    });
  }
  return { onClickOnSelfThread: i };
}
const K = () => {
  const r = w(), a = x(), { openPrivateGroupThreadDialog: c } = _(
    (s) => s.threads
  );
  return /* @__PURE__ */ S(
    j,
    {
      open: c,
      onOpenChange: () => {
        r("/", { replace: !0 }), a(O(!1));
      },
      title: "لینک نا معتبر",
      confirmText: "تایید",
      onConfirm: () => {
        a(O(!1)), r("/", { replace: !0 });
      },
      size: "sm",
      closable: !1,
      children: /* @__PURE__ */ S("div", { className: "mt-4 h-6 text-invertBlackWhite", children: "دسترسی به این گروه برای شما امکان‌پذیر نیست." })
    }
  );
}, X = () => {
  const r = w(), a = x(), { openP2PThreadDialog: c } = _(
    (u) => u.threads
  ), [i] = E(), s = R();
  let n = i.get("uid");
  const h = i.get("state");
  if (h && !n && h.startsWith("uid_")) {
    const u = h.match(/uid_(.*?)_-_/);
    n = u ? u[1] : null;
  }
  const l = n === "self";
  let t = null;
  if (l)
    t = {};
  else if (n)
    try {
      t = JSON.parse(atob(n));
    } catch (u) {
      t = null;
    }
  return /* @__PURE__ */ S(
    j,
    {
      open: c,
      onOpenChange: () => {
        r("/", { replace: !0 }), a(v(!1));
      },
      title: `مایل به ارسال درخواست گفتگو هستید؟
`,
      confirmText: "تایید",
      cancelText: "انصراف",
      onConfirm: () => N(null, null, function* () {
        var p;
        const u = (b) => {
          const D = atob(b), f = Uint8Array.from(D, (C) => C.charCodeAt(0));
          return new TextDecoder().decode(f);
        };
        let g = [];
        t != null && t.pr ? g = [{ id: t == null ? void 0 : t.pr, idType: "TO_BE_USER_ID" }] : t != null && t.t && (g = [u(t == null ? void 0 : t.t)]);
        const T = yield a(
          W({
            threadType: "p2p",
            ids: g
          })
        );
        a(y((p = T == null ? void 0 : T.payload) == null ? void 0 : p.thread)), a(v(!1));
        const o = s.pathname === "/folders" ? "thread" : s.pathname === "/thread" ? "" : "thread";
        r(s.pathname + o);
      }),
      onCancel: () => {
        r("/", { replace: !0 }), a(v(!1));
      },
      size: "sm",
      closable: !1
    }
  );
}, ee = () => {
  const r = x(), a = R(), c = w(), [i] = E(), { onClickOnSelfThread: s } = H(), { threads: n, thread: h } = _(
    (o) => o.threads
  );
  let l = i.get("uid");
  const t = i.get("state");
  if (t && !l && t.startsWith("uid_")) {
    const o = t.match(/uid_(.*?)_-_/);
    l = o ? o[1] : null;
  }
  const d = i.get("tn");
  let e = null;
  const u = F(null), g = F(!0);
  if (l)
    try {
      e = JSON.parse(atob(l));
    } catch (o) {
      e = null;
    }
  const T = l === "self" || q() === (e == null ? void 0 : e.pr);
  return T && (e = {}), I(() => {
    if (a.pathname === "/folders") {
      const o = new URLSearchParams(a.search);
      o.delete("thread");
      const p = o.toString();
      c("/thread" + (p ? `?${p}` : ""), { replace: !0 });
    }
  }, []), I(() => {
    const o = new URLSearchParams(a.search);
    if (n != null && n.length && (o.has("uid") || o.has("state"))) {
      if (g.current && (g.current = !1), l && !e && !T) {
        c("/", { replace: !0 });
        return;
      }
      let p = null;
      e != null && e.pr ? p = n.find(
        (f) => f.partner === Number(e.pr)
      ) : e != null && e.i && (p = n.find(
        (f) => f.id === Number(e.i)
      ));
      const b = () => {
        const { pathname: f } = a, C = f === "/folders" ? "thread" : f === "/thread" ? "" : "thread";
        c(a.pathname + C, { replace: !1 });
      }, D = () => N(null, null, function* () {
        var f;
        if (p)
          r(y(p));
        else if (T)
          s();
        else if ((e != null && e.pr || e != null && e.t) && !h) {
          r(v(!0));
          return;
        } else if ((e == null ? void 0 : e.p) === 1 || d && a.pathname === "/join") {
          r(
            z({
              uniqueName: d,
              open: !0
            })
          );
          return;
        } else if ((e == null ? void 0 : e.p) === 0) {
          const C = Number(e.i);
          let m = n.find((A) => A.id === C);
          if (m || (m = (f = (yield r(
            Q({
              archived: null,
              count: 50,
              offset: 0,
              threadIds: [C]
            })
          )).payload) == null ? void 0 : f[0]), m) {
            if ((m == null ? void 0 : m.id) === (h == null ? void 0 : h.id))
              return;
            r(y(m));
          } else {
            r(O(!0));
            return;
          }
        }
        b();
      });
      (l !== u.current || a.pathname === "/join") && (D(), u.current = l);
    }
  }, [n == null ? void 0 : n.length]), /* @__PURE__ */ k(B, { children: [
    /* @__PURE__ */ S($, {}),
    /* @__PURE__ */ S(K, {}),
    /* @__PURE__ */ S(X, {})
  ] });
};
export {
  ee as default
};
