import { toConsumableArray as _toConsumableArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import React__default from 'react';

var formatLogMessage = function formatLogMessage(message) {
  return cutMessageToLines(message).reduce(groupLinesToPart, []).reduce(composeHeading, []).map(partToHtml);
};

var cutMessageToLines = function cutMessageToLines(message) {
  return message.split(/\r?\n/);
};

var groupLinesToPart = function groupLinesToPart(acc, item, idx) {
  if (!acc.length) {
    acc.push([]);
  } // we create a new part when there is an empty line or it's a second line
  // for cases when the is no visible parts in the cli message


  if (!item || idx === 1) {
    acc.push([]);
  }

  if (item) {
    acc[acc.length - 1].push(item);
  }

  return acc;
};

var composeHeading = function composeHeading(acc, item, idx) {
  // If the first line ends with ':' we assume the second line is a continuation of it
  var extendHeading = idx === 1 ? acc[0] && acc[0][0] && acc[0][0].endsWith(":") : false;

  if (extendHeading) {
    acc[0] = [].concat(_toConsumableArray(acc[0]), _toConsumableArray(item));
  } else {
    acc.push(item);
  }

  return acc;
};

var URL_REGEX = /(https?:\/\/([-a-zA-Z0-9@:%_+.~#?&//=]*))/;

var establishLinks = function establishLinks(item) {
  // make link active
  return item.replace(URL_REGEX, function (url) {
    return "<a href=\"".concat(url, "\" target=\"_blank\" rel=\"noopener noreferrer\">").concat(url, "</a>");
  });
};

var toInlineText = function toInlineText(item) {
  // add space at the end of string to separate phrase from the following one
  return "".concat(establishLinks(item), " ");
};

var ERROR_POINTERS_REGEX = /\^+/;
var LINE_PREFIX_REGEX = /^([\s|>]+\d*\s\|\s)|([\d|\s]+\|)/;

var toCodeLine = function toCodeLine(item) {
  var styledItem = item // wrap "^^^^^^^^^^" in <em> for special styling
  .replace(ERROR_POINTERS_REGEX, function (pointers) {
    return "<em>".concat(pointers, "</em>");
  }) // wrap line number prefix in <span> for special styling
  .replace(LINE_PREFIX_REGEX, function (linePrefix) {
    return "<span>".concat(linePrefix, "</span>");
  });
  return "".concat(styledItem, "\r\n");
}; // We assume that lines starting with '-' are list items


var LIST_MARKER_REGEX = /^-\s/;

var toListItem = function toListItem(item) {
  var cleandItem = item.replace(LIST_MARKER_REGEX, "");
  return "<li>".concat(establishLinks(cleandItem), "</li>");
};

var CODE_REGEX = /^([\s|>]+\d*\s\|\s)|([\d|\s]+\|)|GraphQL/;

var partToHtml = function partToHtml(part) {
  var isList = part.every(function (item) {
    return LIST_MARKER_REGEX.test(item);
  });

  if (isList) {
    return /*#__PURE__*/React__default.createElement("ul", {
      dangerouslySetInnerHTML: {
        __html: part.map(toListItem).join("")
      }
    });
  }

  var isCode = part.every(function (item) {
    return CODE_REGEX.test(item);
  });

  if (isCode) {
    return /*#__PURE__*/React__default.createElement("pre", {
      tabIndex: 0
    }, /*#__PURE__*/React__default.createElement("code", {
      dangerouslySetInnerHTML: {
        __html: part.map(toCodeLine).join("")
      }
    }));
  }

  return /*#__PURE__*/React__default.createElement("p", {
    dangerouslySetInnerHTML: {
      __html: part.map(toInlineText).join("")
    }
  });
};

export { formatLogMessage };
//# sourceMappingURL=utils.esm.js.map
