import { EventEmitter } from "events";
class PRTracker extends EventEmitter {
  list;
  prs;
  issues;
  selectedItem = null;
  viewMode = "prs";
  constructor(list) {
    super();
    this.list = list;
    this.prs = /* @__PURE__ */ new Map();
    this.issues = /* @__PURE__ */ new Map();
    this.initializeUI();
  }
  initializeUI() {
    this.list.on("select", (item, index) => {
      if (this.viewMode === "prs") {
        const prId = Array.from(this.prs.keys())[index];
        this.selectPR(prId);
      } else {
        const issueId = Array.from(this.issues.keys())[index];
        this.selectIssue(issueId);
      }
    });
    this.list.key(["tab"], () => {
      this.toggleView();
    });
  }
  formatPRItem(pr) {
    const status = this.getPRStatusIcon(pr);
    const checks = this.getChecksStatus(pr.checks);
    const reviews = this.getReviewStatus(pr.reviews);
    let item = `${status} #${pr.number}: ${pr.title}
`;
    item += `   {gray-fg}@${pr.author.login} | ${checks} | ${reviews} | +${pr.additions}/-${pr.deletions}{/}`;
    if (pr.linkedIssues?.length) {
      item += ` | {cyan-fg}\u{1F517}${pr.linkedIssues.length}{/}`;
    }
    return item;
  }
  formatIssueItem(issue) {
    const status = issue.state === "open" ? "{green-fg}\u25CF{/}" : "{gray-fg}\u25CF{/}";
    const assignees = issue.assignees?.map((a) => `@${a}`).join(", ") || "unassigned";
    let item = `${status} #${issue.number}: ${issue.title}
`;
    item += `   {gray-fg}@${issue.author.login} | ${assignees} | \u{1F4AC}${issue.comments}{/}`;
    return item;
  }
  getPRStatusIcon(pr) {
    if (pr.state === "merged") return "{magenta-fg}\u2B24{/}";
    if (pr.state === "closed") return "{red-fg}\u2B24{/}";
    if (pr.draft) return "{gray-fg}\u25CB{/}";
    return "{green-fg}\u25CF{/}";
  }
  getChecksStatus(checks) {
    if (!checks) return "{gray-fg}no checks{/}";
    if (checks.failed > 0)
      return `{red-fg}\u2717 ${checks.failed}/${checks.total}{/}`;
    if (checks.pending > 0)
      return `{yellow-fg}\u23F3 ${checks.pending}/${checks.total}{/}`;
    return `{green-fg}\u2713 ${checks.passed}/${checks.total}{/}`;
  }
  getReviewStatus(reviews) {
    const approved = reviews.filter((r) => r.state === "approved").length;
    const changes = reviews.filter(
      (r) => r.state === "changes_requested"
    ).length;
    if (changes > 0) return `{red-fg}\u{1F44E}${changes}{/}`;
    if (approved > 0) return `{green-fg}\u{1F44D}${approved}{/}`;
    return "{gray-fg}no reviews{/}";
  }
  update(data) {
    if (data.prs) {
      this.prs.clear();
      data.prs.forEach((pr) => this.prs.set(pr.id, pr));
    }
    if (data.issues) {
      this.issues.clear();
      data.issues.forEach((issue) => this.issues.set(issue.id, issue));
    }
    this.refreshDisplay();
  }
  refreshDisplay() {
    let items = [];
    let label = "";
    if (this.viewMode === "prs") {
      items = Array.from(this.prs.values()).map(
        (pr) => this.formatPRItem(pr)
      );
      const open = Array.from(this.prs.values()).filter(
        (pr) => pr.state === "open"
      ).length;
      label = ` \u{1F500} Pull Requests (${open}/${this.prs.size}) [Tab] Issues `;
    } else {
      items = Array.from(this.issues.values()).map(
        (issue) => this.formatIssueItem(issue)
      );
      const open = Array.from(this.issues.values()).filter(
        (i) => i.state === "open"
      ).length;
      label = ` \u{1F41B} Issues (${open}/${this.issues.size}) [Tab] PRs `;
    }
    this.list.setItems(items);
    if (this.list.parent && typeof this.list.parent.setLabel === "function") {
      this.list.parent.setLabel(label);
    }
    this.list.screen.render();
  }
  toggleView() {
    this.viewMode = this.viewMode === "prs" ? "issues" : "prs";
    this.refreshDisplay();
  }
  selectPR(prId) {
    const pr = this.prs.get(prId);
    if (pr) {
      this.emit("pr:selected", pr);
    }
  }
  selectIssue(issueId) {
    const issue = this.issues.get(issueId);
    if (issue) {
      this.emit("issue:selected", issue);
    }
  }
  focus() {
    this.list.focus();
  }
  hasFocus() {
    return this.list === this.list.screen.focused;
  }
}
export {
  PRTracker
};
//# sourceMappingURL=pr-tracker.js.map
