import { defineComponent as R, ref as f, computed as V, createElementBlock as p, openBlock as a, createVNode as $, withModifiers as h, normalizeClass as E, createElementVNode as l, renderSlot as q, toDisplayString as g, unref as r, withCtx as H, createCommentVNode as k, Transition as z, createBlock as U, Fragment as G, renderList as M, watch as O } from "vue";
import { useClipboard as j, useShare as A, prettyBytes as I, uploadTorrent as J, useGun as K, useUser as P } from "./composables-BAZQlC3e.js";
import { UiLayer as W, QrShow as X, FileCard as Y } from "./components.js";
const Z = { class: "flex" }, _ = { class: "text-md max-w-55ch" }, ee = { class: "p-0" }, le = { class: "flex flex-wrap gap-2 p-4 items-start" }, te = {
  key: 0,
  class: "flex flex-col gap-2"
}, se = { class: "font-mono text-sm" }, ne = ["href"], oe = { class: "p-0" }, ae = { class: "p-0" }, re = { class: "p-0" }, ve = /* @__PURE__ */ R({
  __name: "TorrentUpload",
  emits: ["uploaded", "url"],
  setup(ie, { emit: L }) {
    const C = L, s = f();
    f();
    const m = f(!1), c = f(!1), x = f(!1), y = f(!1);
    function D(t) {
      var n, b;
      const e = ((n = t.target) == null ? void 0 : n.files) || ((b = t == null ? void 0 : t.dataTransfer) == null ? void 0 : b.files);
      if (!e || e.length === 0) return;
      const { torrent: i } = J(e);
      K();
      const { user: u } = P();
      O(i, async (d) => {
        s.value = d, m.value = !0;
        const o = {
          infoHash: d.infoHash,
          length: d.length,
          name: d.name,
          author: u.pub
        };
        C("uploaded", o), C("url", v.value);
      });
    }
    function B(t) {
      t.preventDefault(), y.value = !0;
    }
    function F(t) {
      t.preventDefault(), y.value = !1;
    }
    function T(t) {
      t.preventDefault(), y.value = !1, D(t);
    }
    const v = V(() => {
      var e, i, u;
      if (!((e = s.value) != null && e.infoHash)) return "";
      let t = new URL((i = window == null ? void 0 : window.location) == null ? void 0 : i.href);
      return t.hash = "", `${t.href}#/files/${(u = s.value) == null ? void 0 : u.infoHash}`;
    }), w = j({ source: v }), { share: N, isSupported: Q } = A();
    return (t, e) => {
      var i, u;
      return a(), p("div", Z, [
        s.value ? (a(), p("div", {
          key: 1,
          class: "animate-pulse p-2 flex flex-wrap gap-2 bg-light-100 dark-bg-dark-400 rounded cursor-pointer items-center break-all border-2 rounded-lg shadow-lg",
          onClick: e[2] || (e[2] = (n) => m.value = !m.value)
        }, [
          e[10] || (e[10] = l("div", { class: "font-bold" }, "Sharing a file:", -1)),
          l("div", _, g((i = s.value) == null ? void 0 : i.name), 1),
          e[11] || (e[11] = l("div", { class: "flex-1" }, null, -1)),
          l("div", ee, g(r(I)((u = s.value) == null ? void 0 : u.length)), 1),
          l("div", {
            class: "i-la-times",
            onClick: e[1] || (e[1] = h((n) => s.value = null, ["prevent", "stop"]))
          })
        ])) : (a(), p("label", {
          key: 0,
          class: E(["border-2 shadow-lg p-4 flex flex-wrap gap-2 bg-light-100 dark-bg-dark-400 rounded-lg cursor-pointer items-center", { "bg-light-800": y.value }]),
          for: "file",
          onDragover: h(B, ["prevent"]),
          onDragleave: h(F, ["prevent"]),
          onDrop: h(T, ["prevent"])
        }, [
          e[9] || (e[9] = l("div", { class: "i-la-paperclip text-xl" }, null, -1)),
          q(t.$slots, "default", {}, () => [
            e[8] || (e[8] = l("div", { class: "font-bold" }, "Drop your files here", -1))
          ]),
          l("input", {
            id: "file",
            type: "file",
            accept: "*",
            onChange: e[0] || (e[0] = (n) => D(n)),
            style: { display: "none" }
          }, null, 32)
        ], 34)),
        $(r(W), {
          open: m.value,
          onClose: e[7] || (e[7] = (n) => m.value = !1)
        }, {
          default: H(() => {
            var n, b, d;
            return [
              l("div", le, [
                s.value ? (a(), p("div", te, [
                  e[17] || (e[17] = l("div", { class: "text-xl" }, "Sharing the torrent", -1)),
                  l("div", se, g((n = s.value) == null ? void 0 : n.infoHash), 1),
                  l("a", {
                    class: "gap-2 button underline font-bold -all",
                    href: v.value,
                    target: "_blank"
                  }, e[12] || (e[12] = [
                    l("div", { class: "i-la-link" }, null, -1),
                    l("div", { class: "p-0" }, "Download URL", -1)
                  ]), 8, ne),
                  r(w).isSupported.value ? (a(), p("button", {
                    key: 0,
                    class: "p-2 button gap-2",
                    onClick: e[3] || (e[3] = (o) => r(w).copy())
                  }, [
                    e[13] || (e[13] = l("div", { class: "i-la-copy" }, null, -1)),
                    l("div", oe, g((b = r(w)) != null && b.copied.value ? "Copied!" : "Copy"), 1)
                  ])) : k("", !0),
                  r(Q) ? (a(), p("button", {
                    key: 1,
                    class: "p-2 button gap-2",
                    onClick: e[4] || (e[4] = (o) => {
                      var S;
                      return r(N)({ title: (S = s.value) == null ? void 0 : S.name, url: v.value });
                    })
                  }, e[14] || (e[14] = [
                    l("div", { class: "i-la-share" }, null, -1),
                    l("div", { class: "p-0" }, " Share", -1)
                  ]))) : k("", !0),
                  l("button", {
                    class: "button p-2 gap-2",
                    onClick: e[5] || (e[5] = (o) => c.value = !c.value)
                  }, [
                    e[15] || (e[15] = l("div", { class: "i-la-qrcode" }, null, -1)),
                    l("div", ae, g(c.value ? "Hide" : "Show") + " QR ", 1)
                  ]),
                  l("button", {
                    class: "button p-2 gap-2",
                    onClick: e[6] || (e[6] = (o) => x.value = !x.value)
                  }, [
                    e[16] || (e[16] = l("div", { class: "i-la-file" }, null, -1)),
                    l("div", re, g(x.value ? "Hide" : "Show") + " file ", 1)
                  ])
                ])) : k("", !0),
                $(z, { name: "fade" }, {
                  default: H(() => [
                    v.value && c.value ? (a(), U(r(X), {
                      key: 0,
                      data: v.value
                    }, null, 8, ["data"])) : k("", !0)
                  ]),
                  _: 1
                }),
                x.value ? (a(!0), p(G, { key: 1 }, M((d = s.value) == null ? void 0 : d.files, (o) => (a(), U(r(Y), {
                  class: "flex-auto",
                  key: o == null ? void 0 : o.name,
                  file: o
                }, null, 8, ["file"]))), 128)) : k("", !0)
              ])
            ];
          }),
          _: 1
        }, 8, ["open"])
      ]);
    };
  }
});
export {
  ve as default
};
//# sourceMappingURL=TorrentUpload-Dz9l6Vkp.js.map
