/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import path from 'path';
import { noColors } from '@isomorphic/colors';
import { msToString } from '@isomorphic/formatUtils';
import { TerminalReporter, formatResultFailure, formatRetry } from './base';
import { stripAnsiEscapes } from '../util';
class GitHubLogger {
    _log(message, type = 'notice', options = {}) {
        message = message.replace(/\n/g, '%0A');
        const configs = Object.entries(options)
            .map(([key, option]) => `${key}=${option}`)
            .join(',');
        // eslint-disable-next-line no-restricted-properties
        process.stdout.write(stripAnsiEscapes(`::${type} ${configs}::${message}\n`));
    }
    debug(message, options) {
        this._log(message, 'debug', options);
    }
    error(message, options) {
        this._log(message, 'error', options);
    }
    notice(message, options) {
        this._log(message, 'notice', options);
    }
    warning(message, options) {
        this._log(message, 'warning', options);
    }
}
export class GitHubReporter extends TerminalReporter {
    githubLogger = new GitHubLogger();
    constructor(options = {}) {
        super(options);
        this.screen = { ...this.screen, colors: noColors };
    }
    printsToStdio() {
        return false;
    }
    async onEnd(result) {
        await super.onEnd(result);
        this._printAnnotations();
    }
    onError(error) {
        const errorMessage = this.formatError(error).message;
        this.githubLogger.error(errorMessage);
    }
    _printAnnotations() {
        const summary = this.generateSummary();
        const summaryMessage = this.generateSummaryMessage(summary);
        if (summary.failuresToPrint.length)
            this._printFailureAnnotations(summary.failuresToPrint);
        this._printSlowTestAnnotations();
        this._printSummaryAnnotation(summaryMessage);
    }
    _printSlowTestAnnotations() {
        this.getSlowTests().forEach(([file, duration]) => {
            const filePath = workspaceRelativePath(path.join(process.cwd(), file));
            this.githubLogger.warning(`${filePath} took ${msToString(duration)}`, {
                title: 'Slow Test',
                file: filePath,
            });
        });
    }
    _printSummaryAnnotation(summary) {
        this.githubLogger.notice(summary, {
            title: '🎭 Playwright Run Summary'
        });
    }
    _printFailureAnnotations(failures) {
        failures.forEach((test, index) => {
            const title = this.formatTestTitle(test);
            const header = this.formatTestHeader(test, { indent: '  ', index: index + 1, mode: 'error' });
            for (const result of test.results) {
                const errors = formatResultFailure(this.screen, test, result, '    ');
                for (const error of errors) {
                    const options = {
                        file: workspaceRelativePath(error.location?.file || test.location.file),
                        title,
                    };
                    if (error.location) {
                        options.line = error.location.line;
                        options.col = error.location.column;
                    }
                    const message = [header, ...formatRetry(this.screen, result), error.message].join('\n');
                    this.githubLogger.error(message, options);
                }
            }
        });
    }
}
function workspaceRelativePath(filePath) {
    return path.relative(process.env['GITHUB_WORKSPACE'] ?? '', filePath);
}
export default GitHubReporter;
//# sourceMappingURL=github.js.map