"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useUpdateHypothesisMutation = void 0;

var _reactQuery = require("react-query");

var _hypothesis = require("../../api/services/hypothesis");

var _useIncidentQuery = require("../query-hooks/useIncidentQuery");

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var updateHypothesisInIncident = function updateHypothesisInIncident(incident, id, values) {
  return incident.map(function (i) {
    return _objectSpread(_objectSpread({}, i), {}, {
      hypothesis: i.hypothesis.map(function (h) {
        if (h.id === id) {
          return _objectSpread(_objectSpread({}, h), values);
        }

        return h;
      })
    });
  });
};

var useUpdateHypothesisMutation = function useUpdateHypothesisMutation() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var _options$incidentId = options.incidentId,
      incidentId = _options$incidentId === void 0 ? "incorrect_incident_id" : _options$incidentId;
  var queryClient = (0, _reactQuery.useQueryClient)();
  var incidentQueryKey = (0, _useIncidentQuery.createIncidentQueryKey)(incidentId);
  return (0, _reactQuery.useMutation)(function (_ref) {
    var id = _ref.id,
        params = _ref.params;
    return (0, _hypothesis.updateHypothesis)(id, params);
  }, {
    onMutate: function () {
      var _onMutate = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(data) {
        var previousIncident, newIncident;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                previousIncident = queryClient.getQueryData(incidentQueryKey);

                if (!(!previousIncident || !previousIncident[0])) {
                  _context.next = 3;
                  break;
                }

                return _context.abrupt("return", {
                  previousIncident: previousIncident
                });

              case 3:
                newIncident = updateHypothesisInIncident(previousIncident, data.id, data.params);
                queryClient.setQueryData(incidentQueryKey, newIncident);
                return _context.abrupt("return", {
                  previousIncident: previousIncident,
                  newIncident: newIncident
                });

              case 6:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      function onMutate(_x) {
        return _onMutate.apply(this, arguments);
      }

      return onMutate;
    }(),
    onSuccess: function onSuccess(resp, variables) {
      var previousIncident = queryClient.getQueryData(incidentQueryKey);

      if (previousIncident && previousIncident[0]) {
        queryClient.setQueryData(incidentQueryKey, updateHypothesisInIncident(previousIncident, variables.id, variables.params));
      }
    },
    onError: function onError(err, newData, context) {
      queryClient.setQueryData(incidentQueryKey, context.previousIncident);
      queryClient.invalidateQueries((0, _useIncidentQuery.createIncidentQueryKey)(incidentId));
    }
  });
};

exports.useUpdateHypothesisMutation = useUpdateHypothesisMutation;