import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback, useEffect } from 'react';
import { withErrorBoundary as withReactErrorBoundary } from 'react-error-boundary';
import { injectIntl } from 'react-intl';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
import { getFirstPartyIdentifier, 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 { isAuxClick } from '../../../utils/click-helpers';
import withIntlProvider from '../../common/intl-provider';
import { useFire3PWorkflowsClickEvent } from '../../SmartLinkEvents/useSmartLinkEvents';
import Hyperlink from '../Hyperlink';
const TRACK_NON_PRIMARY_3P_CLICKS_EXPERIMENT = 'linking_platform_track_non_primary_3p_clicks';
const HyperlinkFallbackComponent = () => null;
const withValidator = (Component, DefaultComponent) => props => {
  const 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);
};
const HyperlinkWithSmartLinkResolverInner = ({
  onClick: onClickCallback,
  ...props
}) => {
  const {
    state
  } = useResolveHyperlink({
    href: props.href || ''
  });
  const thirdPartyARI = getThirdPartyARI(state === null || state === void 0 ? void 0 : state.details);
  const firstPartyIdentifier = getFirstPartyIdentifier();
  const fire3PClickEvent = fg('platform_smartlink_3pclick_analytics') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useFire3PWorkflowsClickEvent(firstPartyIdentifier, thirdPartyARI) : undefined;

  // Shared scope guard for all 3P-click handlers.
  const shouldFire3PClickEvent = (state === null || state === void 0 ? void 0 : state.status) === 'resolved' && fire3PClickEvent && fg('platform_smartlink_3pclick_analytics');
  const onClick = useCallback(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 ? void 0 : fire3PClickEvent();
    }
    onClickCallback === null || onClickCallback === void 0 ? void 0 : onClickCallback(e);
  }, [onClickCallback, fire3PClickEvent, shouldFire3PClickEvent]);

  // Fire experiment exposure once per surface mount, not on every re-render.
  useEffect(() => {
    if (shouldFire3PClickEvent) {
      expValEquals(TRACK_NON_PRIMARY_3P_CLICKS_EXPERIMENT, 'isEnabled', true);
    }
  }, [shouldFire3PClickEvent]);
  const onAuxClick = useCallback(e => {
    // isAuxClick guards against Windows right-clicks firing onAuxClick with button === 2.
    if (isAuxClick(e) && shouldFire3PClickEvent && expValEqualsNoExposure(TRACK_NON_PRIMARY_3P_CLICKS_EXPERIMENT, 'isEnabled', true)) {
      fire3PClickEvent === null || fire3PClickEvent === void 0 ? void 0 : fire3PClickEvent({
        isAuxClick: true
      });
    }
  }, [fire3PClickEvent, shouldFire3PClickEvent]);
  const onContextMenu = useCallback(_e => {
    if (shouldFire3PClickEvent && expValEqualsNoExposure(TRACK_NON_PRIMARY_3P_CLICKS_EXPERIMENT, 'isEnabled', true)) {
      fire3PClickEvent === null || fire3PClickEvent === void 0 ? void 0 : fire3PClickEvent({
        isContextMenu: true
      });
    }
  }, [fire3PClickEvent, shouldFire3PClickEvent]);
  return /*#__PURE__*/React.createElement(Hyperlink, _extends({}, props, {
    onClick: onClick,
    onAuxClick: onAuxClick,
    onContextMenu: onContextMenu
  }));
};
export const HyperlinkWithSmartLinkResolver = withReactErrorBoundary(withValidator(injectIntl(withIntlProvider(HyperlinkWithSmartLinkResolverInner), {
  enforceContext: false
}), Hyperlink), {
  FallbackComponent: HyperlinkFallbackComponent
});