import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["action", "onClick", "onError"];
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { useCallback, useState } from 'react';
import { useAnalyticsEvents } from '../../../../../../common/analytics/generated/use-analytics-events';
import useInvoke from '../../../../../../state/hooks/use-invoke';
import { getInvokeFailureReason } from '../../../../../../state/hooks/use-invoke/utils';
import useResolve from '../../../../../../state/hooks/use-resolve';
import createInvokeRequest from '../../../../../../utils/actions/create-invoke-request';
import Action from '../index';
var ServerAction = function ServerAction(_ref) {
  var action = _ref.action,
    onClick = _ref.onClick,
    onErrorCallback = _ref.onError,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isLoading = _useState2[0],
    setIsLoading = _useState2[1];
  var invoke = useInvoke();
  var reload = useResolve();
  var _useAnalyticsEvents = useAnalyticsEvents(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var handleClick = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var _action$action, smartLinkActionType, request, _t;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!action) {
            _context.next = 5;
            break;
          }
          smartLinkActionType = (_action$action = action.action) === null || _action$action === void 0 ? void 0 : _action$action.actionType;
          _context.prev = 1;
          setIsLoading(true);
          if (smartLinkActionType === 'FollowEntityAction' || smartLinkActionType === 'UnfollowEntityAction') {
            fireEvent('ui.button.clicked.smartLinkFollowButton', {});
          } else {
            fireEvent("ui.button.clicked.".concat(smartLinkActionType), {});
          }
          fireEvent('track.smartLinkQuickAction.started', {
            smartLinkActionType: smartLinkActionType
          });
          request = createInvokeRequest(action);
          _context.next = 2;
          return invoke(request);
        case 2:
          fireEvent('track.smartLinkQuickAction.success', {
            smartLinkActionType: smartLinkActionType
          });
          if (!(action.reload && action.reload.url)) {
            _context.next = 3;
            break;
          }
          _context.next = 3;
          return reload({
            url: action.reload.url,
            isReloading: true,
            id: action.reload.id
          });
        case 3:
          setIsLoading(false);
          if (onClick) {
            onClick();
          }
          _context.next = 5;
          break;
        case 4:
          _context.prev = 4;
          _t = _context["catch"](1);
          setIsLoading(false);
          fireEvent('track.smartLinkQuickAction.failed', {
            smartLinkActionType: smartLinkActionType,
            reason: getInvokeFailureReason(_t)
          });
          onErrorCallback === null || onErrorCallback === void 0 || onErrorCallback();
        case 5:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[1, 4]]);
  })), [action, invoke, onClick, onErrorCallback, reload, fireEvent]);
  return /*#__PURE__*/React.createElement(Action, _extends({}, props, {
    isLoading: isLoading,
    onClick: handleClick
  }));
};
export default ServerAction;