import {
  readPair,
  resolvePair
} from "./chunk-YNB73F22.js";
import {
  log
} from "./chunk-2ODBA7MQ.js";
import "./chunk-PZ5AY32C.js";

// src/contradiction/resolution.ts
var VALID_VERBS = ["keep-a", "keep-b", "merge", "both-valid", "needs-more-context"];
function isValidResolutionVerb(value) {
  return VALID_VERBS.includes(value);
}
async function executeResolution(memoryDir, storage, pairId, verb) {
  const pair = readPair(memoryDir, pairId);
  if (!pair) {
    return { pairId, verb, affectedIds: [], message: `Pair ${pairId} not found` };
  }
  if (pair.resolution) {
    return { pairId, verb, affectedIds: [], message: `Pair already resolved with verb "${pair.resolution}"` };
  }
  const [idA, idB] = pair.memoryIds;
  const affectedIds = [];
  let message = "";
  let supersedeFailed = false;
  switch (verb) {
    case "keep-a": {
      const ok = await supersedeSafe(storage, idB, idA, "contradiction-resolution:keep-a");
      if (ok) {
        affectedIds.push(idB);
        message = `Kept ${idA}, superseded ${idB}`;
      } else {
        supersedeFailed = true;
        message = `Supersede failed for ${idB}; not resolving`;
      }
      break;
    }
    case "keep-b": {
      const ok = await supersedeSafe(storage, idA, idB, "contradiction-resolution:keep-b");
      if (ok) {
        affectedIds.push(idA);
        message = `Kept ${idB}, superseded ${idA}`;
      } else {
        supersedeFailed = true;
        message = `Supersede failed for ${idA}; not resolving`;
      }
      break;
    }
    case "merge": {
      const mergedId = `merged-${pairId}`;
      const okA = await supersedeSafe(storage, idA, mergedId, "contradiction-resolution:merge");
      const okB = await supersedeSafe(storage, idB, mergedId, "contradiction-resolution:merge");
      if (okA) affectedIds.push(idA);
      if (okB) affectedIds.push(idB);
      if (!okA || !okB) {
        supersedeFailed = true;
        message = `Merge incomplete: ${affectedIds.length}/2 superseded; not resolving to allow retry`;
      } else {
        message = `Both memories superseded by merged ${mergedId}`;
      }
      break;
    }
    case "both-valid": {
      message = "Pair marked as both-valid; cooldown applied";
      break;
    }
    case "needs-more-context": {
      message = "Deferred; no action taken, short cooldown applied";
      break;
    }
  }
  if (!supersedeFailed) {
    resolvePair(memoryDir, pairId, verb);
  }
  log.info("[contradiction-resolution] pair=%s verb=%s affected=%d", pairId, verb, affectedIds.length);
  return { pairId, verb, affectedIds, message };
}
async function supersedeSafe(storage, oldId, newId, reason) {
  try {
    const result = await storage.supersedeMemory(oldId, newId, reason);
    if (result === false) {
      log.warn("[contradiction-resolution] supersede returned false for %s \u2192 %s", oldId, newId);
      return false;
    }
    return true;
  } catch (err) {
    log.warn(
      "[contradiction-resolution] supersede failed %s \u2192 %s: %s",
      oldId,
      newId,
      err instanceof Error ? err.message : err
    );
    return false;
  }
}
export {
  executeResolution,
  isValidResolutionVerb
};
//# sourceMappingURL=resolution-YGIBORXI.js.map