"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.HyperlinkWithSmartLinkResolver = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _reactErrorBoundary = require("react-error-boundary");
var _reactIntl = require("react-intl");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _expValEqualsNoExposure = require("@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure");
var _helpers = require("../../../state/helpers");
var _useResolveHyperlink2 = _interopRequireDefault(require("../../../state/hooks/use-resolve-hyperlink"));
var _useResolveHyperlinkValidator = _interopRequireDefault(require("../../../state/hooks/use-resolve-hyperlink/useResolveHyperlinkValidator"));
var _SmartLinkAnalyticsContext = require("../../../utils/analytics/SmartLinkAnalyticsContext");
var _clickHelpers = require("../../../utils/click-helpers");
var _intlProvider = _interopRequireDefault(require("../../common/intl-provider"));
var _useSmartLinkEvents = require("../../SmartLinkEvents/useSmartLinkEvents");
var _Hyperlink = _interopRequireDefault(require("../Hyperlink"));
var _excluded = ["onClick"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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); }
var TRACK_NON_PRIMARY_3P_CLICKS_EXPERIMENT = 'linking_platform_track_non_primary_3p_clicks';
var HyperlinkFallbackComponent = function HyperlinkFallbackComponent() {
  return null;
};
var withValidator = function withValidator(Component, DefaultComponent) {
  return function (props) {
    var shouldResolveHyperlink = (0, _useResolveHyperlinkValidator.default)(props === null || props === void 0 ? void 0 : props.href);
    return shouldResolveHyperlink && props.href ? /*#__PURE__*/_react.default.createElement(_SmartLinkAnalyticsContext.SmartLinkAnalyticsContext, {
      url: props.href,
      display: "url"
    }, /*#__PURE__*/_react.default.createElement(Component, props)) : /*#__PURE__*/_react.default.createElement(DefaultComponent, props);
  };
};
var HyperlinkWithSmartLinkResolverInner = function HyperlinkWithSmartLinkResolverInner(_ref) {
  var onClickCallback = _ref.onClick,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _useResolveHyperlink = (0, _useResolveHyperlink2.default)({
      href: props.href || ''
    }),
    state = _useResolveHyperlink.state;
  var thirdPartyARI = (0, _helpers.getThirdPartyARI)(state === null || state === void 0 ? void 0 : state.details);
  var firstPartyIdentifier = (0, _helpers.getFirstPartyIdentifier)();
  var fire3PClickEvent = (0, _platformFeatureFlags.fg)('platform_smartlink_3pclick_analytics') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  (0, _useSmartLinkEvents.useFire3PWorkflowsClickEvent)(firstPartyIdentifier, thirdPartyARI) : undefined;

  // Shared scope guard for all 3P-click handlers.
  var shouldFire3PClickEvent = (state === null || state === void 0 ? void 0 : state.status) === 'resolved' && fire3PClickEvent && (0, _platformFeatureFlags.fg)('platform_smartlink_3pclick_analytics');
  var onClick = (0, _react.useCallback)(function (e) {
    // button === 0 is left-click, see
    // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
    if (shouldFire3PClickEvent && (e === null || e === void 0 ? void 0 : e.button) === 0) {
      fire3PClickEvent === null || fire3PClickEvent === void 0 || fire3PClickEvent();
    }
    onClickCallback === null || onClickCallback === void 0 || onClickCallback(e);
  }, [onClickCallback, fire3PClickEvent, shouldFire3PClickEvent]);

  // Fire experiment exposure once per surface mount, not on every re-render.
  (0, _react.useEffect)(function () {
    if (shouldFire3PClickEvent) {
      (0, _expValEquals.expValEquals)(TRACK_NON_PRIMARY_3P_CLICKS_EXPERIMENT, 'isEnabled', true);
    }
  }, [shouldFire3PClickEvent]);
  var onAuxClick = (0, _react.useCallback)(function (e) {
    // isAuxClick guards against Windows right-clicks firing onAuxClick with button === 2.
    if ((0, _clickHelpers.isAuxClick)(e) && shouldFire3PClickEvent && (0, _expValEqualsNoExposure.expValEqualsNoExposure)(TRACK_NON_PRIMARY_3P_CLICKS_EXPERIMENT, 'isEnabled', true)) {
      fire3PClickEvent === null || fire3PClickEvent === void 0 || fire3PClickEvent({
        isAuxClick: true
      });
    }
  }, [fire3PClickEvent, shouldFire3PClickEvent]);
  var onContextMenu = (0, _react.useCallback)(function (_e) {
    if (shouldFire3PClickEvent && (0, _expValEqualsNoExposure.expValEqualsNoExposure)(TRACK_NON_PRIMARY_3P_CLICKS_EXPERIMENT, 'isEnabled', true)) {
      fire3PClickEvent === null || fire3PClickEvent === void 0 || fire3PClickEvent({
        isContextMenu: true
      });
    }
  }, [fire3PClickEvent, shouldFire3PClickEvent]);
  return /*#__PURE__*/_react.default.createElement(_Hyperlink.default, (0, _extends2.default)({}, props, {
    onClick: onClick,
    onAuxClick: onAuxClick,
    onContextMenu: onContextMenu
  }));
};
var HyperlinkWithSmartLinkResolver = exports.HyperlinkWithSmartLinkResolver = (0, _reactErrorBoundary.withErrorBoundary)(withValidator((0, _reactIntl.injectIntl)((0, _intlProvider.default)(HyperlinkWithSmartLinkResolverInner), {
  enforceContext: false
}), _Hyperlink.default), {
  FallbackComponent: HyperlinkFallbackComponent
});