import { defineComponent as D, computed as y, createElementBlock as r, openBlock as l, unref as t, normalizeStyle as k, normalizeClass as u, createCommentVNode as o, createElementVNode as n, createVNode as d, withCtx as T, createTextVNode as z, toDisplayString as f, createBlock as b, withModifiers as W, Transition as p } from "vue";
import G from "../../1_atoms/ContextMenu/ContextMenu.vue.js";
import J from "../../1_atoms/LinkPreview/LinkPreview.vue.js";
import K from "../../1_atoms/EmbedPreview/EmbedPreview.vue.js";
import Q from "../BaseReplyMessage/BaseReplyMessage.vue.js";
import X from "../MessageReactions/MessageReactions.vue.js";
import Y from "../MessageStatusIndicator/MessageStatusIndicator.vue.js";
import Z from "../MessageSmsInvite/MessageSmsInvite.vue.js";
import _ from "../../1_atoms/Tooltip/Tooltip.vue.js";
import { useMessageActions as ee } from "../../../hooks/messages/useMessageActions.js";
import { useMessageLinks as se } from "../../../hooks/messages/useMessageLinks.js";
import { useChannelAccentColor as te } from "../../../hooks/messages/useChannelAccentColor.js";
import { useSubtextTooltip as ae } from "../../../hooks/messages/useSubtextTooltip.js";
import { getStatus as ie, getStatusTitle as ne } from "../../../functions/getStatusMessage.js";
import { getMessageClass as le } from "../../../functions/getMessageClass.js";
import { createReactionHandlers as oe } from "../../../functions/createReactionHandlers.js";
import "../../../functions/parseMarkdown.js";
const me = ["messageId"], re = ["src"], ge = {
  key: 1,
  class: "file-message__subtext"
}, ce = ["href"], ue = { class: "file-message__filename-text" }, de = {
  key: 1,
  class: "file-message__text-container"
}, fe = ["innerHTML"], be = { class: "file-message__info-container" }, ve = { class: "file-message__time" }, Ue = /* @__PURE__ */ D({
  __name: "FileMessage",
  props: {
    message: {
      type: Object,
      required: !0
    },
    applyStyle: {
      type: Function,
      default: () => null
    },
    isFirstInSeries: {
      type: Boolean,
      default: !0
    },
    reactionsEnabled: {
      type: Boolean,
      default: !0
    },
    subtextTooltipData: {
      type: Object,
      required: !1,
      default: () => ({})
    },
    channel: {
      type: String,
      required: !1,
      default: void 0
    }
  },
  emits: ["action", "reply", "sms-invite"],
  setup(e, { emit: I }) {
    const a = e, v = I, { linkedHtml: L, inNewWindow: h } = se(() => a.message.text), {
      isOpenMenu: w,
      buttonMenuVisible: A,
      showMenu: R,
      hideMenu: S,
      clickAction: E,
      viewsAction: x,
      handleClickReplied: F
    } = ee(a.message, v), B = y(() => ie(a.message.status)), O = y(() => ne(a.message.status, a.message.statusMsg)), { bubbleStyle: U } = te(
      y(() => a.message),
      { cssVariable: "--chotto-filemessage-right-bg", position: "right" }
    );
    function C(m) {
      return le(m.position, "file-message");
    }
    const { onToggleReaction: V, onAddReaction: H, onRemoveReaction: N } = oe(v), P = async () => {
      if (a.message.url)
        try {
          const m = new URL(a.message.url, window.location.href);
          if (m.origin === window.location.origin) {
            const c = document.createElement("a");
            c.href = m.toString(), c.download = a.message.filename || `file-${a.message.messageId}`, document.body.appendChild(c), c.click(), document.body.removeChild(c);
            return;
          }
          const i = await fetch(a.message.url);
          if (!i.ok)
            throw new Error(`HTTP error! status: ${i.status}`);
          const q = await i.blob(), M = window.URL.createObjectURL(q), g = document.createElement("a");
          g.href = M, g.download = a.message.filename || `file-${a.message.messageId}`, document.body.appendChild(g), g.click(), document.body.removeChild(g), window.URL.revokeObjectURL(M);
        } catch (m) {
          console.error("Ошибка при скачивании файла:", m), window.open(a.message.url, "_blank");
        }
    }, j = ae(() => a.message, () => a.subtextTooltipData);
    function $() {
      v("sms-invite", a.message);
    }
    return (m, s) => (l(), r("div", {
      class: u(["file-message", [
        C(e.message),
        e.applyStyle(e.message)
      ]]),
      messageId: e.message.messageId,
      style: k(t(U)),
      onMouseleave: s[4] || (s[4] = //@ts-ignore
      (...i) => t(S) && t(S)(...i))
    }, [
      e.message.avatar && e.isFirstInSeries ? (l(), r("img", {
        key: 0,
        class: "file-message__avatar",
        src: e.message.avatar,
        height: "32",
        width: "32",
        style: k({ gridRow: e.message.subText ? "2" : "1" })
      }, null, 12, re)) : o("", !0),
      e.message.subText && e.isFirstInSeries ? (l(), r("p", ge, [
        d(_, {
          text: t(j),
          position: e.message.position === "left" ? "right" : "left",
          offset: 8
        }, {
          default: T(() => [
            z(f(e.message.subText), 1)
          ]),
          _: 1
        }, 8, ["text", "position"])
      ])) : o("", !0),
      n("div", {
        class: u(["file-message__content", { "is-first": e.isFirstInSeries, "with-avatar-indent": !e.isFirstInSeries && e.message.avatar }]),
        style: k({ gridRow: e.message.subText ? "2" : "1" }),
        onMouseenter: s[3] || (s[3] = //@ts-ignore
        (...i) => t(R) && t(R)(...i))
      }, [
        e.message.reply ? (l(), b(Q, {
          key: 0,
          message: e.message.reply,
          class: u(e.message.position),
          onReply: t(F)
        }, null, 8, ["message", "class", "onReply"])) : o("", !0),
        n("a", {
          class: "file-message__link",
          href: e.message.url,
          onClick: W(P, ["prevent"])
        }, [
          s[5] || (s[5] = n("span", { class: "pi pi-file" }, null, -1)),
          n("p", ue, f(e.message.filename), 1),
          s[6] || (s[6] = n("div", { class: "file-message__download-button" }, [
            n("span", { class: "pi pi-download" })
          ], -1))
        ], 8, ce),
        e.message.text ? (l(), r("div", de, [
          n("p", {
            onClick: s[0] || (s[0] = //@ts-ignore
            (...i) => t(h) && t(h)(...i)),
            innerHTML: t(L)
          }, null, 8, fe)
        ])) : o("", !0),
        e.message.linkPreview ? (l(), b(J, {
          key: 2,
          class: u(e.message.position),
          "link-preview": e.message.linkPreview
        }, null, 8, ["class", "link-preview"])) : o("", !0),
        e.message.embed ? (l(), b(K, {
          key: 3,
          class: u(e.message.position),
          embed: e.message.embed
        }, null, 8, ["class", "embed"])) : o("", !0),
        d(X, {
          reactions: e.message.reactions,
          "message-id": e.message.messageId,
          enabled: e.reactionsEnabled,
          onToggleReaction: t(V),
          onAddReaction: t(H),
          onRemoveReaction: t(N)
        }, null, 8, ["reactions", "message-id", "enabled", "onToggleReaction", "onAddReaction", "onRemoveReaction"]),
        n("div", be, [
          e.message.views ? (l(), r("div", {
            key: 0,
            class: "file-message__views",
            onClick: s[1] || (s[1] = //@ts-ignore
            (...i) => t(x) && t(x)(...i))
          }, [
            s[7] || (s[7] = n("span", { class: "pi pi-eye" }, null, -1)),
            n("p", null, f(e.message.views), 1)
          ])) : o("", !0),
          n("span", ve, f(e.message.time), 1),
          d(Y, {
            "base-class": "file-message",
            "message-class": C(e.message),
            "message-status": e.message.status,
            "status-class": B.value,
            "status-title": O.value
          }, null, 8, ["message-class", "message-status", "status-class", "status-title"])
        ]),
        d(Z, {
          status: e.message.status,
          "has-messenger-account": e.message.hasMessengerAccount,
          channel: e.channel,
          onSmsInvite: $
        }, null, 8, ["status", "has-messenger-account", "channel"]),
        t(A) && e.message.actions ? (l(), r("button", {
          key: 4,
          class: "file-message__menu-button",
          onClick: s[2] || (s[2] = (i) => w.value = !t(w))
        }, [...s[8] || (s[8] = [
          n("span", { class: "pi pi-ellipsis-h" }, null, -1)
        ])])) : o("", !0),
        d(p, null, {
          default: T(() => [
            t(w) && e.message.actions ? (l(), b(G, {
              key: 0,
              class: "file-message__context-menu",
              actions: e.message.actions,
              onClick: t(E)
            }, null, 8, ["actions", "onClick"])) : o("", !0)
          ]),
          _: 1
        })
      ], 38)
    ], 46, me));
  }
});
export {
  Ue as default
};
