"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TextWithHyperlinks = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const BASIC_HYPERLINKS_ONLY = /(http:\/\/|https:\/\/|ftp:\/\/|www\.)+[^\s]*/gi;
const WHITESPACE_CHARS = /(\s|\n|\r|\t)/g;
const TextWithHyperlinks = ({
  value,
  isMultiline = false,
  testId = 'text-with-hyperlinks',
  hyperlinksMatcher = BASIC_HYPERLINKS_ONLY
}) => {
  const textWithHyperlinks = (0, _react.useMemo)(() => {
    if (!value) {
      return;
    }
    const URLS = value.match(hyperlinksMatcher);
    const splitText = value.split(WHITESPACE_CHARS);
    return splitText.map((text, i) => {
      if (URLS?.includes(text)) {
        // if a link starts with "www." we need to append // before the href
        // otherwise it slaps link behind the parent domain
        // see https://datarobot.atlassian.net/browse/DM-4251
        const isWWWprefix = text.slice(0, 3).toLowerCase() === 'www';
        return /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
          href: isWWWprefix ? `//${text}` : text,
          rel: "noopener noreferrer",
          className: "anchor",
          target: "_blank",
          "test-id": `${text}-hyperlink`,
          children: text
        }, `${text}-${i}`);
      }
      return text;
    });
  }, [value]);
  if (!textWithHyperlinks) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
    "test-id": testId,
    className: (0, _classnames.default)('text-with-hyperlinks', {
      'is-multiline': isMultiline
    }),
    children: textWithHyperlinks
  });
};
exports.TextWithHyperlinks = TextWithHyperlinks;