"use strict";
// Copyright 2024 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.DiffBehaviors = void 0;
exports.expandResponseFiles = expandResponseFiles;
exports.asArray = asArray;
exports.commandLineArgs = commandLineArgs;
const fs = __importStar(require("node:fs"));
function expandResponseFiles(args) {
    const expanded = [];
    for (const arg of args) {
        if (arg.startsWith('@') && arg.length > 1) {
            const rspPath = arg.substring(1);
            if (fs.existsSync(rspPath)) {
                const content = fs.readFileSync(rspPath, 'utf-8');
                const lines = content.split(/\r?\n/).map(line => line.trim()).filter(line => line.length > 0 && !line.startsWith('#'));
                for (const line of expandResponseFiles(lines)) {
                    expanded.push(line);
                }
            }
            else {
                expanded.push(arg);
            }
        }
        else {
            expanded.push(arg);
        }
    }
    return expanded;
}
var DiffBehaviors;
(function (DiffBehaviors) {
    DiffBehaviors["UPDATE"] = "update";
    DiffBehaviors["THROW"] = "throw";
    DiffBehaviors["NO_THROW"] = "no-throw";
    DiffBehaviors["NO_UPDATE"] = "no-update";
})(DiffBehaviors || (exports.DiffBehaviors = DiffBehaviors = {}));
function asArray(value) {
    if (!value) {
        return [];
    }
    if (Array.isArray(value)) {
        return value;
    }
    return [value];
}
function validateDiffBehaviors(args) {
    const failed = [];
    for (const arg of asArray(args)) {
        if (Object.values(DiffBehaviors).includes(arg)) {
            continue;
        }
        if (!arg.startsWith(`${DiffBehaviors.UPDATE}=`)) {
            failed.push(arg);
        }
    }
    if (failed.length > 0) {
        throw new Error(`Invalid options for --on-diff: ${failed}. Valid options are: ${Object.values(DiffBehaviors).join(', ')}`);
    }
    return asArray(args);
}
function commandLineArgs(yargs) {
    return yargs
        .parserConfiguration({
        'camel-case-expansion': false,
    })
        // TODO: add description
        .command('$0 [tests..]', '')
        .option('debug', {
        type: 'boolean',
        default: false,
        desc: 'Execute tests in debug mode',
    })
        .option('headless', {
        type: 'boolean',
        desc: 'Whether to run tests headless. If unspecified, the default depends on the `debug` option',
    })
        .option('coverage', {
        type: 'boolean',
        default: false,
        desc: 'Enable coverage reporting',
    })
        .option('artifacts-dir', {
        type: 'string',
        desc: 'Path to a directory to store test artifacts in (e.g. coverage reports)',
    })
        .option('chrome-binary', {
        type: 'string',
        desc: 'Path to a custom Chrome binary to run',
    })
        .option('on-diff', {
        type: 'string',
        coerce: validateDiffBehaviors,
        choices: Object.values(DiffBehaviors),
        desc: 'Define how to deal with diffs in snapshots/screenshots',
    })
        .option('verbose', {
        alias: 'v',
        type: 'count',
        desc: 'Increases the log level',
    })
        .option('shuffle', {
        type: 'boolean',
        desc: 'Execute tests in random order',
        default: false,
    })
        .option('repeat', {
        type: 'number',
        default: 1,
        desc: 'Reruns the test X number of times regardless of result (e2e tests only)',
    })
        .option('retries', {
        type: 'number',
        desc: 'Reruns the tests if upon failure at max X number of times',
        default: 0,
    })
        .option('grep', {
        type: 'string',
        conflicts: 'fgrep',
        desc: 'Filter tests by name using grep',
    })
        .option('fgrep', {
        type: 'string',
        conflicts: 'grep',
        desc: 'Filter tests by name using fgrep',
    })
        .option('invert-grep', {
        type: 'boolean',
        default: false,
        desc: 'Invert the grep/fgrep result',
    })
        .option('cpu-throttle', {
        type: 'number',
        default: 1,
        desc: 'Throttle the CPU during tests. The number provided is the multiplier used.',
    })
        .option('shard-count', {
        type: 'number',
        desc: 'Total number of shards to split the tests into.',
        implies: 'shard-number',
        default: 1,
    })
        .option('shard-number', {
        type: 'number',
        desc: 'The shard number to run (1-based).',
        implies: 'shard-count',
        default: 1,
    })
        .option('shard-bias', {
        type: 'number',
        desc: 'The bias to be used when calculating the sharding ' +
            'hash. Biases the distribution of tests between different ' +
            'shards for a given shard count. This is useful for ' +
            're-sharding without changing the shard count.',
        implies: ['shard-count', 'shard-number'], // shard-bias only makes sense if sharding is enabled
        default: 0,
    })
        .option('expectations-file', {
        type: 'string',
        desc: 'Path to a custom TestExpectations file',
    });
}
//# sourceMappingURL=commandline.js.map