'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 AACISelectorFactory = (0, _lodash.memoize)(shipId => (0, _reselect.createSelector)([(0, _selectors.shipDataSelectorFactory)(shipId), (0, _selectors.shipEquipDataSelectorFactory)(shipId)], ([_ship = {}, $ship = {}] = [], _equips = []) => {
  const ship = _extends({}, $ship, _ship);
  const equips = _equips.filter(([_equip, $equip, onslot] = []) => !!_equip && !!$equip).map(([_equip, $equip, onslot]) => _extends({}, $equip, _equip));

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

const maxAACIShotdownSelectorFactory = (0, _lodash.memoize)(shipId => (0, _reselect.createSelector)([(0, _selectors.shipDataSelectorFactory)(shipId)], ([_ship = {}, $ship = {}] = []) => {
  const AACIs = (0, _aaci.getShipAllAACIs)(_extends({}, $ship, _ship));
  return Math.max(...AACIs.map(id => _aaci.AACITable[id].fixed || 0));
}));

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

  const tooltip = AACIs.length && _react2.default.createElement(
    'div',
    null,
    AACIs.map(id => _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 ? ` - ${__(i18n.resources.__(_aaci.AACITable[id].name))}` : ''
      ),
      _react2.default.createElement(
        'span',
        null,
        __('Shot down: %s', _aaci.AACITable[id].fixed)
      )
    )),
    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'];