import { defineComponent as O, ref as n, inject as P, computed as B, watchEffect as $, onMounted as H, onUnmounted as L, createElementBlock as _, openBlock as u, Fragment as R, createCommentVNode as b, createElementVNode as p, createBlock as V, normalizeClass as j, createVNode as T, unref as h, Teleport as q } from "vue";
import G from "../FilePreview/FilePreview.vue.js";
import { uploadFile as J } from "../../../hooks/uploadFile/uploadFile.js";
import { useMessageDraft as K } 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 Q from "./icons/FileUploaderIcon.vue.js";
const W = {
  key: 0,
  class: "file-uploader__status file-uploader__status--uploading"
}, X = {
  key: 1,
  class: "file-uploader__status file-uploader__status--error"
}, Y = ["disabled"], ce = /* @__PURE__ */ O({
  __name: "FileUploader",
  props: {
    filebumpUrl: {
      type: String,
      default: ""
    },
    state: {
      type: String,
      default: "active"
    }
  },
  emits: ["fileUploaded"],
  setup(m, { emit: Z }) {
    const v = m, s = n(""), r = n(), f = n(), C = n();
    C.value;
    const i = P("chatAppId"), { setMessageFile: N, resetMessageFile: z, getMessage: I, setRecordingMessage: E } = K(i), c = n("#5F5F5F"), d = n("#404040"), g = n(!1);
    let a = null;
    const y = () => {
      if (!i) {
        c.value = "#5F5F5F", d.value = "#404040";
        return;
      }
      const l = document.getElementById(i);
      if (!l) {
        c.value = "#5F5F5F", d.value = "#404040";
        return;
      }
      const t = window.getComputedStyle(l), e = t.getPropertyValue("--chotto-fileuploader-trigger-color").trim(), o = t.getPropertyValue("--chotto-fileuploader-trigger-hover-color").trim();
      c.value = e || "#5F5F5F", d.value = o || "#404040";
    }, A = B(() => g.value ? d.value : c.value);
    $(() => {
      y();
    });
    const F = B(() => !I().file), w = () => {
      r.value && (r.value.value = "");
    }, k = () => {
      const l = document.getElementById("chat-input-file-line-" + i);
      l && (l.style.display = "none"), z(), f.value = void 0, s.value = "", w();
    }, D = async () => {
      var t, e;
      const l = (e = (t = r.value) == null ? void 0 : t.files) == null ? void 0 : e[0];
      k(), l && await M(l), w();
    }, x = () => {
      r.value && F.value && v.state == "active" && (w(), r.value.click());
    }, U = async (l) => {
      var e;
      const t = (e = l.clipboardData) == null ? void 0 : e.items;
      if (t) {
        for (let o of t)
          if (o.type.indexOf("image") !== -1) {
            l.preventDefault();
            const S = o.getAsFile();
            S && M(S);
          }
      }
    }, M = async (l) => {
      s.value = "uploading", E(!0);
      const t = typeof v.filebumpUrl == "string" ? v.filebumpUrl : null;
      await J(t, l).then((e) => {
        if (E(!1), s.value = e.status, e.status == "success") {
          N({
            url: e.url,
            name: e.name,
            size: e.size,
            type: e.type
          });
          const o = document.getElementById("chat-input-file-line-" + i);
          o && (o.style.display = "inherit"), e.preview && (f.value = {
            previewUrl: e.preview.previewUrl,
            isImage: e.preview.isImage,
            isVideo: e.preview.isVideo,
            isAudio: e.preview.isAudio,
            fileName: e.name,
            fileSize: e.preview.fileSize
          });
        }
      });
    };
    return H(() => {
      if (window.addEventListener("paste", U), y(), i) {
        const l = document.getElementById(i);
        l && (a = new MutationObserver((t) => {
          t.forEach((e) => {
            e.type === "attributes" && e.attributeName === "data-theme" && y();
          });
        }), a.observe(l, {
          attributes: !0,
          attributeFilter: ["data-theme"]
        }));
      }
    }), L(() => {
      window.removeEventListener("paste", U), a && (a.disconnect(), a = null);
    }), (l, t) => (u(), _(R, null, [
      s.value === "uploading" ? (u(), _("div", W, [...t[2] || (t[2] = [
        p("span", null, "Загрузка", -1),
        p("span", null, "файла...", -1)
      ])])) : s.value === "error" ? (u(), _("div", X, [...t[3] || (t[3] = [
        p("span", null, "Ошибка при загрузке файла.", -1)
      ])])) : b("", !0),
      p("span", {
        ref_key: "triggerElement",
        ref: C,
        class: j(["file-uploader__trigger", { "file-uploader__disabled": !F.value || m.state == "disabled" }]),
        disabled: !F.value || m.state == "disabled",
        onClick: x,
        onMouseenter: t[0] || (t[0] = (e) => g.value = !0),
        onMouseleave: t[1] || (t[1] = (e) => g.value = !1)
      }, [
        T(h(Q), { fill: A.value }, null, 8, ["fill"])
      ], 42, Y),
      p("input", {
        ref_key: "fileInput",
        ref: r,
        style: { display: "none" },
        type: "file",
        onChange: D
      }, null, 544),
      h(I)().file ? (u(), V(q, {
        key: 2,
        to: "#chat-input-file-line-" + h(i)
      }, [
        f.value ? (u(), V(G, {
          key: 0,
          "file-info": f.value,
          onReset: k
        }, null, 8, ["file-info"])) : b("", !0)
      ], 8, ["to"])) : b("", !0)
    ], 64));
  }
});
export {
  ce as default
};
