"use client";
import { jsxs as h, jsx as r } from "react/jsx-runtime";
import { useMemo as c } from "react";
import { createAsk as v } from "../../node_modules/use-ask/dist/index.js";
import { AppDialog as x } from "./app-dialog.js";
import { Icon as g } from "./icon.js";
import { SecurePdfViewer as N } from "./secure-pdf-viewer.js";
import { guessKind as y, isHttpUrl as P, getOfficeEmbedUrl as b } from "../../lib/file-preview.js";
const [d, A] = v({});
function p(e) {
  return typeof e == "string" ? { url: e } : e || {};
}
async function S(e) {
  return d.ask(p(e));
}
function U(e) {
  return d.safeAsk(p(e));
}
const _ = () => {
  const [{ payload: e }, { asking: l, ok: i }] = A(), n = e?.url, s = e?.readOnly, t = e?.page, o = c(() => e?.filename ? e?.filename : (e?.url || "").split(/[?#]/)[0].split("/").pop() || "" || "document", [e?.filename, e?.url]), u = c(() => {
    if (!e?.url) return null;
    const a = new URL(e?.url), m = a.searchParams.get("response-content-type") || a.searchParams.get("content-type"), f = e?.mimeType || m || "";
    return y({
      mimeType: f,
      filename: o,
      url: e?.url
    });
  }, [e?.mimeType, o, e?.url]), w = c(
    () => /* @__PURE__ */ h("span", { className: "flex items-center gap-2", children: [
      /* @__PURE__ */ r(g, { name: "File", className: "h-4 w-4" }),
      e?.title || o
    ] }),
    [e?.title, o]
  );
  return /* @__PURE__ */ r(
    x,
    {
      open: l,
      onOpenChange: (a) => !a && i(),
      title: w,
      size: e?.size ?? "xl",
      children: u && n ? /* @__PURE__ */ r(
        O,
        {
          kind: u,
          src: n,
          officeUrl: n,
          readOnly: s,
          page: t
        }
      ) : null
    }
  );
}, O = ({
  kind: e,
  src: l,
  officeUrl: i,
  readOnly: n = !1,
  page: s = 0
}) => {
  if (!l && e !== "text" && e !== "office")
    return /* @__PURE__ */ r("div", { className: "text-d-secondary-foreground", children: "Nessuna sorgente disponibile" });
  switch (e) {
    case "image":
      return /* @__PURE__ */ r("div", { className: "flex h-full items-center justify-center", children: /* @__PURE__ */ r(
        "img",
        {
          src: l,
          alt: "preview",
          className: "max-h-full w-auto max-w-full object-contain"
        }
      ) });
    case "pdf": {
      if (n && l)
        return /* @__PURE__ */ r(
          N,
          {
            url: l,
            initialPage: s || 1,
            className: "h-full w-full"
          }
        );
      let t = l;
      return s > 0 && (t += "#page=" + s), /* @__PURE__ */ r(
        "iframe",
        {
          src: t,
          title: "PDF Preview",
          className: "h-full w-full rounded"
        }
      );
    }
    case "audio":
      return /* @__PURE__ */ r("div", { className: "grid h-full place-items-center", children: /* @__PURE__ */ r("audio", { controls: !0, className: "w-full max-w-2xl", children: /* @__PURE__ */ r("source", { src: l }) }) });
    case "video":
      return /* @__PURE__ */ r("div", { className: "grid h-full place-items-center", children: /* @__PURE__ */ r("video", { controls: !0, className: "max-h-full w-full max-w-3xl rounded", children: /* @__PURE__ */ r("source", { src: l }) }) });
    case "text":
      return /* @__PURE__ */ r(
        "iframe",
        {
          src: l,
          title: "Text Preview",
          className: "h-full w-full rounded"
        }
      );
    case "office": {
      if (i && P(i)) {
        const t = b(i);
        return /* @__PURE__ */ r(
          "iframe",
          {
            src: t,
            title: "Office Preview",
            className: "h-full w-full rounded"
          }
        );
      }
      return /* @__PURE__ */ r("div", { className: "rounded border p-3 text-sm", children: 'Anteprima Office non disponibile per file locali. Usa "Apri in nuova scheda" o "Download".' });
    }
    default:
      return /* @__PURE__ */ r("div", { className: "rounded border p-3 text-sm", children: "Anteprima non disponibile per questo tipo di file." });
  }
};
export {
  _ as FilePreviewer,
  S as previewFileModal,
  U as safePreviewFileModal
};
