import { defineComponent as ht, inject as ft, useId as gt, ref as I, computed as p, watch as yt, onMounted as vt, nextTick as B, onUnmounted as pt, createElementBlock as s, openBlock as o, createElementVNode as r, createCommentVNode as l, withModifiers as kt, normalizeClass as b, normalizeStyle as W, createVNode as A, withCtx as V, toDisplayString as f, createBlock as m, resolveDynamicComponent as xt, Teleport as _t, unref as d, Fragment as Ct, renderList as wt } from "vue";
import { getStatus as It, statuses as Tt } from "../../../functions/getStatusMessage.js";
import "../../../functions/parseMarkdown.js";
import { t as U } from "../../../locale/useLocale.js";
import "../../../hooks/useMessageDraft.js";
import "../../../hooks/useSearchModel.js";
import { useTheme as Mt } from "../../../hooks/useTheme.js";
/* empty css                              */
/* empty css                                              */
import "../../../node_modules/linkifyjs/dist/linkify.js";
import "../../../node_modules/linkify-string/dist/linkify-string.js";
import $ from "../../1_atoms/Tooltip/Tooltip.vue.js";
import Et from "../../1_atoms/ButtonContextMenu/ButtonContextMenu.vue.js";
import St from "../../1_atoms/ContextMenu/ContextMenu.vue.js";
import Bt from "../../1_icons/AvatarIcon.vue.js";
import bt from "../../1_icons/MessageStatus/ErrorStatusIcon.vue.js";
import At from "../../1_icons/MessageStatus/PendingStatusIcon.vue.js";
import Vt from "../../1_icons/MessageStatus/ReadStatusIcon.vue.js";
import Ut from "../../1_icons/MessageStatus/ReceivedStatusIcon.vue.js";
import Lt from "../../1_icons/MessageStatus/SentStatusIcon.vue.js";
import Dt from "./icons/AudioIcon.vue.js";
import Nt from "./icons/FileIcon.vue.js";
import Rt from "./icons/ImageIcon.vue.js";
import Xt from "./icons/MissedCallIcon.vue.js";
import z from "./icons/StickerIcon.vue.js";
import Yt from "./icons/VideoIcon.vue.js";
import Ft from "./icons/VoiceIcon.vue.js";
const Ht = { class: "chat-item__avatar-container" }, Wt = ["src"], $t = {
  key: 1,
  class: "chat-item__avatar-placeholder"
}, zt = { class: "chat-item__info-container" }, Ot = { class: "chat-item__name" }, qt = {
  key: 0,
  class: "chat-item__last-message"
}, Pt = { class: "chat-item__last-message-text" }, Gt = { class: "chat-item__details-container" }, Jt = {
  key: 0,
  class: "chat-item__time"
}, Kt = { class: "chat-item__status-unread-container" }, Qt = {
  key: 1,
  class: "chat-item__unread"
}, Zt = {
  key: 2,
  class: "chat-item__status-chat-container"
}, jt = {
  key: 0,
  class: "chat-item__fixed pi pi-thumbtack"
}, te = {
  key: 0,
  id: "noSelectButton",
  class: "chat-item__menu-button"
}, ee = {
  key: 1,
  id: "noSelectButton",
  class: "chat-item__menu-button"
}, ne = {
  key: 0,
  class: "dialog__container"
}, ie = ["onClick"], oe = ["src"], ce = {
  key: 1,
  class: "dialog__icon pi pi-user"
}, se = { class: "dialog__text-container" }, ae = { class: "dialog__name" }, re = { class: "dialog__time" }, Ne = /* @__PURE__ */ ht({
  __name: "ChatItem",
  props: {
    chat: {},
    showDialogs: { type: Boolean, default: !0 },
    contextMenuTrigger: { default: "hover" }
  },
  emits: ["select", "action", "expand"],
  setup(c, { emit: O }) {
    const q = ft("chatAppId"), { getTheme: L } = Mt(q), e = c, T = O, g = gt(), y = I(!1), k = I(!1), E = I(!1), P = I(null), G = () => {
      E.value = !0;
    }, J = (t) => {
      E.value || t.target instanceof HTMLElement && t.target.id != "noSelectButton" && T("select", { chat: e.chat, dialog: null }), E.value = !1;
    }, K = (t) => {
      T("select", { chat: e.chat, dialog: t });
    }, Q = () => e.chat.isSelected ? "chat-item__selected" : "", Z = (t) => t.isSelected ? "dialog__selected" : "", D = (t) => {
      e.contextMenuTrigger === "rightClick" && S(), T("action", { chat: e.chat, ...t });
    }, S = () => {
      const t = document.getElementById("context-menu-rightclick-" + g);
      t && (t.style.top = "0", t.style.left = "0", t.style.opacity = "0", t.style.display = "none"), k.value = !1;
    }, j = () => {
      document.querySelectorAll('[id^="context-menu-rightclick-"]').forEach((n) => {
        const i = n;
        i.id !== "context-menu-rightclick-" + g && (i.style.top = "0", i.style.left = "0", i.style.opacity = "0", i.style.display = "none");
      });
    }, tt = () => e.chat.dialogs ? [...e.chat.dialogs].sort((t, n) => Number(t["lastActivity.timestamp"]) > Number(n["lastActivity.timestamp"]) ? -1 : Number(t["lastActivity.timestamp"]) < Number(n["lastActivity.timestamp"]) ? 1 : (Number(t["lastActivity.timestamp"]) == Number(n["lastActivity.timestamp"]), 0)) : [], et = p(() => It(e.chat["lastMessage.status"])), nt = p(() => e.chat.showEmptyIndicator || e.chat.countUnread > 0), it = p(() => e.chat.countUnread > 0 ? e.chat.countUnread > 99 ? "99+" : e.chat.countUnread : e.chat.showEmptyIndicator ? "" : void 0);
    let M;
    const v = I(0), N = [U("component.ChatItem.typing") + ".", U("component.ChatItem.typing") + "..", U("component.ChatItem.typing") + "..."], R = (t) => {
      var u, a, h;
      if (typeof t == "string")
        return t;
      if ((t == null ? void 0 : t.type) === "message.sticker")
        return "Стикер";
      if ((t == null ? void 0 : t.type) === "message.audio")
        return (t.isVoiceMessage || ((u = t == null ? void 0 : t.data) == null ? void 0 : u.isVoiceMessage)) === !0 ? "Голосовое сообщение" : "Аудиофайл";
      if ((t == null ? void 0 : t.type) === "message.missedCall")
        return "Пропущенный звонок";
      const n = (a = t == null ? void 0 : t.data) == null ? void 0 : a.text;
      if (n)
        return n;
      const i = (h = t == null ? void 0 : t.data) == null ? void 0 : h.filename;
      return i || "";
    }, ot = (t) => typeof t == "string" ? null : (t == null ? void 0 : t.type) || null, X = p(() => {
      var n, i, u;
      if (e.chat.typing)
        return null;
      const t = ot(e.chat.lastMessage);
      if (!t)
        return null;
      if (t === "message.sticker")
        return z;
      if (t === "message.image")
        return Rt;
      if (t === "message.video")
        return Yt;
      if (t === "message.audio") {
        const a = e.chat.lastMessage;
        return (a.isVoiceMessage || ((n = a == null ? void 0 : a.data) == null ? void 0 : n.isVoiceMessage)) === !0 ? Ft : Dt;
      }
      if (t === "message.missedCall")
        return Xt;
      if (t === "message.file") {
        const a = e.chat.lastMessage, h = ((u = (i = a == null ? void 0 : a.data) == null ? void 0 : i.filename) == null ? void 0 : u.toLowerCase()) || "";
        return h.endsWith(".tgs") || h.endsWith(".webp") ? z : Nt;
      }
      return null;
    }), ct = p(() => e.chat.typing ? N[v.value] : R(e.chat.lastMessage)), st = p(() => e.chat.typing ? N[v.value] : R(e.chat.lastMessage));
    yt(
      () => e.chat.typing,
      () => {
        e.chat.typing ? M = setInterval(() => {
          v.value < 2 ? v.value += 1 : v.value = 0;
        }, 1e3) : (v.value = 0, M && (clearInterval(M), M = void 0));
      },
      { immediate: !0 }
    );
    const at = () => {
      e.contextMenuTrigger === "hover" && (y.value = !0);
    }, rt = (t) => {
      e.contextMenuTrigger === "hover" && (t.relatedTarget instanceof HTMLElement && t.relatedTarget.className == "context-menu__list" ? y.value = !0 : y.value = !1);
    }, lt = (t) => {
      e.contextMenuTrigger === "rightClick" && e.chat.actions && ut(t);
    }, ut = (t) => {
      j(), k.value = !0, B(() => {
        const n = document.getElementById("context-menu-rightclick-" + g);
        if (n) {
          const i = t.clientX, u = t.clientY;
          n.style.display = "inherit";
          const a = n.getBoundingClientRect();
          n.style.display = "none", B(() => {
            const h = window.innerWidth, H = window.innerHeight, x = window.scrollX || window.pageXOffset, _ = window.scrollY || window.pageYOffset;
            let C = i + x, w = u + _;
            C + a.width > h + x && (C = i + x - a.width), w + a.height > H + _ && (w = u + _ - a.height), C < x && (C = x), w < _ && (w = _), n.style.top = w + "px", n.style.left = C + "px", n.style.opacity = "1", n.style.display = "inherit";
          });
        }
      });
    }, mt = () => {
    }, dt = () => {
    }, Y = (t) => {
      if (e.contextMenuTrigger === "rightClick" && k.value) {
        const n = t.target, i = document.getElementById("context-menu-rightclick-" + g);
        (!i || !i.contains(n)) && S();
      }
    }, F = () => {
      e.contextMenuTrigger === "rightClick" && k.value && S();
    };
    return vt(() => {
      e.contextMenuTrigger === "rightClick" && (document.addEventListener("click", Y), window.addEventListener("scroll", F, !0), B(() => {
        const t = document.getElementById("context-menu-rightclick-" + g);
        t && (t.style.display = "none", t.style.opacity = "0");
      }));
    }), pt(() => {
      document.removeEventListener("click", Y), window.removeEventListener("scroll", F, !0);
    }), (t, n) => (o(), s("div", null, [
      r("div", {
        ref_key: "containerRef",
        ref: P,
        class: b(["chat-item__container", Q()]),
        onMouseenter: at,
        onMouseleave: rt,
        onClick: J,
        onContextmenu: kt(lt, ["prevent"])
      }, [
        r("div", Ht, [
          r("span", {
            class: "chat-item__status-user",
            style: W({ backgroundColor: e.chat.status })
          }, null, 4),
          e.chat.avatar ? (o(), s("img", {
            key: 0,
            src: e.chat.avatar,
            height: "48",
            width: "48"
          }, null, 8, Wt)) : (o(), s("div", $t, [
            A(Bt)
          ]))
        ]),
        r("div", zt, [
          A($, {
            text: c.chat.name,
            position: "bottom"
          }, {
            default: V(() => [
              r("div", Ot, f(c.chat.name), 1)
            ]),
            _: 1
          }, 8, ["text"]),
          A($, {
            text: ct.value,
            position: "bottom"
          }, {
            default: V(() => [
              c.chat.lastMessage || c.chat.typing ? (o(), s("div", qt, [
                X.value ? (o(), m(xt(X.value), {
                  key: 0,
                  class: "chat-item__message-icon"
                })) : l("", !0),
                r("span", Pt, f(st.value), 1)
              ])) : l("", !0)
            ]),
            _: 1
          }, 8, ["text"])
        ]),
        r("div", Gt, [
          c.chat["lastActivity.time"] && (!c.chat.actions || !y.value) ? (o(), s("div", Jt, f(c.chat["lastActivity.time"]), 1)) : l("", !0),
          y.value && c.chat.actions && c.contextMenuTrigger === "hover" ? (o(), m(Et, {
            key: 1,
            mode: "click",
            "menu-side": "bottom-right",
            actions: c.chat.actions,
            onClick: D,
            onButtonClick: G,
            onMenuMouseLeave: n[0] || (n[0] = (i) => y.value = !1)
          }, {
            default: V(() => [...n[2] || (n[2] = [
              r("span", { class: "pi pi-ellipsis-h chat-item__actions-trigger" }, null, -1)
            ])]),
            _: 1
          }, 8, ["actions"])) : l("", !0),
          (o(), m(_t, { to: "body" }, [
            c.contextMenuTrigger === "rightClick" && c.chat.actions && k.value ? (o(), m(St, {
              key: 0,
              id: "context-menu-rightclick-" + d(g),
              actions: c.chat.actions,
              "data-theme": d(L)().theme ? d(L)().theme : "light",
              onClick: D,
              onMouseenter: mt,
              onMouseleave: dt
            }, null, 8, ["id", "actions", "data-theme"])) : l("", !0)
          ])),
          r("div", Kt, [
            d(Tt).includes(c.chat["lastMessage.status"]) ? (o(), s("div", {
              key: 0,
              class: b(["chat-item__status-message", et.value])
            }, [
              c.chat["lastMessage.status"] === "pending" ? (o(), m(d(At), { key: 0 })) : c.chat["lastMessage.status"] === "error" ? (o(), m(d(bt), { key: 1 })) : c.chat["lastMessage.status"] === "read" ? (o(), m(d(Vt), { key: 2 })) : c.chat["lastMessage.status"] === "received" ? (o(), m(d(Ut), { key: 3 })) : (o(), m(d(Lt), { key: 4 }))
            ], 2)) : l("", !0),
            nt.value ? (o(), s("div", Qt, f(it.value), 1)) : l("", !0)
          ]),
          c.chat.countUnread < 1 ? (o(), s("div", Zt, [
            c.chat.isFixedTop || c.chat.isFixedBottom ? (o(), s("span", jt)) : l("", !0)
          ])) : l("", !0)
        ]),
        e.showDialogs && c.chat.dialogs ? (o(), s("div", {
          key: 0,
          class: "chat-item__dialog-buttons",
          onClick: n[1] || (n[1] = (i) => T("expand", e.chat))
        }, [
          e.showDialogs && !c.chat.dialogsExpanded ? (o(), s("button", te, [...n[3] || (n[3] = [
            r("span", {
              id: "noSelectButton",
              class: "pi pi-angle-down"
            }, null, -1)
          ])])) : l("", !0),
          e.showDialogs && c.chat.dialogsExpanded ? (o(), s("button", ee, [...n[4] || (n[4] = [
            r("span", {
              id: "noSelectButton",
              class: "pi pi-angle-up"
            }, null, -1)
          ])])) : l("", !0)
        ])) : l("", !0)
      ], 34),
      e.showDialogs && c.chat.dialogsExpanded ? (o(), s("div", ne, [
        (o(!0), s(Ct, null, wt(tt(), (i) => (o(), s("div", {
          key: i.dialogId,
          class: b(["dialog__item", Z(i)]),
          onClick: (u) => K(i)
        }, [
          i.icon ? (o(), s("img", {
            key: 0,
            class: "dialog__icon",
            src: i.icon,
            height: "16",
            width: "16"
          }, null, 8, oe)) : (o(), s("span", ce)),
          r("div", se, [
            r("div", ae, f(i.name), 1),
            r("div", re, f(i["lastActivity.time"]), 1)
          ]),
          i.countUnread > 0 ? (o(), s("div", {
            key: 2,
            class: "chat-item__unread",
            style: W(i.colorUnread ? { backgroundColor: i.colorUnread } : {})
          }, f(i.countUnread > 99 ? "99+" : i.countUnread), 5)) : l("", !0)
        ], 10, ie))), 128))
      ])) : l("", !0)
    ]));
  }
});
export {
  Ne as default
};
