'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.dockingCompleteObserver = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

exports.reducer = reducer;

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _tools = require('views/utils/tools');

var _reduxObservers = require('redux-observers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// Preserved fields: api_id, api_member_id
const emptyRepair = {
  api_complete_time: 0,
  api_complete_time_str: '0',
  api_item1: 0,
  api_item2: 0,
  api_item3: 0,
  api_item4: 0,
  api_ship_id: 0,
  api_state: 0
};

function reducer(state = [], { type, body, postBody }) {
  switch (type) {
    case '@@Response/kcsapi/api_get_member/ndock':
      return (0, _tools.compareUpdate)(state, body);
    case '@@Response/kcsapi/api_port/port':
      return (0, _tools.compareUpdate)(state, body.api_ndock);
    case '@@Response/kcsapi/api_req_nyukyo/speedchange':
      {
        const { api_ndock_id } = postBody;
        state = state.slice();
        state[api_ndock_id - 1] = _extends({}, state[api_ndock_id - 1], emptyRepair);
        state;
      }
  }
  return state;
}

// observe docking complete events and modify ship HP accordingly.
const dockingCompleteObserver = exports.dockingCompleteObserver = (0, _reduxObservers.observer)(state => state.info.repairs, (dispatch, current, previous) => {
  /*
     only observe valid state changes:
     - the state should be available before and after
     - no length change allowed
   */
  if (!Array.isArray(current) || !Array.isArray(previous) || current.length !== previous.length) {
    return;
  }

  current.map((repairDataCur, ind) => {
    const repairDataPrev = previous[ind];
    const rstId = repairDataPrev.api_ship_id;

    if (
    // roster id is valid
    _lodash2.default.isInteger(rstId) && rstId > 0 &&
    // sanity check: now current position should be empty
    repairDataCur.api_ship_id === 0 &&
    // state transition: docking complete
    repairDataPrev.api_state === 1 && repairDataCur.api_state === 0) {
      dispatch({
        type: '@@info.ships@RepairCompleted',
        body: { api_ship_id: rstId }
      });
    }
  });
});