import { defineComponent as E, ref as n, inject as F, watch as h, createElementBlock as v, openBlock as u, Fragment as A, createBlock as D, createCommentVNode as S, normalizeClass as P, createElementVNode as r, toDisplayString as _, unref as U } from "vue";
import V from "../FilePreview/FilePreview.vue.js";
import { uploadFile as N } from "../../../hooks/uploadFile/uploadFile.js";
import "../../../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";
const O = ["accept"], B = { class: "attachment-section__info" }, G = {
  key: 2,
  class: "attachment-section__error-text"
}, Q = /* @__PURE__ */ E({
  __name: "WABAAttachmentSection",
  props: {
    type: {
      type: String,
      required: !0
    },
    templateId: {
      type: String,
      required: !0
    }
  },
  emits: ["fileSelected"],
  setup(m, { emit: w }) {
    const c = m, z = n(F("filebumpUrl")), p = n(null), o = n(), l = n(""), i = n(""), f = n(), d = w, k = () => {
      o.value && i.value != "success" && o.value.click();
    }, s = {
      DOCUMENT: {
        text: "Документ до 100 МБ",
        accept: ".pdf,.doc,.docx,.xls,.xlsx",
        maxSize: 104857600,
        error: "Некорректный тип файла, выберите PDF, DOC или XLS",
        icon: "pi pi-file"
      },
      IMAGE: {
        text: "Изображение до 5 МБ",
        accept: ".jpg,.jpeg,.png",
        maxSize: 5242880,
        error: "Некорректный тип файла, выберите JPG, JPEG или PNG",
        icon: "pi pi-image"
      },
      VIDEO: {
        text: "Видео до 16 МБ",
        accept: ".mp4,.mov",
        maxSize: 16777216,
        error: "Некорректный тип файла, выберите MP4 или MOV",
        icon: "pi pi-video"
      }
    }, I = (a) => {
      var x;
      const t = (x = a.target.files) == null ? void 0 : x[0];
      if (t) {
        l.value = "";
        const y = t.name.split(".");
        if (s[c.type].accept.indexOf(y[y.length - 1]) == -1)
          l.value = s[c.type].error;
        else if (t.size > s[c.type].maxSize)
          l.value = "Файл слишком большого размера";
        else if (!l.value) {
          p.value = t, i.value = "uploading";
          const g = U(z.value);
          N(typeof g == "string" ? g : null, t).then((e) => {
            i.value = e.status, e.status == "success" && (d("fileSelected", {
              url: e.url,
              filename: e.name,
              filesize: e.size,
              filetype: e.type
            }), 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
            }));
          });
        }
      }
    }, C = () => {
      p.value && (p.value = null, i.value = "");
    };
    return h(
      () => i.value,
      (a) => {
        a === "" && (o.value && (o.value.value = ""), p.value = null, d("fileSelected", null));
      }
    ), h(
      () => c.templateId,
      () => {
        i.value = "";
      }
    ), (a, t) => (u(), v(A, null, [
      i.value !== "success" ? (u(), v("div", {
        key: 0,
        class: P(["attachment-section", { "attachment-section__error": l.value }]),
        onClick: k
      }, [
        r("input", {
          id: "file-upload",
          ref_key: "fileInput",
          ref: o,
          type: "file",
          style: { display: "none" },
          accept: s[m.type].accept,
          onChange: I
        }, null, 40, O),
        t[1] || (t[1] = r("span", { class: "pi pi-paperclip attachment-section__icon" }, null, -1)),
        r("div", null, [
          t[0] || (t[0] = r("p", { class: "attachment-section__title" }, " Прикрепить файл ", -1)),
          r("p", B, _(s[m.type].text), 1)
        ])
      ], 2)) : i.value === "success" && f.value ? (u(), D(V, {
        key: 1,
        "file-info": f.value,
        onReset: C
      }, null, 8, ["file-info"])) : S("", !0),
      l.value ? (u(), v("div", G, _(l.value), 1)) : S("", !0)
    ], 64));
  }
});
export {
  Q as default
};
