'use strict';

var fs = require('fs');
var path = require('path');

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

var fs__default = /*#__PURE__*/_interopDefault(fs);
var path__default = /*#__PURE__*/_interopDefault(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__default.default.join(baseDir, "watch-pr-review");
  return {
    configPath: path__default.default.join(dir, "repos.json"),
    dir,
    historyPath: path__default.default.join(dir, "history.json"),
    reportsDir: path__default.default.join(dir, "reports")
  };
}
function ensureWatchFiles(watchPaths) {
  fs__default.default.mkdirSync(watchPaths.dir, { recursive: true });
  fs__default.default.mkdirSync(watchPaths.reportsDir, { recursive: true });
  if (!fs__default.default.existsSync(watchPaths.configPath)) {
    fs__default.default.writeFileSync(watchPaths.configPath, `${JSON.stringify(DEFAULT_CONFIG, null, 2)}
`, "utf8");
  }
  if (!fs__default.default.existsSync(watchPaths.historyPath)) {
    fs__default.default.writeFileSync(watchPaths.historyPath, `${JSON.stringify(DEFAULT_HISTORY, null, 2)}
`, "utf8");
  }
}
function loadWatchConfig(watchPaths) {
  try {
    const raw = fs__default.default.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__default.default.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__default.default.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));
}

exports.DEFAULT_CONCURRENCY = DEFAULT_CONCURRENCY;
exports.DEFAULT_POLL_INTERVAL_MS = DEFAULT_POLL_INTERVAL_MS;
exports.ensureWatchFiles = ensureWatchFiles;
exports.getWatchPaths = getWatchPaths;
exports.hasReviewedSameCommits = hasReviewedSameCommits;
exports.loadWatchConfig = loadWatchConfig;
exports.loadWatchHistory = loadWatchHistory;
exports.saveWatchHistory = saveWatchHistory;
exports.upsertReviewedPr = upsertReviewedPr;
