'use strict';

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

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

var fs__default = /*#__PURE__*/_interopDefault(fs);

// src/features/watch-pr-review/infrastructure/gh.ts
var GH_EXEC_OPTIONS = {
  encoding: "utf8",
  maxBuffer: 1024 * 1024 * 50,
  stdio: ["ignore", "pipe", "pipe"]
};
function isGhAvailable() {
  try {
    child_process.execFileSync("gh", ["auth", "status"], GH_EXEC_OPTIONS);
    return true;
  } catch {
    return false;
  }
}
function getListOpenPullRequests(repo) {
  const stdout = child_process.execFileSync(
    "gh",
    [
      "pr",
      "list",
      "--repo",
      repo,
      "--state",
      "open",
      "--json",
      "number,title,url,author,baseRefName,headRefName"
    ],
    GH_EXEC_OPTIONS
  );
  const parsed = JSON.parse(stdout);
  return parsed.map((item) => ({
    author: item.author?.login ?? "(unknown)",
    baseRefName: item.baseRefName,
    headRefName: item.headRefName,
    number: item.number,
    title: item.title,
    url: item.url
  }));
}
function getPullRequestCommitOids(repo, prNumber) {
  const stdout = child_process.execFileSync(
    "gh",
    ["pr", "view", String(prNumber), "--repo", repo, "--json", "commits"],
    GH_EXEC_OPTIONS
  );
  const parsed = JSON.parse(stdout);
  return parsed.commits.map((commit) => commit.oid);
}
function getPullRequestComments(repo, prNumber) {
  const stdout = child_process.execFileSync(
    "gh",
    ["pr", "view", String(prNumber), "--repo", repo, "--json", "comments"],
    GH_EXEC_OPTIONS
  );
  const parsed = JSON.parse(stdout);
  return parsed.comments.map((comment) => comment.body);
}
function getPullRequestChangedFiles(repo, prNumber) {
  const stdout = child_process.execFileSync(
    "gh",
    ["pr", "view", String(prNumber), "--repo", repo, "--json", "files"],
    GH_EXEC_OPTIONS
  );
  const parsed = JSON.parse(stdout);
  return parsed.files.map((file) => file.path);
}
function writePullRequestDiff(repo, prNumber, outputPath) {
  const fileDescriptor = fs__default.default.openSync(outputPath, "w");
  try {
    child_process.execFileSync("gh", ["pr", "diff", String(prNumber), "--repo", repo], {
      maxBuffer: 1024 * 1024 * 50,
      stdio: ["ignore", fileDescriptor, "pipe"]
    });
  } finally {
    fs__default.default.closeSync(fileDescriptor);
  }
  return fs__default.default.statSync(outputPath).size;
}
function createPullRequestComment(repo, prNumber, bodyFilePath) {
  child_process.execFileSync(
    "gh",
    ["pr", "comment", String(prNumber), "--repo", repo, "--body-file", bodyFilePath],
    GH_EXEC_OPTIONS
  );
}

exports.createPullRequestComment = createPullRequestComment;
exports.getListOpenPullRequests = getListOpenPullRequests;
exports.getPullRequestChangedFiles = getPullRequestChangedFiles;
exports.getPullRequestComments = getPullRequestComments;
exports.getPullRequestCommitOids = getPullRequestCommitOids;
exports.isGhAvailable = isGhAvailable;
exports.writePullRequestDiff = writePullRequestDiff;
