import React, { useMemo } from 'react';
import classNames from 'classnames';
import { jsx as _jsx } from "react/jsx-runtime";
const BASIC_HYPERLINKS_ONLY = /(http:\/\/|https:\/\/|ftp:\/\/|www\.)+[^\s]*/gi;
const WHITESPACE_CHARS = /(\s|\n|\r|\t)/g;
export const TextWithHyperlinks = ({
  value,
  isMultiline = false,
  testId = 'text-with-hyperlinks',
  hyperlinksMatcher = BASIC_HYPERLINKS_ONLY
}) => {
  const textWithHyperlinks = 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__*/_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__*/_jsx("span", {
    "test-id": testId,
    className: classNames('text-with-hyperlinks', {
      'is-multiline': isMultiline
    }),
    children: textWithHyperlinks
  });
};