'use strict';

var DWHHPWIY_cjs = require('./DWHHPWIY.cjs');
var _5RAWDGJY_cjs = require('./5RAWDGJY.cjs');
var JJXZWB7K_cjs = require('./JJXZWB7K.cjs');
var VO65XOWD_cjs = require('./VO65XOWD.cjs');
var process = require('process');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var process__default = /*#__PURE__*/_interopDefault(process);

// src/core/version-resolver.ts
VO65XOWD_cjs.init_cjs_shims();
function extractUserVersions(options) {
  const userVersions = {};
  for (const tool of DWHHPWIY_cjs.tools) {
    const versionKey = `${tool.name}Version`;
    const versionValue = options[versionKey];
    if (typeof versionValue === "string" && versionValue !== "") {
      userVersions[tool.name] = versionValue;
    }
  }
  return userVersions;
}
VO65XOWD_cjs.__name(extractUserVersions, "extractUserVersions");
function resolveToolVersion(toolName, userSpecifiedVersions, options, cwd) {
  const tool = DWHHPWIY_cjs.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__default.default.env[envKey];
  if (typeof envVersion === "string" && envVersion !== "") {
    return envVersion;
  }
  try {
    const parsedVersions = _5RAWDGJY_cjs.parseVersionFileContents(cwd);
    const versionKey = `${tool.name}_version`.toUpperCase();
    const foundVersion = parsedVersions[versionKey];
    if (typeof foundVersion === "string" && foundVersion !== "") {
      return foundVersion;
    }
  } catch (error) {
  }
  return DWHHPWIY_cjs.getToolDefaultVersion(tool.name);
}
VO65XOWD_cjs.__name(resolveToolVersion, "resolveToolVersion");
function resolveTypeVersions(type, userSpecifiedVersions, options, cwd) {
  const matrix = DWHHPWIY_cjs.findMatrixByType(type);
  const typeTools = DWHHPWIY_cjs.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 === DWHHPWIY_cjs.getToolDefaultVersion(tool.name)) {
      usedDefaultFor.push(tool.name);
    }
    if (version !== DWHHPWIY_cjs.getToolDefaultVersion(tool.name)) {
      const supportedVersions = DWHHPWIY_cjs.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 = JJXZWB7K_cjs.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
  };
}
VO65XOWD_cjs.__name(resolveTypeVersions, "resolveTypeVersions");

exports.extractUserVersions = extractUserVersions;
exports.resolveToolVersion = resolveToolVersion;
exports.resolveTypeVersions = resolveTypeVersions;
//# sourceMappingURL=TYFYAP7W.cjs.map
//# sourceMappingURL=TYFYAP7W.cjs.map