import fs from 'node:fs';
import path from 'node:path';

// src/features/watch-pr-review/infrastructure/store.ts
var DEFAULT_POLL_INTERVAL_MS = 1e4;
var MIN_POLL_INTERVAL_MS = 3e3;
var DEFAULT_CONCURRENCY = 2;
var MIN_CONCURRENCY = 1;
var MAX_CONCURRENCY = 8;
var DEFAULT_CONFIG = {
  concurrency: DEFAULT_CONCURRENCY,
  pollIntervalMs: DEFAULT_POLL_INTERVAL_MS,
  repos: [{ repo: "owner/repository", enabled: false }]
};
var DEFAULT_HISTORY = { reviewed: [] };
function getWatchPaths(baseDir) {
  const dir = path.join(baseDir, "watch-pr-review");
  return {
    configPath: path.join(dir, "repos.json"),
    dir,
    historyPath: path.join(dir, "history.json"),
    reportsDir: path.join(dir, "reports")
  };
}
function ensureWatchFiles(watchPaths) {
  fs.mkdirSync(watchPaths.dir, { recursive: true });
  fs.mkdirSync(watchPaths.reportsDir, { recursive: true });
  if (!fs.existsSync(watchPaths.configPath)) {
    fs.writeFileSync(watchPaths.configPath, `${JSON.stringify(DEFAULT_CONFIG, null, 2)}
`, "utf8");
  }
  if (!fs.existsSync(watchPaths.historyPath)) {
    fs.writeFileSync(watchPaths.historyPath, `${JSON.stringify(DEFAULT_HISTORY, null, 2)}
`, "utf8");
  }
}
function loadWatchConfig(watchPaths) {
  try {
    const raw = fs.readFileSync(watchPaths.configPath, "utf8");
    const parsed = JSON.parse(raw);
    return {
      concurrency: normalizeConcurrency(parsed.concurrency),
      pollIntervalMs: normalizePollInterval(parsed.pollIntervalMs),
      repos: Array.isArray(parsed.repos) ? parsed.repos : []
    };
  } catch {
    return { ...DEFAULT_CONFIG, repos: [] };
  }
}
function loadWatchHistory(watchPaths) {
  try {
    const raw = fs.readFileSync(watchPaths.historyPath, "utf8");
    const parsed = JSON.parse(raw);
    return { reviewed: Array.isArray(parsed.reviewed) ? parsed.reviewed : [] };
  } catch {
    return { reviewed: [] };
  }
}
function saveWatchHistory(watchPaths, history) {
  fs.writeFileSync(watchPaths.historyPath, `${JSON.stringify(history, null, 2)}
`, "utf8");
}
function hasReviewedSameCommits(history, url, commitOids) {
  const previous = history.reviewed.find((entry) => entry.url === url);
  if (!previous) {
    return false;
  }
  return isSameCommitSet(previous.commitOids, commitOids);
}
function upsertReviewedPr(history, entry) {
  const reviewed = history.reviewed.filter((item) => item.url !== entry.url);
  reviewed.push(entry);
  return { reviewed };
}
function normalizePollInterval(value) {
  if (typeof value !== "number" || Number.isNaN(value)) {
    return DEFAULT_POLL_INTERVAL_MS;
  }
  return Math.max(MIN_POLL_INTERVAL_MS, value);
}
function normalizeConcurrency(value) {
  if (typeof value !== "number" || Number.isNaN(value)) {
    return DEFAULT_CONCURRENCY;
  }
  return Math.min(MAX_CONCURRENCY, Math.max(MIN_CONCURRENCY, Math.floor(value)));
}
function isSameCommitSet(a, b) {
  if (a.length !== b.length) {
    return false;
  }
  const setA = new Set(a);
  return b.every((oid) => setA.has(oid));
}

export { DEFAULT_CONCURRENCY, DEFAULT_POLL_INTERVAL_MS, ensureWatchFiles, getWatchPaths, hasReviewedSameCommits, loadWatchConfig, loadWatchHistory, saveWatchHistory, upsertReviewedPr };
