const d = z(), I = K(), F = Q(), N = Y(), k = {
  // Default is to fail on error, no placeholder
  imagePlaceholder: void 0,
  // Default cache bust is false, it will use the cache
  cacheBust: !1
}, b = {
  toSvg: j,
  toPng: _,
  toJpeg: X,
  toBlob: G,
  toPixelData: V,
  cloneNode: O,
  impl: {
    fontFaces: F,
    images: N,
    util: d,
    inliner: I,
    options: {}
  }
};
typeof module < "u" ? module.exports = b : globalThis.domtoimage = b;
async function j(o, n) {
  n = n || {}, $(n);
  const t = await Promise.resolve(o), s = await O(t, n.filter, !0), l = await W(s), r = await q(l), c = await h(r);
  return await J(
    c,
    n.width || d.width(o),
    n.height || d.height(o)
  );
  function h(m) {
    return n.bgcolor && (m.style.backgroundColor = n.bgcolor), n.width && (m.style.width = n.width + "px"), n.height && (m.style.height = n.height + "px"), n.style && Object.keys(n.style).forEach(function(g) {
      m.style[g] = n.style[g];
    }), m;
  }
}
async function V(o, n) {
  const t = await R(o, n || {});
  return t ? t.getContext("2d").getImageData(
    0,
    0,
    d.width(o),
    d.height(o)
  ).data : null;
}
async function _(o, n) {
  const t = await R(o, n || {});
  return t ? t.toDataURL() : null;
}
async function X(o, n) {
  n = n || {};
  const t = await R(o, n);
  return t ? t.toDataURL("image/jpeg", n.quality || 1) : null;
}
function G(o, n) {
  return R(o, n || {}).then(d.canvasToBlob);
}
function $(o) {
  typeof o.imagePlaceholder > "u" ? b.impl.options.imagePlaceholder = k.imagePlaceholder : b.impl.options.imagePlaceholder = o.imagePlaceholder, typeof o.cacheBust > "u" ? b.impl.options.cacheBust = k.cacheBust : b.impl.options.cacheBust = o.cacheBust;
}
function R(o, n) {
  return j(o, n).then(d.makeImage).then(d.delay(100)).then(function(s) {
    const l = t(o, n), r = l.getContext("2d");
    return r ? (r.drawImage(s, 0, 0), l) : new HTMLCanvasElement();
  }).catch(function(s) {
    console.error("Failed to draw canvas:", s);
  });
  function t(s, l) {
    const r = document.createElement("canvas"), c = s.getBoundingClientRect(), h = window.devicePixelRatio || 1;
    r.width = (l.width || c.width) * h, r.height = (l.height || c.height) * h, r.style.width = (l.width || c.width) + "px", r.style.height = (l.height || c.height) + "px";
    const m = r.getContext("2d");
    return m ? (m.scale(h, h), l.bgcolor && (m.fillStyle = l.bgcolor, m.fillRect(0, 0, r.width, r.height)), r) : new HTMLCanvasElement();
  }
}
async function O(o, n, t) {
  if (!t && n && !n(o)) return Promise.resolve();
  const s = await Promise.resolve(o), l = await c(s), r = await h(o, l, n);
  return m(o, r);
  function c(g) {
    return g instanceof HTMLCanvasElement ? d.makeImage(g.toDataURL()) : g.cloneNode(!1);
  }
  async function h(g, a, T) {
    const x = g.childNodes;
    if (x.length === 0) return Promise.resolve(a);
    return await C(a, d.asArray(x), T), a;
    function C(U, v, p) {
      let w = Promise.resolve();
      return v.forEach(function(e) {
        w = w.then(function() {
          return O(e, p);
        }).then(function(i) {
          i && U.appendChild(i);
        });
      }), w;
    }
  }
  function m(g, a) {
    if (!(a instanceof Element)) return a;
    return Promise.resolve().then(T).then(x).then(C).then(U).then(function() {
      return a;
    });
    function T() {
      v(window.getComputedStyle(g), a.style);
      function v(p, w) {
        p.cssText ? w.cssText = p.cssText : e(p, w);
        function e(i, f) {
          d.asArray(i).forEach(function(u) {
            f.setProperty(
              u,
              i.getPropertyValue(u),
              i.getPropertyPriority(u)
            );
          });
        }
      }
    }
    function x() {
      [":before", ":after"].forEach(function(p) {
        v(p);
      });
      function v(p) {
        const w = window.getComputedStyle(g, p), e = w.getPropertyValue("content");
        if (e === "" || e === "none") return;
        const i = d.uid();
        a.className = a.className + " " + i;
        const f = document.createElement("style");
        f.appendChild(u(i, p, w)), a.appendChild(f);
        function u(y, E, S) {
          const B = "." + y + ":" + E, P = S.cssText ? L(S) : H(S);
          return document.createTextNode(B + "{" + P + "}");
          function L(A) {
            const D = A.getPropertyValue("content");
            return A.cssText + " content: " + D + ";";
          }
          function H(A) {
            return d.asArray(A).map(D).join("; ") + ";";
            function D(M) {
              return M + ": " + A.getPropertyValue(M) + (A.getPropertyPriority(M) ? " !important" : "");
            }
          }
        }
      }
    }
    function C() {
      g instanceof HTMLTextAreaElement && (a.innerHTML = g.value), g instanceof HTMLInputElement && a.setAttribute("value", g.value);
    }
    function U() {
      a instanceof SVGElement && (a.setAttribute("xmlns", "http://www.w3.org/2000/svg"), a instanceof SVGRectElement && ["width", "height"].forEach(function(v) {
        const p = a.getAttribute(v);
        p && a.style.setProperty(v, p);
      }));
    }
  }
}
async function W(o) {
  const n = await F.resolveAll(), t = document.createElement("style");
  return o.appendChild(t), t.appendChild(document.createTextNode(n)), o;
}
function q(o) {
  return N.inlineAll(o).then(function() {
    return o;
  });
}
function J(o, n, t) {
  return Promise.resolve(o).then(function(s) {
    return s.setAttribute("xmlns", "http://www.w3.org/1999/xhtml"), new XMLSerializer().serializeToString(s);
  }).then(d.escapeXhtml).then(function(s) {
    return '<foreignObject x="0" y="0" width="100%" height="100%">' + s + "</foreignObject>";
  }).then(function(s) {
    return '<svg xmlns="http://www.w3.org/2000/svg" width="' + n + '" height="' + t + '">' + s + "</svg>";
  }).then(function(s) {
    return "data:image/svg+xml;charset=utf-8," + s;
  });
}
function z() {
  return {
    escape: T,
    parseExtension: n,
    mimeType: t,
    dataAsUrl: a,
    isDataUrl: s,
    canvasToBlob: r,
    resolveUrl: c,
    getAndEncode: g,
    uid: h(),
    delay: x,
    asArray: C,
    escapeXhtml: U,
    makeImage: m,
    width: v,
    height: p
  };
  function o() {
    const e = "application/font-woff", i = "image/jpeg";
    return {
      woff: e,
      woff2: e,
      ttf: "application/font-truetype",
      eot: "application/vnd.ms-fontobject",
      png: "image/png",
      jpg: i,
      jpeg: i,
      gif: "image/gif",
      tiff: "image/tiff",
      svg: "image/svg+xml",
      webp: "image/webp",
      avif: "image/avif"
    };
  }
  function n(e) {
    const i = /\.([^\.\/]*?)$/g.exec(e);
    return i ? i[1] : "";
  }
  function t(e) {
    const i = n(e).toLowerCase();
    return o()[i] || "";
  }
  function s(e) {
    return e.search(/^(data:)/) !== -1;
  }
  function l(e) {
    return new Promise(function(i) {
      const f = window.atob(e.toDataURL().split(",")[1]), u = f.length, y = new Uint8Array(u);
      for (let E = 0; E < u; E++)
        y[E] = f.charCodeAt(E);
      i(new Blob([y], {
        type: "image/png"
      }));
    });
  }
  function r(e) {
    return e.toBlob ? new Promise(function(i) {
      e.toBlob(i);
    }) : l(e);
  }
  function c(e, i) {
    const f = document.implementation.createHTMLDocument(), u = f.createElement("base");
    f.head.appendChild(u);
    const y = f.createElement("a");
    return f.body.appendChild(y), u.href = i, y.href = e, y.href;
  }
  function h() {
    let e = 0;
    return function() {
      return "u" + i() + e++;
      function i() {
        return ("0000" + (Math.random() * Math.pow(36, 4) << 0).toString(36)).slice(-4);
      }
    };
  }
  function m(e) {
    return new Promise(function(i, f) {
      const u = new Image();
      u.onload = function() {
        i(u);
      }, u.onerror = f, u.src = e;
    });
  }
  function g(e) {
    return b.impl.options.cacheBust && (e += (/\?/.test(e) ? "&" : "?") + (/* @__PURE__ */ new Date()).getTime()), new Promise(function(f) {
      const u = new XMLHttpRequest();
      u.onreadystatechange = E, u.ontimeout = S, u.responseType = "blob", u.timeout = 3e4, u.open("GET", e, !0), u.send();
      let y;
      if (b.impl.options.imagePlaceholder) {
        const P = b.impl.options.imagePlaceholder.split(/,/);
        P && P[1] && (y = P[1]);
      }
      function E() {
        if (u.readyState !== 4) return;
        if (u.status !== 200) {
          y ? f(y) : B("cannot fetch resource: " + e + ", status: " + u.status);
          return;
        }
        const P = new FileReader();
        P.onloadend = function() {
          const L = P.result.split(/,/)[1];
          f(L);
        }, P.readAsDataURL(u.response);
      }
      function S() {
        y ? f(y) : B("timeout of 30000ms occured while fetching resource: " + e);
      }
      function B(P) {
        console.error(P), f("");
      }
    });
  }
  function a(e, i) {
    return "data:" + i + ";base64," + e;
  }
  function T(e) {
    return e.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
  }
  function x(e) {
    return function(i) {
      return new Promise(function(f) {
        setTimeout(function() {
          f(i);
        }, e);
      });
    };
  }
  function C(e) {
    const i = [], f = e.length;
    for (let u = 0; u < f; u++) i.push(e[u]);
    return i;
  }
  function U(e) {
    return e.replace(/#/g, "%23").replace(/\n/g, "%0A");
  }
  function v(e) {
    const i = w(e, "border-left-width"), f = w(e, "border-right-width");
    return e.scrollWidth + i + f;
  }
  function p(e) {
    const i = w(e, "border-top-width"), f = w(e, "border-bottom-width");
    return e.scrollHeight + i + f;
  }
  function w(e, i) {
    const f = window.getComputedStyle(e).getPropertyValue(i);
    return parseFloat(f.replace("px", ""));
  }
}
function K() {
  const o = /url\(['"]?([^'"]+?)['"]?\)/g;
  return {
    inlineAll: l,
    shouldProcess: n,
    impl: {
      readUrls: t,
      inline: s
    }
  };
  function n(r) {
    return r.search(o) !== -1;
  }
  function t(r) {
    const c = [];
    let h;
    for (; (h = o.exec(r)) !== null; )
      c.push(h[1]);
    return c.filter(function(m) {
      return !d.isDataUrl(m);
    });
  }
  function s(r, c, h, m) {
    return Promise.resolve(c).then(function(a) {
      return h ? d.resolveUrl(a, h) : a;
    }).then(m || d.getAndEncode).then(function(a) {
      return d.dataAsUrl(a, d.mimeType(c));
    }).then(function(a) {
      return r.replace(g(c), "$1" + a + "$3");
    });
    function g(a) {
      return new RegExp(`(url\\(['"]?)(` + d.escape(a) + `)(['"]?\\))`, "g");
    }
  }
  function l(r, c, h) {
    if (m()) return Promise.resolve(r);
    return Promise.resolve(r).then(t).then(function(g) {
      let a = Promise.resolve(r);
      return g.forEach(function(T) {
        a = a.then(function(x) {
          return s(x, T, c, h);
        });
      }), a;
    });
    function m() {
      return !n(r);
    }
  }
}
function Q() {
  return {
    resolveAll: o,
    impl: {
      readAll: n
    }
  };
  function o() {
    return n().then(function(t) {
      return Promise.all(
        t.map(function(s) {
          return s.resolve();
        })
      );
    }).then(function(t) {
      return t.join(`
`);
    });
  }
  function n() {
    return Promise.resolve(d.asArray(document.styleSheets)).then(s).then(t).then(function(r) {
      return r.map(l);
    });
    function t(r) {
      return r.filter(function(c) {
        return c.type === CSSRule.FONT_FACE_RULE;
      }).filter(function(c) {
        return I.shouldProcess(c.style.getPropertyValue("src"));
      });
    }
    function s(r) {
      const c = [];
      return r.forEach(function(h) {
        if (Object.prototype.hasOwnProperty.call(h, "cssRules"))
          try {
            d.asArray(h.cssRules || []).forEach(c.push.bind(c));
          } catch (m) {
            console.log("Error while reading CSS rules from " + h.href, m.toString());
          }
      }), c;
    }
    function l(r) {
      return {
        resolve: function() {
          const h = (r.parentStyleSheet || {}).href;
          return I.inlineAll(r.cssText, h);
        },
        src: function() {
          return r.style.getPropertyValue("src");
        }
      };
    }
  }
}
function Y() {
  return {
    inlineAll: n,
    impl: {
      newImage: o
    }
  };
  function o(t) {
    return {
      inline: s
    };
    function s(l) {
      return d.isDataUrl(t.src) ? Promise.resolve() : Promise.resolve(t.src).then(l || d.getAndEncode).then(function(r) {
        return d.dataAsUrl(r, d.mimeType(t.src));
      }).then(function(r) {
        return new Promise(function(c) {
          t.onload = function() {
            c();
          }, t.onerror = function() {
            const h = b.impl.options.imagePlaceholder;
            if (h)
              try {
                t.onload = null, t.onerror = null, t.src = h, t.srcset = "";
              } catch (m) {
                console.error("Image placeholder assignment failed:", m);
              }
            else
              console.warn("Image inline failed, continuing without placeholder:", t.src);
            c();
          }, t.src = r, t.srcset = "";
        });
      });
    }
  }
  function n(t) {
    if (!(t instanceof Element)) return Promise.resolve(t);
    return s(t).then(function() {
      return t instanceof HTMLImageElement ? o(t).inline() : Promise.all(
        d.asArray(t.childNodes).map(function(l) {
          return n(l);
        })
      );
    });
    function s(l) {
      const r = l.style.getPropertyValue("background");
      return r ? I.inlineAll(r).then(function(c) {
        l.style.setProperty(
          "background",
          c,
          l.style.getPropertyPriority("background")
        );
      }).then(function() {
        return l;
      }) : Promise.resolve(l);
    }
  }
}
export {
  b as domtoimage
};
