"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractLocksForPackageFile = exports.getLockedVersion = exports.massageProviderLookupName = exports.checkIfStringIsPath = exports.checkFileContainsDependency = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const regex_1 = require("../../../util/regex");
const terraform_provider_1 = require("../../datasource/terraform-provider");
const util_1 = require("./lockfile/util");
function checkFileContainsDependency(content, checkList) {
    return checkList.some((check) => content.includes(check));
}
exports.checkFileContainsDependency = checkFileContainsDependency;
const pathStringRegex = (0, regex_1.regEx)(/(.|..)?(\/[^/])+/);
function checkIfStringIsPath(path) {
    const match = pathStringRegex.exec(path);
    return !!match;
}
exports.checkIfStringIsPath = checkIfStringIsPath;
function massageProviderLookupName(dep) {
    if (!dep.packageName) {
        dep.packageName = dep.depName;
    }
    // TODO #7154
    if (!dep.packageName.includes('/')) {
        dep.packageName = `hashicorp/${dep.packageName}`;
    }
    // handle cases like `Telmate/proxmox`
    // TODO #7154
    dep.packageName = dep.packageName.toLowerCase();
}
exports.massageProviderLookupName = massageProviderLookupName;
function getLockedVersion(dep, locks) {
    const depRegistryUrl = dep.registryUrls
        ? dep.registryUrls[0]
        : terraform_provider_1.TerraformProviderDatasource.defaultRegistryUrls[0];
    const foundLock = locks.find((lock) => lock.packageName === dep.packageName &&
        lock.registryUrl === depRegistryUrl);
    if (foundLock) {
        return foundLock.version;
    }
    return undefined;
}
exports.getLockedVersion = getLockedVersion;
async function extractLocksForPackageFile(fileName) {
    const locks = [];
    const lockFilePath = (0, util_1.findLockFile)(fileName);
    if (lockFilePath) {
        const lockFileContent = await (0, util_1.readLockFile)(lockFilePath);
        if (lockFileContent) {
            const extractedLocks = (0, util_1.extractLocks)(lockFileContent);
            if (is_1.default.nonEmptyArray(extractedLocks)) {
                locks.push(...extractedLocks);
            }
        }
    }
    return locks;
}
exports.extractLocksForPackageFile = extractLocksForPackageFile;
//# sourceMappingURL=util.js.map