'use strict';

var cloneNode = require('./clone-node.js');
var embedImages = require('./embed-images.js');
var applyStyle = require('./apply-style.js');
var embedWebfonts = require('./embed-webfonts.js');
var util = require('./util.js');

async function toSvg(node, options = {}) {
    const { width, height } = util.getImageSize(node, options);
    const clonedNode = (await cloneNode.cloneNode(node, options, true));
    await embedWebfonts.embedWebFonts(clonedNode, options);
    await embedImages.embedImages(clonedNode, options);
    applyStyle.applyStyle(clonedNode, options);
    const datauri = await util.nodeToDataURL(clonedNode, width, height);
    return datauri;
}
async function toCanvas(node, options = {}) {
    const { width, height } = util.getImageSize(node, options);
    const svg = await toSvg(node, options);
    const img = await util.createImage(svg);
    const canvas = document.createElement('canvas');
    const context = canvas.getContext('2d');
    const ratio = options.pixelRatio || util.getPixelRatio();
    const canvasWidth = options.canvasWidth || width;
    const canvasHeight = options.canvasHeight || height;
    canvas.width = canvasWidth * ratio;
    canvas.height = canvasHeight * ratio;
    if (!options.skipAutoScale) {
        util.checkCanvasDimensions(canvas);
    }
    canvas.style.width = `${canvasWidth}`;
    canvas.style.height = `${canvasHeight}`;
    if (options.backgroundColor) {
        context.fillStyle = options.backgroundColor;
        context.fillRect(0, 0, canvas.width, canvas.height);
    }
    context.drawImage(img, 0, 0, canvas.width, canvas.height);
    return canvas;
}
async function toPixelData(node, options = {}) {
    const { width, height } = util.getImageSize(node, options);
    const canvas = await toCanvas(node, options);
    const ctx = canvas.getContext('2d');
    return ctx.getImageData(0, 0, width, height).data;
}
async function toPng(node, options = {}) {
    const canvas = await toCanvas(node, options);
    return canvas.toDataURL();
}
async function toJpeg(node, options = {}) {
    const canvas = await toCanvas(node, options);
    return canvas.toDataURL('image/jpeg', options.quality || 1);
}
async function toBlob(node, options = {}) {
    const canvas = await toCanvas(node, options);
    const blob = await util.canvasToBlob(canvas);
    return blob;
}
async function getFontEmbedCSS(node, options = {}) {
    return embedWebfonts.getWebFontCSS(node, options);
}

exports.getFontEmbedCSS = getFontEmbedCSS;
exports.toBlob = toBlob;
exports.toCanvas = toCanvas;
exports.toJpeg = toJpeg;
exports.toPixelData = toPixelData;
exports.toPng = toPng;
exports.toSvg = toSvg;
//# sourceMappingURL=index.js.map
