"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PathPair = exports.BUILD_WITH_CHROMIUM = exports.GEN_DIR = exports.BUILD_ROOT = exports.CHECKOUT_ROOT = exports.SOURCE_ROOT = void 0;
exports.rebase = rebase;
exports.isContainedInDirectory = isContainedInDirectory;
exports.defaultChromePath = defaultChromePath;
const os = __importStar(require("node:os"));
const path = __importStar(require("node:path"));
// @ts-expect-error created at test/BUILD.gn
const build_js_1 = __importDefault(require("../build.js"));
exports.SOURCE_ROOT = path.join(__dirname, '..', build_js_1.default.SOURCE_ROOT);
exports.CHECKOUT_ROOT = path.join(__dirname, '..', build_js_1.default.CHECKOUT_ROOT);
exports.BUILD_ROOT = path.join(__dirname, '..', build_js_1.default.BUILD_ROOT);
exports.GEN_DIR = path.normalize(path.join(__dirname, '..', '..'));
exports.BUILD_WITH_CHROMIUM = build_js_1.default.BUILD_WITH_CHROMIUM;
function rebase(fromRoot, toRoot, filename, newExt) {
    if (!path.isAbsolute(filename) || !path.isAbsolute(fromRoot) || !path.isAbsolute(toRoot)) {
        return filename;
    }
    const relative = path.relative(fromRoot, filename);
    if (relative.startsWith('.')) {
        return filename;
    }
    if (!path.relative(toRoot, filename).startsWith('.') && toRoot.length > fromRoot.length) {
        return filename;
    }
    const rebased = path.resolve(toRoot, relative);
    if (!newExt) {
        return rebased;
    }
    const { ext } = path.parse(rebased);
    return ext.length > 0 ? rebased.substr(0, rebased.length - ext.length) + newExt : rebased;
}
function isContainedInDirectory(contained, directory) {
    return !path.relative(directory, contained).startsWith('..');
}
class PathPair {
    sourcePath;
    buildPath;
    constructor(sourcePath, buildPath) {
        this.sourcePath = sourcePath;
        this.buildPath = buildPath;
        if (!path.isAbsolute(sourcePath) || !path.isAbsolute(buildPath)) {
            throw new Error('Repo paths must be absolute');
        }
    }
    static get(pathname) {
        const absPath = path.normalize(path.resolve(pathname));
        if (!absPath) {
            return null;
        }
        const sourcePath = rebase(exports.GEN_DIR, exports.SOURCE_ROOT, absPath, '.ts');
        const buildPath = rebase(exports.SOURCE_ROOT, exports.GEN_DIR, absPath, '.js');
        return new PathPair(sourcePath, buildPath);
    }
}
exports.PathPair = PathPair;
function defaultChromePath() {
    if (exports.BUILD_WITH_CHROMIUM) {
        // In a full chromium checkout, find the chrome binary in the build directory.
        const paths = {
            linux: path.join('chrome'),
            darwin: path.join('Chromium.app', 'Contents', 'MacOS', 'Chromium'),
            win32: path.join('chrome.exe'),
        };
        return path.join(exports.BUILD_ROOT, paths[os.platform()]);
    }
    return path.join(exports.SOURCE_ROOT, 'third_party', 'chrome', localChromePath());
}
function localChromePath() {
    const platform = os.platform();
    switch (platform) {
        case 'linux':
            return path.join('chrome-linux', 'chrome-linux64', 'chrome');
        case 'darwin': {
            const name = os.arch() === 'arm64' ? 'chrome-mac-arm64' : 'chrome-mac-x64';
            return path.join(name, name, 'Google Chrome for Testing.app', 'Contents', 'MacOS', 'Google Chrome for Testing');
        }
        case 'win32':
            return path.join('chrome-win', 'chrome-win64', 'chrome.exe');
        default:
            throw new Error(`Unsupported platform: ${platform}`);
    }
}
//# sourceMappingURL=paths.js.map