import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["href", "children", "checkSafety", "onClick", "testId", "isLinkComponent", "enableResolve"],
  _excluded2 = ["isLinkSafe", "showSafetyWarningModal"];
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';
var PACKAGE_DATA = {
  packageName: "@atlaskit/smart-card",
  packageVersion: "43.20.1",
  componentName: 'linkUrl'
};
var Anchor = withLinkClickedEvent('a');
export var LinkComponent = withLinkClickedEvent(AKLink);
var LinkUrl = function LinkUrl(_ref) {
  var href = _ref.href,
    children = _ref.children,
    _ref$checkSafety = _ref.checkSafety,
    checkSafety = _ref$checkSafety === void 0 ? true : _ref$checkSafety,
    _onClick = _ref.onClick,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'link-with-safety' : _ref$testId,
    _ref$isLinkComponent = _ref.isLinkComponent,
    isLinkComponent = _ref$isLinkComponent === void 0 ? false : _ref$isLinkComponent,
    _ref$enableResolve = _ref.enableResolve,
    enableResolve = _ref$enableResolve === void 0 ? false : _ref$enableResolve,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useLinkWarningModal = useLinkWarningModal(),
    isLinkSafe = _useLinkWarningModal.isLinkSafe,
    showSafetyWarningModal = _useLinkWarningModal.showSafetyWarningModal,
    linkWarningModalProps = _objectWithoutProperties(_useLinkWarningModal, _excluded2);
  var resolveHyperlinkFG = FeatureGates.checkGate('platform_editor_resolve_hyperlinks_killswitch');
  if (resolveHyperlinkFG) {
    var _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: function 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));
  }
  var 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: testId
  } : {
    'data-testid': testId
  }, {
    href: href || '',
    onClick: function 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);