'use strict';

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

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _reactFontawesome = require('react-fontawesome');

var _reactFontawesome2 = _interopRequireDefault(_reactFontawesome);

var _lodash = require('lodash');

require('./assets/landbase-button.css');

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

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

const fatiguedLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'danger', className: 'airbase-state-label' },
  __('Fatigued')
);
const emptyLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'warning', className: 'airbase-state-label' },
  __('Empty slot')
);
const relocateLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'warning', className: 'airbase-state-label' },
  __('Relocating')
);
const resupplyLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'warning', className: 'airbase-state-label' },
  __('Resupply needed')
);
const noActionLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'warning', className: 'airbase-state-label' },
  __('No action')
);
const readyLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'success', className: 'airbase-state-label' },
  __('Ready')
);

function _ref(a) {
  return a.api_area_id;
}

function _ref2(a, b) {
  return a - b;
}

function _ref3(a, i, arr) {
  return a != arr[i - 1];
}

function _ref5(s) {
  return s.api_count !== s.api_max_count;
}

function _ref6(a, b) {
  return a || b;
}

function _ref7(a) {
  return a.api_plane_info.map(_ref5).reduce(_ref6, false);
}

function _ref8(a, b) {
  return a || b;
}

function _ref10(s) {
  return s.api_state;
}

function _ref11(a, b) {
  return a * b;
}

function _ref12(a) {
  return a.api_plane_info.map(_ref10).reduce(_ref11, 1);
}

function _ref13(a, b) {
  return a * b;
}

function _ref15(s) {
  return s.api_cond || 1;
}

function _ref16(a, b) {
  return a * b;
}

function _ref17(a) {
  return a.api_plane_info.map(_ref15).reduce(_ref16, 1);
}

function _ref18(a, b) {
  return a * b;
}

function _ref20(a) {
  return a.api_action_kind !== 1 && a.api_action_kind !== 2;
}

function _ref21(a, b) {
  return a || b;
}

function _ref23(s) {
  return s.api_state === 0;
}

function _ref24(a, b) {
  return a && b;
}

function _ref25(a) {
  return a.api_plane_info.map(_ref23).reduce(_ref24, true);
}

function _ref26(a, b) {
  return a && b;
}

function _ref27(a) {
  return !a.allEmpty;
}

function _ref28(a) {
  return a.needSupply;
}

function _ref29(a, b) {
  return a || b;
}

function _ref30(a) {
  return !a.allEmpty;
}

function _ref31(a) {
  return a.squardState;
}

function _ref32(a, b) {
  return a * b;
}

function _ref33(a) {
  return !a.allEmpty;
}

function _ref34(a) {
  return a.squardCond;
}

function _ref35(a, b) {
  return a * b;
}

function _ref36(a) {
  return !a.allEmpty;
}

function _ref37(a) {
  return a.noAction;
}

function _ref38(a, b) {
  return a || b;
}

function _ref39(a) {
  return !a.allEmpty;
}

function _ref40(a, b) {
  return a || b;
}

const LandbaseButton = exports.LandbaseButton = (0, _reactRedux.connect)(state => ({
  sortieStatus: (0, _lodash.get)(state, 'sortie.sortieStatus', []),
  airbase: (0, _lodash.get)(state, 'info.airbase', []),
  mapareas: (0, _lodash.get)(state, 'const.$mapareas', {})
}))(({ fleetId, activeFleetId, onClick, disabled, airbase, sortieStatus, mapareas, isMini }) => {
  function _ref4(a) {
    return mapareas[a.api_area_id];
  }

  function _ref9(a) {
    return mapareas[a.api_area_id];
  }

  function _ref14(a) {
    return mapareas[a.api_area_id];
  }

  function _ref19(a) {
    return mapareas[a.api_area_id];
  }

  function _ref22(a) {
    return mapareas[a.api_area_id];
  }

  const airbaseProps = airbase.map(_ref).filter(a => mapareas[a]).sort(_ref2).filter(_ref3).map(i => ({
    mapId: i,
    needSupply: airbase.filter(_ref4).filter(a => a.api_area_id === i).map(_ref7).reduce(_ref8, false),
    // 0: 未配属あり, 1: 配属済み, >1: 配置転換中あり
    squardState: airbase.filter(_ref9).filter(a => a.api_area_id === i).map(_ref12).reduce(_ref13, 1),
    // 1: 通常, >1: 黄疲労・赤疲労あり
    squardCond: airbase.filter(_ref14).filter(a => a.api_area_id === i).map(_ref17).reduce(_ref18, 1),
    noAction: airbase.filter(_ref19).filter(a => a.api_area_id === i).map(_ref20).reduce(_ref21, false),
    allEmpty: airbase.filter(_ref22).filter(a => a.api_area_id === i).map(_ref25).reduce(_ref26, true)
  }));
  const needSupply = airbaseProps.filter(_ref27).map(_ref28).reduce(_ref29, false);
  const squardState = airbaseProps.filter(_ref30).map(_ref31).reduce(_ref32, 1);
  const squardCond = airbaseProps.filter(_ref33).map(_ref34).reduce(_ref35, 1);
  const noAction = airbaseProps.filter(_ref36).map(_ref37).reduce(_ref38, false);
  const sortie = sortieStatus.filter(_ref39).reduce(_ref40, false);
  const bsStyle = (() => {
    if (sortie) {
      return 'default';
    } else if (squardCond > 1) {
      return 'danger';
    } else if (squardState !== 1 || needSupply) {
      return 'warning';
    } else if (noAction) {
      return 'info';
    } else {
      return 'success';
    }
  })();
  const propTooltip = _react2.default.createElement(
    _reactBootstrap.Tooltip,
    { id: isMini ? 'airbase-tooltip-mini' : 'airbase-tooltip' },
    airbaseProps.map((airbase, i) => {
      const { mapId, needSupply, squardState, squardCond, noAction } = airbase;
      return _react2.default.createElement(
        'div',
        { key: i },
        _react2.default.createElement(
          'div',
          null,
          '[',
          mapId,
          '] ',
          window.i18n.resources.__((mapareas[mapId] || {}).api_name)
        ),
        squardCond > 1 && fatiguedLabel,
        squardState < 1 && emptyLabel,
        squardState > 1 && relocateLabel,
        needSupply && resupplyLabel,
        noAction && noActionLabel,
        squardCond === 1 && squardState === 1 && !needSupply && !noAction && readyLabel
      );
    })
  );
  return _react2.default.createElement(
    _reactBootstrap.OverlayTrigger,
    { placement: 'bottom', overlay: propTooltip },
    _react2.default.createElement(
      _reactBootstrap.Button,
      {
        bsSize: isMini ? 'xsmall' : 'small',
        bsStyle: bsStyle,
        onClick: onClick,
        disabled: disabled,
        className: fleetId == activeFleetId ? 'active' : ''
      },
      _react2.default.createElement(_reactFontawesome2.default, { name: 'plane' })
    )
  );
});