import { regEx } from "../../../util/regex.js";
import { TerraformProviderDatasource } from "../../datasource/terraform-provider/index.js";
import { extractLocks, findLockFile, readLockFile } from "./lockfile/util.js";
import { isNonEmptyArray } from "@sindresorhus/is";

//#region lib/modules/manager/terraform/util.ts
function checkFileContainsDependency(content, checkList) {
	return checkList.some((check) => content.includes(check));
}
const pathStringRegex = regEx(/(.|..)?(\/[^/])+/);
function checkIfStringIsPath(path) {
	return !!pathStringRegex.exec(path);
}
function massageProviderLookupName(dep) {
	dep.packageName ??= dep.depName;
	if (!dep.packageName.includes("/")) dep.packageName = `hashicorp/${dep.packageName}`;
	dep.packageName = dep.packageName.toLowerCase();
}
function getLockedVersion(dep, locks) {
	const depRegistryUrl = dep.registryUrls ? dep.registryUrls[0] : TerraformProviderDatasource.defaultRegistryUrls[0];
	const foundLock = locks.find((lock) => lock.packageName === dep.packageName && lock.registryUrl === depRegistryUrl);
	if (foundLock) return foundLock.version;
}
async function extractLocksForPackageFile(fileName) {
	const locks = [];
	const lockFilePath = await findLockFile(fileName);
	if (lockFilePath) {
		const lockFileContent = await readLockFile(lockFilePath);
		if (lockFileContent) {
			const extractedLocks = extractLocks(lockFileContent);
			if (isNonEmptyArray(extractedLocks)) locks.push(...extractedLocks);
		}
	}
	return locks;
}

//#endregion
export { checkFileContainsDependency, checkIfStringIsPath, extractLocksForPackageFile, getLockedVersion, massageProviderLookupName };
//# sourceMappingURL=util.js.map