import { defineComponent as D, inject as N, ref as o, computed as T, watch as j, createElementBlock as y, openBlock as c, createCommentVNode as m, createBlock as M, createElementVNode as a, toDisplayString as x, unref as w, normalizeClass as L, Teleport as P } from "vue";
import { uploadFile as O } from "../../../hooks/uploadFile/uploadFile.js";
import { useMessageDraft as q } from "../../../hooks/useMessageDraft.js";
import "../../../hooks/useSearchModel.js";
import "../../../hooks/useTheme.js";
/* empty css                              */
/* empty css                                              */
import "../../../node_modules/linkifyjs/dist/linkify.js";
import "../../../node_modules/linkify-string/dist/linkify-string.js";
import "../../../functions/parseMarkdown.js";
import G from "../../2_chatinput_elements/FilePreview/FilePreview.vue.js";
const H = { class: "audio-recorder__container" }, J = {
  key: 0,
  class: "audio-recorder__status"
}, K = {
  key: 1,
  class: "audio-recorder__recording-container"
}, Q = { class: "audio-recorder__recording-time" }, re = /* @__PURE__ */ D({
  __name: "AudioRecorder",
  props: {
    state: {
      type: String,
      default: "active"
    },
    filebumpUrl: {
      type: String,
      default: null
    }
  },
  setup(R) {
    const f = N("chatAppId"), { getMessage: n, setMessageFile: U, resetMessageFile: S, setRecordingMessage: k } = q(f), h = o(), l = o(0), r = o(0), s = o(0), d = o(0), z = T(() => {
      let t = d.value < 10 ? "0" + d.value : d.value, e = s.value < 10 ? "0" + s.value : s.value, i = r.value < 10 ? "0" + r.value : r.value, _ = l.value < 10 ? "00" + l.value : l.value < 100 ? "0" + l.value : l.value;
      return t + ":" + e + ":" + i + ":" + _;
    }), v = o(""), g = o(!1), u = o(), b = o([]), A = o(), p = o(), C = R, B = async () => {
      if (!n().file && C.state == "active") {
        const t = await navigator.mediaDevices.getUserMedia({ audio: !0, video: !1 });
        h.value = setInterval(() => {
          l.value += 10, l.value == 1e3 && (l.value = 0, r.value++, r.value == 60 && (r.value = 0, s.value++, s.value == 60 && (s.value = 0, d.value++)));
        }, 10), k(!0), g.value = !0, u.value = new MediaRecorder(t), u.value.start(), u.value.ondataavailable = (e) => {
          b.value.push(e.data);
        };
      }
    }, F = () => {
      I(), A.value = void 0;
    }, E = () => {
      u.value && (u.value.stop(), u.value.onstop = async () => {
        const t = new File(b.value, "voicemessage.mp3", { type: "audio/*" }), e = URL.createObjectURL(t);
        A.value = e, v.value = "uploading", await O(C.filebumpUrl, t).then((i) => {
          if (v.value = i.status, i.status == "success") {
            const _ = document.getElementById("chat-input-file-line-" + f);
            _ && (_.style.display = "inherit"), U({
              url: i.url,
              name: i.name,
              size: i.size,
              type: i.type
            }), i.preview && (p.value = {
              previewUrl: i.preview.previewUrl,
              isImage: i.preview.isImage,
              isVideo: i.preview.isVideo,
              isAudio: i.preview.isAudio,
              fileName: i.name,
              fileSize: i.preview.fileSize
            });
          }
        });
      }), I();
    }, I = () => {
      clearInterval(h.value), l.value = 0, r.value = 0, s.value = 0, d.value = 0, k(!1), g.value = !1, u.value = void 0, b.value = [];
    }, V = () => {
      const t = document.getElementById("chat-input-file-line-" + f);
      t && (t.style.display = "none"), S(), p.value = void 0;
    };
    return j(
      () => n().file,
      () => {
        n().file || (p.value = void 0);
      }
    ), (t, e) => (c(), y("div", H, [
      v.value === "uploading" ? (c(), y("div", J, [...e[0] || (e[0] = [
        a("span", null, "Загрузка файла...", -1)
      ])])) : m("", !0),
      g.value && v.value != "uploading" ? (c(), y("div", K, [
        e[3] || (e[3] = a("span", { class: "audio-recorder__recording-icon pi pi-circle-fill" }, null, -1)),
        a("button", {
          class: "audio-recorder__button audio-recorder__button-record",
          onClick: F
        }, [...e[1] || (e[1] = [
          a("span", { class: "pi pi-trash" }, null, -1)
        ])]),
        a("button", {
          class: "audio-recorder__button audio-recorder__button-record",
          onClick: E
        }, [...e[2] || (e[2] = [
          a("div", { class: "audio-recorder__stop" }, null, -1)
        ])]),
        a("span", Q, x(z.value), 1)
      ])) : m("", !0),
      !g.value && v.value != "uploading" && !w(n)().isRecording ? (c(), y("button", {
        key: 2,
        class: L(["audio-recorder__button", { "audio-recorder__button-disabled": R.state == "disabled" || w(n)().file }]),
        onClick: B
      }, [...e[4] || (e[4] = [
        a("span", { class: "pi pi-microphone" }, null, -1)
      ])], 2)) : m("", !0),
      w(n)().file ? (c(), M(P, {
        key: 3,
        to: "#chat-input-file-line-" + w(f)
      }, [
        p.value ? (c(), M(G, {
          key: 0,
          "file-info": p.value,
          onReset: V
        }, null, 8, ["file-info"])) : m("", !0)
      ], 8, ["to"])) : m("", !0)
    ]));
  }
});
export {
  re as default
};
