import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["onClick", "onError", "testId", "status", "summariseUrl"];
import React, { useCallback } from 'react';
import { FormattedMessage } from 'react-intl-next';
import AiIcon from '@atlaskit/icon/core/atlassian-intelligence';
import { useAnalyticsEvents } from '../../../../../common/analytics/generated/use-analytics-events';
import { messages } from '../../../../../messages';
import Action from '../action';
import { getErrorMessage } from './utils';
export function AISummariseAction(_ref) {
  var onClickCallback = _ref.onClick,
    onErrorCallback = _ref.onError,
    testId = _ref.testId,
    status = _ref.status,
    summariseUrl = _ref.summariseUrl,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useAnalyticsEvents = useAnalyticsEvents(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var onCompleted = useCallback(function (state) {
    if ((state === null || state === void 0 ? void 0 : state.status) === 'error') {
      onErrorCallback === null || onErrorCallback === void 0 || onErrorCallback(getErrorMessage(state.error));
    }
  }, [onErrorCallback]);
  var handleActionClick = useCallback(function () {
    var _summariseUrl;
    fireEvent('ui.button.clicked.aiSummary', {});
    fireEvent('track.aiInteraction.initiated', {
      aiFeatureName: 'Smart Links Summary',
      proactiveAIGenerated: 0,
      userGeneratedAI: 1
    });
    (_summariseUrl = summariseUrl()) === null || _summariseUrl === void 0 || _summariseUrl.then(onCompleted, onCompleted);
    onClickCallback === null || onClickCallback === void 0 || onClickCallback();
  }, [fireEvent, onClickCallback, onCompleted, summariseUrl]);
  return /*#__PURE__*/React.createElement(Action, _extends({
    content: /*#__PURE__*/React.createElement(FormattedMessage, messages.ai_summary_action),
    icon: /*#__PURE__*/React.createElement(AiIcon, {
      spacing: "spacious",
      color: "currentColor",
      label: "Summarise with AI"
    }),
    onClick: handleActionClick,
    testId: "".concat(testId, "-summarise-action"),
    isLoading: status === 'loading',
    tooltipMessage: /*#__PURE__*/React.createElement(FormattedMessage, messages.ai_summary_action_description)
  }, props));
}