const h = z(), R = 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: h,
    inliner: R,
    options: {}
  }
};
typeof module < "u" ? module.exports = b : globalThis.domtoimage = b;
async function j(o, t) {
  t = t || {}, $(t);
  const n = await Promise.resolve(o), a = await O(n, t.filter, !0), l = await W(a), r = await q(l), s = await d(r);
  return await J(
    s,
    t.width || h.width(o),
    t.height || h.height(o)
  );
  function d(m) {
    return t.bgcolor && (m.style.backgroundColor = t.bgcolor), t.width && (m.style.width = t.width + "px"), t.height && (m.style.height = t.height + "px"), t.style && Object.keys(t.style).forEach(function(g) {
      m.style[g] = t.style[g];
    }), m;
  }
}
async function V(o, t) {
  const n = await I(o, t || {});
  return n ? n.getContext("2d").getImageData(
    0,
    0,
    h.width(o),
    h.height(o)
  ).data : null;
}
async function _(o, t) {
  const n = await I(o, t || {});
  return n ? n.toDataURL() : null;
}
async function X(o, t) {
  t = t || {};
  const n = await I(o, t);
  return n ? n.toDataURL("image/jpeg", t.quality || 1) : null;
}
function G(o, t) {
  return I(o, t || {}).then(h.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 I(o, t) {
  return j(o, t).then(h.makeImage).then(h.delay(100)).then(function(a) {
    const l = n(o, t), r = l.getContext("2d");
    return r ? (r.drawImage(a, 0, 0), l) : new HTMLCanvasElement();
  }).catch(function(a) {
    console.error("Failed to draw canvas:", a);
  });
  function n(a, l) {
    const r = document.createElement("canvas"), s = a.getBoundingClientRect(), d = window.devicePixelRatio || 1;
    r.width = (l.width || s.width) * d, r.height = (l.height || s.height) * d, r.style.width = (l.width || s.width) + "px", r.style.height = (l.height || s.height) + "px";
    const m = r.getContext("2d");
    return m ? (m.scale(d, d), l.bgcolor && (m.fillStyle = l.bgcolor, m.fillRect(0, 0, r.width, r.height)), r) : new HTMLCanvasElement();
  }
}
async function O(o, t, n) {
  if (!n && t && !t(o)) return Promise.resolve();
  const a = await Promise.resolve(o), l = await s(a), r = await d(o, l, t);
  return m(o, r);
  function s(g) {
    return g instanceof HTMLCanvasElement ? h.makeImage(g.toDataURL()) : g.cloneNode(!1);
  }
  async function d(g, c, T) {
    const x = g.childNodes;
    if (x.length === 0) return Promise.resolve(c);
    return await C(c, h.asArray(x), T), c;
    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, c) {
    if (!(c instanceof Element)) return c;
    return Promise.resolve().then(T).then(x).then(C).then(U).then(function() {
      return c;
    });
    function T() {
      v(window.getComputedStyle(g), c.style);
      function v(p, w) {
        p.cssText ? w.cssText = p.cssText : e(p, w);
        function e(i, f) {
          h.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 = h.uid();
        c.className = c.className + " " + i;
        const f = document.createElement("style");
        f.appendChild(u(i, p, w)), c.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 h.asArray(A).map(D).join("; ") + ";";
            function D(M) {
              return M + ": " + A.getPropertyValue(M) + (A.getPropertyPriority(M) ? " !important" : "");
            }
          }
        }
      }
    }
    function C() {
      g instanceof HTMLTextAreaElement && (c.innerHTML = g.value), g instanceof HTMLInputElement && c.setAttribute("value", g.value);
    }
    function U() {
      c instanceof SVGElement && (c.setAttribute("xmlns", "http://www.w3.org/2000/svg"), c instanceof SVGRectElement && ["width", "height"].forEach(function(v) {
        const p = c.getAttribute(v);
        p && c.style.setProperty(v, p);
      }));
    }
  }
}
async function W(o) {
  const t = await F.resolveAll(), n = document.createElement("style");
  return o.appendChild(n), n.appendChild(document.createTextNode(t)), o;
}
function q(o) {
  return N.inlineAll(o).then(function() {
    return o;
  });
}
function J(o, t, n) {
  return Promise.resolve(o).then(function(a) {
    return a.setAttribute("xmlns", "http://www.w3.org/1999/xhtml"), new XMLSerializer().serializeToString(a);
  }).then(h.escapeXhtml).then(function(a) {
    return '<foreignObject x="0" y="0" width="100%" height="100%">' + a + "</foreignObject>";
  }).then(function(a) {
    return '<svg xmlns="http://www.w3.org/2000/svg" width="' + t + '" height="' + n + '">' + a + "</svg>";
  }).then(function(a) {
    return "data:image/svg+xml;charset=utf-8," + a;
  });
}
function z() {
  return {
    escape: T,
    parseExtension: t,
    mimeType: n,
    dataAsUrl: c,
    isDataUrl: a,
    canvasToBlob: r,
    resolveUrl: s,
    getAndEncode: g,
    uid: d(),
    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"
    };
  }
  function t(e) {
    const i = /\.([^\.\/]*?)$/g.exec(e);
    return i ? i[1] : "";
  }
  function n(e) {
    const i = t(e).toLowerCase();
    return o()[i] || "";
  }
  function a(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 s(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 d() {
    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 c(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: t,
    impl: {
      readUrls: n,
      inline: a
    }
  };
  function t(r) {
    return r.search(o) !== -1;
  }
  function n(r) {
    const s = [];
    let d;
    for (; (d = o.exec(r)) !== null; )
      s.push(d[1]);
    return s.filter(function(m) {
      return !h.isDataUrl(m);
    });
  }
  function a(r, s, d, m) {
    return Promise.resolve(s).then(function(c) {
      return d ? h.resolveUrl(c, d) : c;
    }).then(m || h.getAndEncode).then(function(c) {
      return h.dataAsUrl(c, h.mimeType(s));
    }).then(function(c) {
      return r.replace(g(s), "$1" + c + "$3");
    });
    function g(c) {
      return new RegExp(`(url\\(['"]?)(` + h.escape(c) + `)(['"]?\\))`, "g");
    }
  }
  function l(r, s, d) {
    if (m()) return Promise.resolve(r);
    return Promise.resolve(r).then(n).then(function(g) {
      let c = Promise.resolve(r);
      return g.forEach(function(T) {
        c = c.then(function(x) {
          return a(x, T, s, d);
        });
      }), c;
    });
    function m() {
      return !t(r);
    }
  }
}
function Q() {
  return {
    resolveAll: o,
    impl: {
      readAll: t
    }
  };
  function o() {
    return t().then(function(n) {
      return Promise.all(
        n.map(function(a) {
          return a.resolve();
        })
      );
    }).then(function(n) {
      return n.join(`
`);
    });
  }
  function t() {
    return Promise.resolve(h.asArray(document.styleSheets)).then(a).then(n).then(function(r) {
      return r.map(l);
    });
    function n(r) {
      return r.filter(function(s) {
        return s.type === CSSRule.FONT_FACE_RULE;
      }).filter(function(s) {
        return R.shouldProcess(s.style.getPropertyValue("src"));
      });
    }
    function a(r) {
      const s = [];
      return r.forEach(function(d) {
        if (Object.prototype.hasOwnProperty.call(d, "cssRules"))
          try {
            h.asArray(d.cssRules || []).forEach(s.push.bind(s));
          } catch (m) {
            console.log("Error while reading CSS rules from " + d.href, m.toString());
          }
      }), s;
    }
    function l(r) {
      return {
        resolve: function() {
          const d = (r.parentStyleSheet || {}).href;
          return R.inlineAll(r.cssText, d);
        },
        src: function() {
          return r.style.getPropertyValue("src");
        }
      };
    }
  }
}
function Y() {
  return {
    inlineAll: t,
    impl: {
      newImage: o
    }
  };
  function o(n) {
    return {
      inline: a
    };
    function a(l) {
      return h.isDataUrl(n.src) ? Promise.resolve() : Promise.resolve(n.src).then(l || h.getAndEncode).then(function(r) {
        return h.dataAsUrl(r, h.mimeType(n.src));
      }).then(function(r) {
        return new Promise(function(s, d) {
          n.onload = s, n.onerror = d, n.src = r, n.srcset = "";
        });
      });
    }
  }
  function t(n) {
    if (!(n instanceof Element)) return Promise.resolve(n);
    return a(n).then(function() {
      return n instanceof HTMLImageElement ? o(n).inline() : Promise.all(
        h.asArray(n.childNodes).map(function(l) {
          return t(l);
        })
      );
    });
    function a(l) {
      const r = l.style.getPropertyValue("background");
      return r ? R.inlineAll(r).then(function(s) {
        l.style.setProperty(
          "background",
          s,
          l.style.getPropertyPriority("background")
        );
      }).then(function() {
        return l;
      }) : Promise.resolve(l);
    }
  }
}
export {
  b as domtoimage
};
