import { tools, findToolByName, getToolDefaultVersion, findMatrixByType, findToolsByType, getToolSupportedVersions } from './W2CW3JVG.js';
import { parseVersionFileContents } from './3CLGGUZ6.js';
import { findBestCompatibleCombination } from './HR64SZQX.js';
import { init_esm_shims, __name } from './4RNWLMHM.js';
import process from 'process';

// src/core/version-resolver.ts
init_esm_shims();
function extractUserVersions(options) {
  const userVersions = {};
  for (const tool of tools) {
    const versionKey = `${tool.name}Version`;
    const versionValue = options[versionKey];
    if (typeof versionValue === "string" && versionValue !== "") {
      userVersions[tool.name] = versionValue;
    }
  }
  return userVersions;
}
__name(extractUserVersions, "extractUserVersions");
function resolveToolVersion(toolName, userSpecifiedVersions, options, cwd) {
  const tool = findToolByName(toolName);
  if (!tool) {
    throw new Error(`Tool ${toolName} not found`);
  }
  const userVersion = userSpecifiedVersions[toolName];
  if (typeof userVersion === "string" && userVersion !== "") {
    return userVersion;
  }
  const envKey = `${tool.name.toUpperCase()}_VERSION`;
  const envVersion = process.env[envKey];
  if (typeof envVersion === "string" && envVersion !== "") {
    return envVersion;
  }
  try {
    const parsedVersions = parseVersionFileContents(cwd);
    const versionKey = `${tool.name}_version`.toUpperCase();
    const foundVersion = parsedVersions[versionKey];
    if (typeof foundVersion === "string" && foundVersion !== "") {
      return foundVersion;
    }
  } catch (error) {
  }
  return getToolDefaultVersion(tool.name);
}
__name(resolveToolVersion, "resolveToolVersion");
function resolveTypeVersions(type, userSpecifiedVersions, options, cwd) {
  const matrix = findMatrixByType(type);
  const typeTools = findToolsByType(type);
  if (!matrix || typeTools.length === 0) {
    throw new Error(`Type ${type} not found or no version matrix available`);
  }
  const resolvedVersions = {};
  const usedDefaultFor = [];
  const warnings = [];
  for (const tool of typeTools) {
    const version = resolveToolVersion(tool.name, userSpecifiedVersions, options, cwd);
    if (!version) {
      continue;
    }
    resolvedVersions[tool.name] = version;
    if (version === getToolDefaultVersion(tool.name)) {
      usedDefaultFor.push(tool.name);
    }
    if (version !== getToolDefaultVersion(tool.name)) {
      const supportedVersions = getToolSupportedVersions(tool.name);
      if (!supportedVersions.includes(version)) {
        warnings.push(`\u26A0\uFE0F  Version ${version} for ${tool.name} is not in the supported list.
   Supported versions: ${supportedVersions.join(", ")}
   Continuing with Docker availability as final validation...`);
      }
    }
  }
  let matchedCombination;
  let isCompatibleCombination = false;
  for (const combo of matrix.combinations) {
    const matches = Object.entries(combo.versions).every(([tool, version]) => resolvedVersions[tool] === version);
    if (matches) {
      matchedCombination = combo;
      isCompatibleCombination = true;
      break;
    }
  }
  if (!isCompatibleCombination && usedDefaultFor.length > 0) {
    const compatibleCombo = findBestCompatibleCombination(resolvedVersions, matrix);
    if (compatibleCombo) {
      let adjusted = false;
      for (const toolName of usedDefaultFor) {
        const comboVersion = compatibleCombo.versions[toolName];
        if (comboVersion !== void 0 && comboVersion !== resolvedVersions[toolName]) {
          const oldVersion = resolvedVersions[toolName];
          resolvedVersions[toolName] = comboVersion;
          warnings.push(`\u{1F504} Auto-adjusted ${toolName}: ${oldVersion} \u2192 ${comboVersion} (for compatibility)`);
          adjusted = true;
        }
      }
      if (adjusted) {
        matchedCombination = compatibleCombo;
        isCompatibleCombination = true;
      }
    }
  }
  if (!isCompatibleCombination) {
    const recommendedCombos = matrix.combinations.filter((c) => c.isRecommended).map((c) => c.id).join(", ");
    warnings.push(`\u26A0\uFE0F  Custom version combination detected - not officially tested.
   \u{1F4A1} Stable combinations: ${recommendedCombos}
   \u{1F50D} Use 'lz-tool --list-versions' to see all tested combinations.`);
  } else if (matchedCombination?.description !== void 0) {
    console.info(`\u2705 ${matchedCombination.description} (${matchedCombination.id})`);
  }
  return {
    resolvedVersions,
    isCompatibleCombination,
    usedDefaultFor,
    warnings,
    combinationId: matchedCombination?.id
  };
}
__name(resolveTypeVersions, "resolveTypeVersions");

export { extractUserVersions, resolveToolVersion, resolveTypeVersions };
//# sourceMappingURL=A7N65AYR.js.map
//# sourceMappingURL=A7N65AYR.js.map