import { useState as E, useCallback as h, useEffect as A } from "react";
import { appConsole as l } from "../appConsole.js";
const b = /* @__PURE__ */ new Date("2025-10-03T12:47:51.103Z"), d = "Please visit https://www.react-pdf.dev/manage-license/ to generate a new license key.", s = {
  invalidLicense: `You are currently using without a valid license. ${d}`,
  mismatchedDomain: `Your license key is not valid for the current domain / IP. ${d}`,
  expired: `Your license key has expired. ${d}`,
  exceededVersion: "Your license does not support the current version of React PDF. The library version you are using exceeds the supported range of your license.",
  invalidSignature: "Invalid license key: Signature mismatch"
};
function L(r) {
  const t = r.replace(/-----BEGIN PUBLIC KEY-----/, "").replace(/-----END PUBLIC KEY-----/, "").replace(/\s+/g, ""), e = atob(t), n = new Uint8Array(e.length);
  for (let i = 0; i < e.length; i++)
    n[i] = e.charCodeAt(i);
  return n.buffer;
}
function S(r) {
  const t = atob(r), e = new Uint8Array(t.length);
  for (let n = 0; n < t.length; n++)
    e[n] = t.charCodeAt(n);
  return e.buffer;
}
async function I(r, t = "SHA-256") {
  try {
    const { data: e, signature: n } = JSON.parse(atob(r)), i = L(`-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvIokvErJ0Fctu0jduSAx
gr+5Har/VSuZLlOunS28hnlqqA+OF1apHL7RYkjBosS15yvviYdLrVWYHpHnoY4b
HLQ9I6YX6FMB9T/VbB4xLdVdKvfi8r49aTScl5EKuSpgpPgz2VSJTfvVGcRuth/Y
Bm7PPGYdL2l2gvKDxchmmsHFPukGki9L4JOUeIVX0GXAxabENckaEH/iC1NZhX+W
vXnpHeHuIIKlZ16LSivXNgo6BH2Z3GFdg4G3cAaXr6qNP8fsacYNqv2SpTW1viFD
vzcGRabN25gpJAp9/syeScefWH3OiJY1lmw88oTiJbjIoiq6AkPhfn3G9mF54nvR
kwIDAQAB
-----END PUBLIC KEY-----`), a = await window.crypto.subtle.importKey(
      "spki",
      // str2ab(publicKeyContent), // Convert PEM string to ArrayBuffer
      i,
      { name: "RSA-PSS", hash: t },
      !0,
      ["verify"]
    );
    if (!await window.crypto.subtle.verify(
      { name: "RSA-PSS", saltLength: 32 },
      a,
      S(n),
      // Convert signature to ArrayBuffer
      new TextEncoder().encode(atob(e))
    ))
      throw new Error(s.invalidSignature);
    return JSON.parse(atob(e));
  } catch {
    throw new Error(s.invalidLicense);
  }
}
const y = (r, t) => r === "specific" ? t === window.location.host : window.location.host.includes(t), u = {
  isValid: !0,
  invalidatedMessage: s.invalidLicense,
  type: void 0
}, x = (r) => {
  const [t, e] = E(u), n = h(async () => {
    if (e((c) => ({ ...c, validating: !0 })), !r)
      throw e({ ...u, validating: !1 }), new Error(s.invalidLicense);
    let i;
    try {
      i = await I(r);
    } catch (c) {
      throw l.warn(c.message), new Error(c.message);
    }
    l.debug(">>> validatedLicense", i);
    const { avu: a, exp: o, dmt: m, dm: w, t: f } = i;
    if (!o)
      throw new Error("License is missing expiration timestamp");
    if (o > Number.MAX_SAFE_INTEGER / 1e3)
      throw new Error("Invalid expiration timestamp: value too large");
    const p = new Date(o * 1e3), g = (/* @__PURE__ */ new Date()).getTime();
    if (p.getTime() < g)
      throw new Error(s.expired);
    if (!a)
      throw new Error("License is missing available until version timestamp");
    if (a > Number.MAX_SAFE_INTEGER / 1e3)
      throw new Error("Invalid available until version timestamp: value too large");
    const v = new Date(a * 1e3);
    if (l.debug("availableUntilTimestamp", v), v.getTime() < b.getTime())
      throw new Error(s.exceededVersion);
    if (!y(m, w))
      throw new Error(s.mismatchedDomain);
    return {
      isValid: !0,
      type: f,
      invalidatedMessage: void 0
    };
  }, [r]);
  return A(() => {
    n().then(({ isValid: i, type: a, invalidatedMessage: o }) => {
      e({
        isValid: i,
        type: a,
        invalidatedMessage: o,
        validating: !1
      });
    }).catch((i) => {
      e({
        ...u,
        isValid: !1,
        validating: !1
      }), l.warn(i.message);
    });
  }, [n]), t;
};
export {
  x as useLicense
};
