import _extends from "@babel/runtime/helpers/extends";
import React from 'react';
import { di } from 'react-magnetic-di';
import { withAnalyticsContext } from '@atlaskit/analytics-next';
import FeatureGates from '@atlaskit/feature-gate-js-client';
import AKLink from '@atlaskit/link';
import { withLinkClickedEvent } from '../../utils/analytics/click';
import { LinkAnalyticsContext } from '../../utils/analytics/LinkAnalyticsContext';
import Hyperlink from './Hyperlink';
import { HyperlinkWithSmartLinkResolver } from './HyperlinkResolver';
import LinkWarningModal from './LinkWarningModal';
import { useLinkWarningModal } from './LinkWarningModal/hooks/use-link-warning-modal';
const PACKAGE_DATA = {
  packageName: "@atlaskit/smart-card",
  packageVersion: "43.15.0",
  componentName: 'linkUrl'
};
const Anchor = withLinkClickedEvent('a');
export const LinkComponent = withLinkClickedEvent(AKLink);
const LinkUrl = ({
  href,
  children,
  checkSafety = true,
  onClick,
  testId = 'link-with-safety',
  isLinkComponent = false,
  enableResolve = false,
  ...props
}) => {
  const {
    isLinkSafe,
    showSafetyWarningModal,
    ...linkWarningModalProps
  } = useLinkWarningModal();
  const resolveHyperlinkFG = FeatureGates.checkGate('platform_editor_resolve_hyperlinks_killswitch');
  if (resolveHyperlinkFG) {
    const Link = enableResolve ? HyperlinkWithSmartLinkResolver : Hyperlink;
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(LinkAnalyticsContext, {
      url: href,
      display: "url"
    }, /*#__PURE__*/React.createElement(Link, _extends({
      href: href,
      isLinkComponent: isLinkComponent,
      onClick: e => {
        if (!checkSafety) {
          onClick && onClick(e);
          return;
        }

        // Only call the onClick if the link is safe
        if (isLinkSafe(e, href)) {
          onClick && onClick(e);
        } else {
          showSafetyWarningModal(e, href);
        }
      }
    }, props), children)), checkSafety && /*#__PURE__*/React.createElement(LinkWarningModal, linkWarningModalProps));
  }
  const Link = isLinkComponent ? LinkComponent : Anchor;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(LinkAnalyticsContext, {
    url: href,
    display: "url"
  }, /*#__PURE__*/React.createElement(Link, _extends({}, isLinkComponent ? {
    testId
  } : {
    'data-testid': testId
  }, {
    href: href || '',
    onClick: e => {
      if (!checkSafety) {
        onClick && onClick(e);
        return;
      }

      // Only call the onClick if the link is safe
      if (isLinkSafe(e, href)) {
        onClick && onClick(e);
      } else {
        showSafetyWarningModal(e, href);
      }
    }
  }, props), children)), checkSafety && /*#__PURE__*/React.createElement(LinkWarningModal, linkWarningModalProps));
};
export default withAnalyticsContext(PACKAGE_DATA)(LinkUrl);