import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["onClick"];
import React, { useCallback } from 'react';
import { withErrorBoundary as withReactErrorBoundary } from 'react-error-boundary';
import { injectIntl } from 'react-intl-next';
import FeatureGates from '@atlaskit/feature-gate-js-client';
import { extractSmartLinkProvider } from '@atlaskit/link-extractors';
import { fg } from '@atlaskit/platform-feature-flags';
import { getFirstPartyIdentifier, getServices, getThirdPartyARI } from '../../../state/helpers';
import useResolveHyperlink from '../../../state/hooks/use-resolve-hyperlink';
import useResolveHyperlinkValidator from '../../../state/hooks/use-resolve-hyperlink/useResolveHyperlinkValidator';
import { SmartLinkAnalyticsContext } from '../../../utils/analytics/SmartLinkAnalyticsContext';
import withIntlProvider from '../../common/intl-provider';
import { useFire3PWorkflowsClickEvent } from '../../SmartLinkEvents/useSmartLinkEvents';
import Hyperlink from '../Hyperlink';
import HyperlinkUnauthorizedView from './unauthorize-view';
var HyperlinkFallbackComponent = function HyperlinkFallbackComponent() {
  return null;
};
var withValidator = function withValidator(Component, DefaultComponent) {
  return function (props) {
    var shouldResolveHyperlink = useResolveHyperlinkValidator(props === null || props === void 0 ? void 0 : props.href);
    return shouldResolveHyperlink && props.href ? /*#__PURE__*/React.createElement(SmartLinkAnalyticsContext, {
      url: props.href,
      display: "url"
    }, /*#__PURE__*/React.createElement(Component, props)) : /*#__PURE__*/React.createElement(DefaultComponent, props);
  };
};
var HyperlinkWithSmartLinkResolverInner = function HyperlinkWithSmartLinkResolverInner(_ref) {
  var onClickCallback = _ref.onClick,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useResolveHyperlink = useResolveHyperlink({
      href: props.href || ''
    }),
    actions = _useResolveHyperlink.actions,
    state = _useResolveHyperlink.state;
  var services = getServices(state === null || state === void 0 ? void 0 : state.details);
  var thirdPartyARI = getThirdPartyARI(state === null || state === void 0 ? void 0 : state.details);
  var firstPartyIdentifier = getFirstPartyIdentifier();
  var fire3PClickEvent = fg('platform_smartlink_3pclick_analytics') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useFire3PWorkflowsClickEvent(firstPartyIdentifier, thirdPartyARI) : undefined;
  var onClick = 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 && 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 = 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() && (FeatureGates.getExperimentValue('platform_linking_bluelink_connect_confluence', 'isEnabled', false) || FeatureGates.getExperimentValue('platform_linking_bluelink_connect_jira', 'isEnabled', false))) {
    var provider = extractSmartLinkProvider(state === null || state === void 0 ? void 0 : state.details);
    return /*#__PURE__*/React.createElement(HyperlinkUnauthorizedView, _extends({}, 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.createElement(Hyperlink, _extends({}, props, {
    onClick: onClick
  }));
};
export var HyperlinkWithSmartLinkResolver = withReactErrorBoundary(withValidator(injectIntl(withIntlProvider(HyperlinkWithSmartLinkResolverInner), {
  enforceContext: false
}), Hyperlink), {
  FallbackComponent: HyperlinkFallbackComponent
});