"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.evaluationString = exports.convertHeadersToObject = exports.convertHeadersToEntries = exports.clamp = exports.findVisibleQuad = exports.arePointsEqual = exports.quadCenterPoint = exports.computeQuadArea = exports.fromCdpQuad = exports.rectToQuad = exports.asyncRegexpReplace = void 0;
async function asyncRegexpReplace(str, regexp, replacer) {
    const matches = [];
    regexp.lastIndex = 0;
    let m = regexp.exec(str);
    while (m) {
        matches.push(m);
        if (!regexp.flags.includes('g')) {
            break;
        }
        m = regexp.exec(str);
    }
    const replacements = await Promise.all(matches.map(m => replacer(m)));
    let result = str;
    let offset = 0;
    for (let i = 0; i < matches.length; i++) {
        const m = matches[i];
        const r = replacements[i];
        const index = m.index + offset;
        const length = m[0].length;
        result = result.substring(0, index) + r + result.substring(index + length);
        offset = offset + r.length - length;
    }
    return result;
}
exports.asyncRegexpReplace = asyncRegexpReplace;
function rectToQuad(rect) {
    return [
        { x: rect.left, y: rect.top },
        { x: rect.left + rect.width, y: rect.top },
        { x: rect.left + rect.width, y: rect.top + rect.height },
        { x: rect.left, y: rect.top + rect.height },
    ];
}
exports.rectToQuad = rectToQuad;
function fromCdpQuad(quad) {
    return [
        { x: quad[0], y: quad[1] },
        { x: quad[2], y: quad[3] },
        { x: quad[4], y: quad[5] },
        { x: quad[6], y: quad[7] },
    ];
}
exports.fromCdpQuad = fromCdpQuad;
function computeQuadArea(quad) {
    let area = 0;
    for (let i = 0; i < quad.length; i++) {
        const p1 = quad[i];
        const p2 = quad[(i + 1) % quad.length];
        area += (p1.x * p2.y - p2.x * p1.y) / 2;
    }
    return area;
}
exports.computeQuadArea = computeQuadArea;
function quadCenterPoint(quad) {
    let x = 0;
    let y = 0;
    for (const point of quad) {
        x += point.x;
        y += point.y;
    }
    return {
        x: x / 4,
        y: y / 4,
    };
}
exports.quadCenterPoint = quadCenterPoint;
function arePointsEqual(a, b) {
    return a.x === b.x && a.y === b.y;
}
exports.arePointsEqual = arePointsEqual;
function findVisibleQuad(quads, viewportWidth, viewportHeight) {
    for (const quad of quads) {
        const trimmedQuad = quad.map(p => {
            return {
                x: clamp(p.x, 0, viewportWidth),
                y: clamp(p.y, 0, viewportHeight),
            };
        });
        if (computeQuadArea(trimmedQuad) > 0) {
            return trimmedQuad;
        }
    }
    return null;
}
exports.findVisibleQuad = findVisibleQuad;
function clamp(number, min, max) {
    return Math.min(Math.max(Number(number || 0), min), max);
}
exports.clamp = clamp;
function convertHeadersToEntries(headers) {
    if (Array.isArray(headers)) {
        return headers;
    }
    return Object.entries(headers).map(entry => {
        return {
            name: entry[0],
            value: entry[1],
        };
    });
}
exports.convertHeadersToEntries = convertHeadersToEntries;
function convertHeadersToObject(headers) {
    if (Array.isArray(headers)) {
        const result = {};
        for (const { name, value } of headers) {
            result[name] = value;
        }
        return result;
    }
    return headers;
}
exports.convertHeadersToObject = convertHeadersToObject;
function evaluationString(pageFn, ...args) {
    const functionDeclaration = pageFn.toString();
    function serializeArgument(arg) {
        if (Object.is(arg, undefined)) {
            return 'undefined';
        }
        return JSON.stringify(arg);
    }
    return `(${functionDeclaration})(${args.map(serializeArgument).join(',')})`;
}
exports.evaluationString = evaluationString;
//# sourceMappingURL=cdp-util.js.map