"use client";
import { jsxs as c, jsx as e } from "react/jsx-runtime";
import "react-pdf/dist/Page/AnnotationLayer.css";
import "react-pdf/dist/Page/TextLayer.css";
import { useState as l, useRef as W, useMemo as v, useEffect as d } from "react";
import { Button as N } from "./button.js";
import { Icon as s } from "./icon.js";
function R({
  url: m,
  initialPage: g = 1,
  className: u = ""
}) {
  const [o, j] = l(null), [a, P] = l(), [t, f] = l(g), [p, h] = l(null), n = W(null), [b, x] = l(950), E = v(() => ({ url: m }), [m]), w = v(() => o ? {
    cMapUrl: `https://unpkg.com/pdfjs-dist@${o.pdfjs.version}/cmaps/`,
    cMapPacked: !0
  } : null, [o]);
  d(() => {
    if (typeof window > "u") return;
    let r = !0;
    return import("react-pdf").then((i) => {
      r && (i.pdfjs.GlobalWorkerOptions.workerSrc = `https://unpkg.com/pdfjs-dist@${i.pdfjs.version}/build/pdf.worker.min.mjs`, j(i));
    }), () => {
      r = !1;
    };
  }, []);
  const L = ({ numPages: r }) => {
    P(r), h(null), setTimeout(() => {
      if (n.current) {
        const i = n.current.clientWidth;
        x(i - 32);
      }
    }, 50);
  }, y = (r) => {
    console.error("PDF Load Error:", r), h(r.message || "Errore nel caricamento del PDF");
  };
  if (d(() => {
    n.current && (n.current.scrollTop = 0);
  }, [t]), d(() => {
    const r = () => {
      if (n.current) {
        const i = n.current.clientWidth;
        x(i - 32);
      }
    };
    return r(), window.addEventListener("resize", r), () => window.removeEventListener("resize", r);
  }, []), p)
    return /* @__PURE__ */ c("div", { className: "text-d-destructive flex min-h-[600px] flex-col items-center justify-center gap-3", children: [
      /* @__PURE__ */ e(s, { name: "AlertCircle", className: "h-12 w-12" }),
      /* @__PURE__ */ e("p", { className: "font-semibold", children: "Errore nel caricamento del PDF" }),
      /* @__PURE__ */ e("p", { className: "text-d-muted-foreground text-sm", children: p })
    ] });
  if (!o || !w)
    return /* @__PURE__ */ e(
      "div",
      {
        className: `flex min-h-[600px] w-full flex-col items-center ${u}`,
        children: /* @__PURE__ */ e("div", { className: "flex min-h-[600px] w-full items-center justify-center", children: /* @__PURE__ */ e(
          s,
          {
            name: "Loader2",
            className: "text-d-primary h-8 w-8 animate-spin"
          }
        ) })
      }
    );
  const { Document: C, Page: k } = o;
  return /* @__PURE__ */ c(
    "div",
    {
      className: `flex min-h-[600px] w-full flex-col items-center ${u}`,
      children: [
        /* @__PURE__ */ e(
          "div",
          {
            ref: n,
            className: "border-d-border max-h-[500px] w-full overflow-auto rounded border",
            children: /* @__PURE__ */ e(
              C,
              {
                file: E,
                onLoadSuccess: L,
                onLoadError: y,
                options: w,
                loading: /* @__PURE__ */ e("div", { className: "flex min-h-[600px] items-center justify-center", children: /* @__PURE__ */ e(
                  s,
                  {
                    name: "Loader2",
                    className: "text-d-primary h-8 w-8 animate-spin"
                  }
                ) }),
                error: /* @__PURE__ */ c("div", { className: "text-d-destructive flex min-h-[600px] flex-col items-center justify-center gap-2", children: [
                  /* @__PURE__ */ e(s, { name: "AlertCircle", className: "h-12 w-12" }),
                  /* @__PURE__ */ e("p", { children: "Errore nel caricamento del PDF" })
                ] }),
                className: "h-full min-h-[600px] w-full border-0",
                children: /* @__PURE__ */ e(k, { pageNumber: t, width: b })
              }
            )
          }
        ),
        /* @__PURE__ */ c("div", { className: "my-8 flex h-full w-full items-center justify-center gap-4", children: [
          /* @__PURE__ */ e(
            N,
            {
              onClick: () => f(Math.max(1, t - 1)),
              disabled: t <= 1,
              variant: "outline",
              size: "sm",
              children: /* @__PURE__ */ e(s, { name: "ChevronLeft", className: "h-4 w-4" })
            }
          ),
          /* @__PURE__ */ c("p", { className: "text-d-foreground text-sm", children: [
            t,
            " / ",
            a
          ] }),
          /* @__PURE__ */ e(
            N,
            {
              onClick: () => f(Math.min(a || 1, t + 1)),
              disabled: t >= (a || 1),
              variant: "outline",
              size: "sm",
              children: /* @__PURE__ */ e(s, { name: "ChevronRight", className: "h-4 w-4" })
            }
          )
        ] })
      ]
    }
  );
}
export {
  R as SecurePdfViewer
};
