import axios from 'axios';
import React from 'react';

export const onIssuesRefreshClick = (e: React.MouseEvent) => {
  if (e.target !== e.currentTarget) return;
  e.stopPropagation();
  const rawInput = prompt(
    'Force issues refresh (delimiters - space, comma, semicolon or tab)',
    '',
  );
  if (!rawInput) return;
  const list = rawInput
    .split(/[ ,;\t\n\r]/)
    .map((item) => Number(item))
    .filter((item) => Number.isFinite(item) && item > 0);
  if (!list) return;
  axios.post(`/redmine-event-emitter/append-issues`, list);
};

export const onGetIssuesQueueSizeClick = async (
  e: React.MouseEvent,
): Promise<void> => {
  if (e.target !== e.currentTarget) return;
  e.stopPropagation();
  const resp = await axios.get(
    `${process.env.REACT_APP_BACKEND}redmine-event-emitter/get-issues-queue-size`,
  );
  console.debug(`resp -`, resp); // DEBUG
  if (!resp || typeof resp.data !== 'number') return;
  alert(`Issues queue size - ${resp.data}`);
};
