// ui/jira_chip.jsx — one vulnerability's Jira state, in the space of a table cell.
/* ---------------------------------------------------------------- jira */
/* One vulnerability's Jira state, in the space of a table cell.
   Linked   -> the issue key, opening the ticket in a new tab.
   Unlinked -> a "File issue" action, shown ONLY when the company has a working
               connection (ready). A button that always fails is worse than no
               button, and a customer who does not use Jira should never see it.
   `busy` is owned by the caller because the create is not optimistic: the chip must
   not claim a ticket until the tracker has actually issued one. */
function JiraChip({ link, ready, busy, onCreate, compact }) {
  if (link) {
    return (
      <a className={"jira-chip" + (compact ? " jira-chip-sm" : "")} href={link.issueUrl}
        target="_blank" rel="noopener noreferrer"
        title={`${link.issueKey} in Jira${link.createdByUsername ? " · filed by " + link.createdByUsername : ""}`}>
        <Icon name="link" size={compact ? 11 : 13} />
        <span className="jira-chip-key mono">{link.issueKey}</span>
        <Icon name="ext" size={compact ? 10 : 12} className="jira-chip-ext" />
      </a>
    );
  }
  if (!ready) return <span className="jira-chip is-none">—</span>;
  return (
    <button className={"jira-chip jira-chip-new" + (compact ? " jira-chip-sm" : "")} type="button"
      onClick={onCreate} disabled={busy} title="Create a Jira issue for this vulnerability">
      {busy ? <span className="btn-spin btn-spin-sm" /> : <Icon name="plus" size={compact ? 11 : 13} />}
      <span>{busy ? "Filing…" : "File issue"}</span>
    </button>
  );
}

Object.assign(window, { JiraChip });
