"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _useAnalyticsEvents2 = require("../../../../../../common/analytics/generated/use-analytics-events");
var _useInvoke = _interopRequireDefault(require("../../../../../../state/hooks/use-invoke"));
var _utils = require("../../../../../../state/hooks/use-invoke/utils");
var _useResolve = _interopRequireDefault(require("../../../../../../state/hooks/use-resolve"));
var _createInvokeRequest = _interopRequireDefault(require("../../../../../../utils/actions/create-invoke-request"));
var _index = _interopRequireDefault(require("../index"));
var _excluded = ["action", "onClick", "onError"];
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 _t2 in e) "default" !== _t2 && {}.hasOwnProperty.call(e, _t2) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t2)) && (i.get || i.set) ? o(f, _t2, i) : f[_t2] = e[_t2]); return f; })(e, t); }
var ServerAction = function ServerAction(_ref) {
  var action = _ref.action,
    onClick = _ref.onClick,
    onErrorCallback = _ref.onError,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    isLoading = _useState2[0],
    setIsLoading = _useState2[1];
  var invoke = (0, _useInvoke.default)();
  var reload = (0, _useResolve.default)();
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var handleClick = (0, _react.useCallback)( /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
    var _action$action, smartLinkActionType, request, _t;
    return _regenerator.default.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 = (0, _createInvokeRequest.default)(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(action.reload.url, true, undefined, 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: (0, _utils.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.default.createElement(_index.default, (0, _extends2.default)({}, props, {
    isLoading: isLoading,
    onClick: handleClick
  }));
};
var _default = exports.default = ServerAction;