"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 _reactIntlNext = require("react-intl-next");
var _featureGateJsClient = _interopRequireDefault(require("@atlaskit/feature-gate-js-client"));
var _linkExtractors = require("@atlaskit/link-extractors");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
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 _intlProvider = _interopRequireDefault(require("../../common/intl-provider"));
var _useSmartLinkEvents = require("../../SmartLinkEvents/useSmartLinkEvents");
var _Hyperlink = _interopRequireDefault(require("../Hyperlink"));
var _unauthorizeView = _interopRequireDefault(require("./unauthorize-view"));
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 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 || ''
    }),
    actions = _useResolveHyperlink.actions,
    state = _useResolveHyperlink.state;
  var services = (0, _helpers.getServices)(state === null || state === void 0 ? void 0 : state.details);
  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;
  var onClick = (0, _react.useCallback)(function (e) {
    // Only fire the event if the feature flag is on and other conditions are met
    if ((state === null || state === void 0 ? void 0 : state.status) === 'resolved' && (e === null || e === void 0 ? void 0 : e.button) === 0 && fire3PClickEvent && (0, _platformFeatureFlags.fg)('platform_smartlink_3pclick_analytics')) {
      // 0 taken from button state representation -
      // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
      fire3PClickEvent();
    }
    onClickCallback === null || onClickCallback === void 0 || onClickCallback(e);
  }, [onClickCallback, fire3PClickEvent, state === null || state === void 0 ? void 0 : state.status]);
  var onAuthorize = (0, _react.useCallback)(function () {
    return actions.authorize('url');
  }, [actions]);
  var shouldRenderConnectBtn = function shouldRenderConnectBtn() {
    if (!props.children || !Array.isArray(props.children) || props.children.length === 0) {
      return false;
    }
    var firstChild = props.children[0];
    try {
      var _firstChild$props;
      // Check if first child has a string matching href
      if (typeof firstChild === 'string') {
        return props.href === firstChild;
      }

      // Check if first child has another child object containing matching href. This aligns with the behavior of the TextWrapper component used by editor to render link nodes
      if (firstChild !== null && firstChild !== void 0 && (_firstChild$props = firstChild.props) !== null && _firstChild$props !== void 0 && _firstChild$props.children && typeof firstChild.props.children === 'string') {
        return props.href === firstChild.props.children;
      }
    } catch (_) {
      return false;
    }
  };
  if ((state === null || state === void 0 ? void 0 : state.status) === 'unauthorized' && shouldRenderConnectBtn() && (_featureGateJsClient.default.getExperimentValue('platform_linking_bluelink_connect_confluence', 'isEnabled', false) || _featureGateJsClient.default.getExperimentValue('platform_linking_bluelink_connect_jira', 'isEnabled', false))) {
    var provider = (0, _linkExtractors.extractSmartLinkProvider)(state === null || state === void 0 ? void 0 : state.details);
    return /*#__PURE__*/_react.default.createElement(_unauthorizeView.default, (0, _extends2.default)({}, props, {
      onAuthorize: services !== null && services !== void 0 && services.length ? onAuthorize : undefined,
      onClick: onClick,
      showConnectBtn: (services === null || services === void 0 ? void 0 : services.length) > 0,
      provider: provider
    }));
  }
  return /*#__PURE__*/_react.default.createElement(_Hyperlink.default, (0, _extends2.default)({}, props, {
    onClick: onClick
  }));
};
var HyperlinkWithSmartLinkResolver = exports.HyperlinkWithSmartLinkResolver = (0, _reactErrorBoundary.withErrorBoundary)(withValidator((0, _reactIntlNext.injectIntl)((0, _intlProvider.default)(HyperlinkWithSmartLinkResolverInner), {
  enforceContext: false
}), _Hyperlink.default), {
  FallbackComponent: HyperlinkFallbackComponent
});