import { defineComponent as oe, inject as ie, ref as p, computed as w, watch as le, createElementBlock as r, openBlock as o, Fragment as me, createElementVNode as l, createBlock as c, unref as a, normalizeStyle as F, normalizeClass as y, createCommentVNode as i, createVNode as m, withCtx as d, createTextVNode as re, toDisplayString as T, Transition as b, withModifiers as $, Teleport as ue } from "vue";
import de from "../../1_atoms/ContextMenu/ContextMenu.vue.js";
import ce from "../../1_atoms/LinkPreview/LinkPreview.vue.js";
import ge from "../../1_atoms/EmbedPreview/EmbedPreview.vue.js";
import ve from "../BaseReplyMessage/BaseReplyMessage.vue.js";
import fe from "../../2_modals/ModalFullscreen/ModalFullscreen.vue.js";
import ye from "../MessageReactions/MessageReactions.vue.js";
import be from "../MessageStatusIndicator/MessageStatusIndicator.vue.js";
import ke from "../MessageSmsInvite/MessageSmsInvite.vue.js";
import pe from "../../1_atoms/Tooltip/Tooltip.vue.js";
import { useMessageActions as we } from "../../../hooks/messages/useMessageActions.js";
import { useMessageLinks as he } from "../../../hooks/messages/useMessageLinks.js";
import { useChannelAccentColor as xe } from "../../../hooks/messages/useChannelAccentColor.js";
import { useSubtextTooltip as Me } from "../../../hooks/messages/useSubtextTooltip.js";
import { getStatus as Te, getStatusTitle as Re } from "../../../functions/getStatusMessage.js";
import { getMessageClass as Ce } from "../../../functions/getMessageClass.js";
import { createReactionHandlers as Se } from "../../../functions/createReactionHandlers.js";
import "../../../functions/parseMarkdown.js";
import "../../../hooks/useMessageDraft.js";
import "../../../hooks/useSearchModel.js";
import { useTheme as Ie } from "../../../hooks/useTheme.js";
/* empty css                              */
/* empty css                                              */
const Ae = ["messageId"], Ee = ["src"], Le = {
  key: 1,
  class: "video-message__subtext"
}, Be = ["src"], Fe = {
  key: 0,
  class: "video-message__info-container"
}, $e = { class: "video-message__time" }, Pe = {
  key: 1,
  class: "video-message__text-container"
}, Oe = ["innerHTML"], je = ["src", "alt"], ms = /* @__PURE__ */ oe({
  inheritAttrs: !1,
  __name: "VideoMessage",
  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: P }) {
    const O = ie("chatAppId"), { getTheme: R } = Ie(O), t = e, h = P;
    function C(u) {
      return Ce(u.position, "video-message");
    }
    const S = p(), g = p(), x = p(!1), v = p(!1), { linkedHtml: j, inNewWindow: I } = he(() => t.message.text), {
      isOpenMenu: M,
      buttonMenuVisible: H,
      showMenu: N,
      hideMenu: A,
      clickAction: q,
      viewsAction: E,
      handleClickReplied: D
    } = we(t.message, h), U = () => {
      N(), v.value = !0;
    }, V = w(() => Te(t.message.status)), z = w(() => Re(t.message.status, t.message.statusMsg)), { bubbleStyle: W } = xe(
      w(() => t.message),
      { cssVariable: "--chotto-videomessage-right-bg", position: "right" }
    ), G = () => {
      g.value && (g.value.currentTime = 0, g.value.play());
    };
    le([S, g], ([u, s]) => {
      u ? s && (s.pause(), s.currentTime = 0) : s && (s.play(), s.currentTime = 0);
    });
    const J = w(() => t.message.reply && t.message.text ? "0" : t.message.text ? "5px 5px 0 0" : t.message.reply ? "0 0 5px 5px" : "8px"), K = () => x.value = !1, { onToggleReaction: Q, onAddReaction: X, onRemoveReaction: Y } = Se(h), Z = async () => {
      var u, s;
      if (t.message.url)
        try {
          const n = await fetch(t.message.url, {
            headers: {
              Accept: "video/*"
            }
          });
          if (!n.ok)
            throw new Error(`HTTP error! status: ${n.status}`);
          const se = n.headers.get("content-type") || "", te = await n.blob(), k = ((s = (u = t.message.url.split(".").pop()) == null ? void 0 : u.split("?")[0]) == null ? void 0 : s.toLowerCase()) || "", ae = {
            "video/mp4": "mp4",
            "video/webm": "webm",
            "video/ogg": "ogv",
            "video/quicktime": "mov",
            "video/x-matroska": "mkv",
            "application/octet-stream": k || "mp4"
          };
          let L = k && ["mp4", "mov", "webm", "ogv", "mkv"].includes(k) ? k : ae[se] || "mp4";
          const ne = t.message.alt ? t.message.alt.includes(".") ? t.message.alt : `${t.message.alt}.${L}` : `video-${t.message.messageId}.${L}`, B = window.URL.createObjectURL(te), f = document.createElement("a");
          f.href = B, f.download = ne, document.body.appendChild(f), f.click(), document.body.removeChild(f), window.URL.revokeObjectURL(B);
        } catch (n) {
          console.error("Ошибка при скачивании видео:", n), window.open(t.message.url, "_blank");
        }
    }, _ = Me(() => t.message, () => t.subtextTooltipData);
    function ee() {
      h("sms-invite", t.message);
    }
    return (u, s) => (o(), r(me, null, [
      l("div", {
        class: y(["video-message", [
          C(e.message),
          e.applyStyle(e.message)
        ]]),
        messageId: e.message.messageId,
        style: F(a(W)),
        onMouseleave: s[5] || (s[5] = //@ts-ignore
        (...n) => a(A) && a(A)(...n))
      }, [
        e.message.avatar && e.isFirstInSeries ? (o(), r("img", {
          key: 0,
          class: "video-message__avatar",
          src: e.message.avatar,
          height: "32",
          width: "32"
        }, null, 8, Ee)) : i("", !0),
        e.message.subText && e.isFirstInSeries ? (o(), r("p", Le, [
          m(pe, {
            text: a(_),
            position: e.message.position === "left" ? "right" : "left",
            offset: 8
          }, {
            default: d(() => [
              re(T(e.message.subText), 1)
            ]),
            _: 1
          }, 8, ["text", "position"])
        ])) : i("", !0),
        l("div", {
          class: y(["video-message__content", { "is-first": e.isFirstInSeries, "with-avatar-indent": !e.isFirstInSeries && e.message.avatar }])
        }, [
          e.message.reply ? (o(), c(ve, {
            key: 0,
            style: { margin: "10px 10px 4px 16px" },
            class: y(e.message.position),
            message: e.message.reply,
            onReply: a(D)
          }, null, 8, ["class", "message", "onReply"])) : i("", !0),
          l("div", {
            class: "video-message__preview-button",
            onClick: s[1] || (s[1] = (n) => x.value = !0),
            onMouseenter: U,
            onMouseleave: s[2] || (s[2] = (n) => v.value = !v.value)
          }, [
            l("video", {
              ref_key: "previewPlayer",
              ref: g,
              class: "video-message__video",
              style: F({ borderRadius: J.value }),
              src: e.message.url,
              muted: !0,
              autoplay: "",
              onEnded: G
            }, null, 44, Be),
            m(b, { name: "modal-fade" }, {
              default: d(() => [
                v.value ? (o(), r("div", Fe, [
                  e.message.views ? (o(), r("div", {
                    key: 0,
                    class: "video-message__views",
                    onClick: s[0] || (s[0] = $(
                      //@ts-ignore
                      (...n) => a(E) && a(E)(...n),
                      ["stop"]
                    ))
                  }, [
                    s[6] || (s[6] = l("span", { class: "pi pi-eye" }, null, -1)),
                    l("p", null, T(e.message.views), 1)
                  ])) : i("", !0),
                  l("span", $e, T(e.message.time), 1),
                  m(be, {
                    "base-class": "video-message",
                    "message-class": C(e.message),
                    "message-status": e.message.status,
                    "status-class": V.value,
                    "status-title": z.value
                  }, null, 8, ["message-class", "message-status", "status-class", "status-title"])
                ])) : i("", !0)
              ]),
              _: 1
            }),
            m(b, { name: "modal-fade" }, {
              default: d(() => [
                v.value ? (o(), r("button", {
                  key: 0,
                  class: "video-message__download-button",
                  type: "button",
                  onClick: $(Z, ["stop"])
                }, [...s[7] || (s[7] = [
                  l("span", { class: "pi pi-download" }, null, -1)
                ])])) : i("", !0)
              ]),
              _: 1
            })
          ], 32),
          m(b, { name: "modal-fade" }, {
            default: d(() => [
              a(H) && e.message.actions ? (o(), r("button", {
                key: 0,
                class: "video-message__menu-button",
                onClick: s[3] || (s[3] = (n) => M.value = !a(M))
              }, [...s[8] || (s[8] = [
                l("span", { class: "pi pi-ellipsis-h" }, null, -1)
              ])])) : i("", !0)
            ]),
            _: 1
          }),
          m(b, { name: "context-menu" }, {
            default: d(() => [
              a(M) && e.message.actions ? (o(), c(de, {
                key: 0,
                class: "video-message__context-menu",
                actions: e.message.actions,
                onClick: a(q)
              }, null, 8, ["actions", "onClick"])) : i("", !0)
            ]),
            _: 1
          }),
          e.message.text ? (o(), r("div", Pe, [
            l("p", {
              onClick: s[4] || (s[4] = //@ts-ignore
              (...n) => a(I) && a(I)(...n)),
              innerHTML: a(j)
            }, null, 8, Oe)
          ])) : i("", !0),
          m(ke, {
            status: e.message.status,
            "has-messenger-account": e.message.hasMessengerAccount,
            channel: e.channel,
            onSmsInvite: ee
          }, null, 8, ["status", "has-messenger-account", "channel"]),
          e.message.linkPreview ? (o(), c(ce, {
            key: 2,
            class: y(["video-message__link-preview", e.message.position]),
            "link-preview": e.message.linkPreview
          }, null, 8, ["class", "link-preview"])) : i("", !0),
          e.message.embed ? (o(), c(ge, {
            key: 3,
            class: y(e.message.position),
            embed: e.message.embed
          }, null, 8, ["class", "embed"])) : i("", !0),
          m(ye, {
            reactions: e.message.reactions,
            "message-id": e.message.messageId,
            enabled: e.reactionsEnabled,
            onToggleReaction: a(Q),
            onAddReaction: a(X),
            onRemoveReaction: a(Y)
          }, null, 8, ["reactions", "message-id", "enabled", "onToggleReaction", "onAddReaction", "onRemoveReaction"])
        ], 2)
      ], 46, Ae),
      (o(), c(ue, { to: "body" }, [
        m(b, { name: "modal-fade" }, {
          default: d(() => [
            x.value ? (o(), c(fe, {
              key: 0,
              "data-theme": a(R)().theme ? a(R)().theme : "light",
              title: e.message.alt,
              onClose: K
            }, {
              default: d(() => [
                l("video", {
                  ref_key: "player",
                  ref: S,
                  class: "video-message__modal-video",
                  src: e.message.url,
                  alt: e.message.alt,
                  controls: "",
                  autoplay: ""
                }, null, 8, je)
              ]),
              _: 1
            }, 8, ["data-theme", "title"])) : i("", !0)
          ]),
          _: 1
        })
      ]))
    ], 64));
  }
});
export {
  ms as default
};
