'use strict';

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

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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactRedux = require('react-redux');

var _reselect = require('reselect');

var _lodash = require('lodash');

var _reactBootstrap = require('react-bootstrap');

var _selectors = require('views/utils/selectors');

var _aaci = require('views/utils/aaci');

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

const { i18n } = window;
const __ = i18n.main.__.bind(i18n.main);

const __t = str => str.includes('/') ? str.split(' / ').map(i18n.resources.__).map(__).join(' / ') : __(i18n.resources.__(str));

function _ref([_equip, $equip, onslot] = []) {
  return !!_equip && !!$equip;
}

function _ref2([_equip, $equip, onslot]) {
  return _extends({}, $equip, _equip);
}

function _ref3([_ship = {}, $ship = {}] = [], _equips = []) {
  const ship = _extends({}, $ship, _ship);
  const equips = _equips.filter(_ref).map(_ref2);

  return (0, _aaci.getShipAACIs)(ship, equips);
}

const AACISelectorFactory = (0, _lodash.memoize)(shipId => (0, _reselect.createSelector)([(0, _selectors.shipDataSelectorFactory)(shipId), (0, _selectors.shipEquipDataSelectorFactory)(shipId)], _ref3));

function _ref4(id) {
  return _aaci.AACITable[id].fixed || 0;
}

function _ref5([_ship = {}, $ship = {}] = []) {
  const AACIs = (0, _aaci.getShipAllAACIs)(_extends({}, $ship, _ship));
  return Math.max(...AACIs.map(_ref4));
}

const maxAACIShotdownSelectorFactory = (0, _lodash.memoize)(shipId => (0, _reselect.createSelector)([(0, _selectors.shipDataSelectorFactory)(shipId)], _ref5));

function _ref6(id) {
  return _aaci.AACITable[id].fixed || 0;
}

function _ref7(id) {
  return _react2.default.createElement(
    'div',
    { className: 'info-tooltip-entry', key: id },
    _react2.default.createElement(
      'span',
      { className: 'info-tooltip-item' },
      __('Type %s', id),
      (0, _lodash.get)(_aaci.AACITable, `${id}.name.length`, 0) > 0 ? ` - ${__t(_aaci.AACITable[id].name)}` : ''
    ),
    _react2.default.createElement(
      'span',
      null,
      __('Shot down: %s', _aaci.AACITable[id].fixed)
    ),
    _react2.default.createElement(
      'span',
      { style: { marginLeft: '2ex' } },
      __('Modifier: %s', _aaci.AACITable[id].modifier)
    )
  );
}

const AACIIndicator = (0, _reactRedux.connect)((state, { shipId }) => ({
  AACIs: AACISelectorFactory(shipId)(state) || [],
  maxShotdown: maxAACIShotdownSelectorFactory(shipId)(state)
}))(({ AACIs, maxShotdown, shipId }) => {
  const currentMax = Math.max(...AACIs.map(_ref6));

  const tooltip = AACIs.length && _react2.default.createElement(
    'div',
    null,
    AACIs.map(_ref7),
    currentMax < maxShotdown && _react2.default.createElement(
      'span',
      null,
      __('Max shot down not reached')
    )
  );

  return AACIs.length ? _react2.default.createElement(
    'span',
    { className: 'ship-aaci' },
    _react2.default.createElement(
      _reactBootstrap.OverlayTrigger,
      { placement: 'top', overlay: _react2.default.createElement(
          _reactBootstrap.Tooltip,
          { className: 'info-tooltip', id: `aaci-info-${shipId}` },
          tooltip
        ) },
      _react2.default.createElement(
        _reactBootstrap.Label,
        { bsStyle: 'warning' },
        __('AACI')
      )
    )
  ) : _react2.default.createElement('noscript', null);
});

exports.default = AACIIndicator;
module.exports = exports['default'];