"use strict";
// Copyright 2026 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.sanitize = sanitize;
exports.formatDiff = formatDiff;
exports.resultAssertionsDiff = resultAssertionsDiff;
exports.formatDiffText = formatDiffText;
exports.formatSummary = formatSummary;
exports.formatAsHtml = formatAsHtml;
/* eslint-disable @typescript-eslint/no-explicit-any */
// @ts-expect-error
const diffImport = __importStar(require("diff"));
const diff = diffImport;
function sanitize(message) {
    return message.replaceAll('&', '&amp;')
        .replaceAll('<', '&lt;')
        .replaceAll('>', '&gt;')
        .replaceAll('"', '&quot;')
        .replaceAll('\'', '&#39;');
}
function* formatDiff(diffBlocks, onSame, onAdded, onRemoved) {
    for (const block of diffBlocks) {
        const lines = block.value.split('\n').filter(l => l.length > 0);
        if (!block.added && !block.removed && lines.length > 3) {
            yield onSame(lines[0]);
            yield onSame('  ...');
            yield onSame(lines[lines.length - 1]);
        }
        else {
            for (const line of lines) {
                if (block.added) {
                    yield onAdded(line);
                }
                else if (block.removed) {
                    yield onRemoved(line);
                }
                else {
                    yield onSame(line);
                }
            }
        }
    }
}
function resultAssertionsDiff(assertionErrors) {
    if (!assertionErrors || assertionErrors.length === 0) {
        return [];
    }
    const firstError = assertionErrors[0];
    if (!firstError) {
        return [];
    }
    const formatValue = (value) => {
        return (value && typeof value === 'object') ? JSON.stringify(value, null, 2) : String(value);
    };
    const expected = formatValue(firstError.expected);
    const actual = formatValue(firstError.actual);
    return diff.diffLines(expected, actual);
}
function formatDiffText(assertionDiff) {
    const diffLines = Array.from(formatDiff(assertionDiff, same => ` ${same}`, actual => `+${actual}`, expected => `-${expected}`));
    if (diffLines.length > 0) {
        return [
            '- expected',
            '+ actual',
            '',
            ...diffLines,
        ].join('\n');
    }
    return null;
}
// The max length of the summary is 4000, but we need to leave some room for
// the rest of the HTML formatting (e.g. <pre> and </pre>).
const SUMMARY_LENGTH_CUTOFF = 3950;
function formatSummary(message, diffText, cutoff = SUMMARY_LENGTH_CUTOFF) {
    let summary = message;
    if (diffText) {
        summary += '\n\n' + diffText;
    }
    const lines = summary.split('\n');
    let result = '';
    let currentLength = 13; // for <pre></pre> and some safety margin
    for (const line of lines) {
        const sanitized = sanitize(line);
        let highlighted = sanitized;
        if (line.startsWith('+')) {
            highlighted = `<span style="color: green;">${sanitized}</span>`;
        }
        else if (line.startsWith('-')) {
            highlighted = `<span style="color: red;">${sanitized}</span>`;
        }
        if (currentLength + highlighted.length + 1 > cutoff) {
            result += '\n... (truncated)';
            break;
        }
        if (result.length > 0) {
            result += '\n';
        }
        result += highlighted;
        currentLength += highlighted.length + 1;
    }
    return `<pre>${result}</pre>`;
}
function formatAsHtml(assertionDiff) {
    const diffText = formatDiffText(assertionDiff);
    if (!diffText) {
        return null;
    }
    return formatSummary('', diffText);
}
//# sourceMappingURL=diff-utils.js.map