"use strict";

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _LoadingScreen = require("./LoadingScreen");

var _LoadingScreen2 = _interopRequireDefault(_LoadingScreen);

var _IMOAPI = require("./IMOAPI");

var _IMOAPI2 = _interopRequireDefault(_IMOAPI);

var _Error = require("./Error");

var _Error2 = _interopRequireDefault(_Error);

var _SearchField = require("./SearchField");

var _SearchField2 = _interopRequireDefault(_SearchField);

var _Modifiers = require("./Modifiers");

var _Modifiers2 = _interopRequireDefault(_Modifiers);

var _SelectTable = require("./SelectTable");

var _SelectTable2 = _interopRequireDefault(_SelectTable);

var _reactBootstrapModal = require("react-bootstrap-modal");

var _reactBootstrapModal2 = _interopRequireDefault(_reactBootstrapModal);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var DEFAULT_STATE = {
  isLoading: false,
  isError: false,
  options: [],
  origOptions: [],
  modifiers: []
};

var IMOModal = function (_Component) {
  _inherits(IMOModal, _Component);

  function IMOModal(props) {
    _classCallCheck(this, IMOModal);

    var _this = _possibleConstructorReturn(this, (IMOModal.__proto__ || Object.getPrototypeOf(IMOModal)).call(this, props));

    _this.handleError = function () {
      _this.setState({ isError: true, isLoading: false });
    };

    _this.search = function (query, conf) {
      _this.setState({ isLoading: true });
      _this.api.search(query, conf).then(function (options) {
        return _this.setState({ options: options, isLoading: false });
      }).catch(_this.handleError);
    };

    _this.handleSearch = function (_ref) {
      var formData = _ref.formData;
      var problem = _this.props.uiSchema.imo.problem;

      _this.search(formData, problem);
    };

    _this.searchDetails = function (code, conf) {
      _this.setState({ isLoading: true });
      _this.api.searchDetails(code, conf).then(function (_ref2) {
        var options = _ref2.options,
            modifiers = _ref2.modifiers;

        _this.setState({
          options: options,
          origOptions: options,
          modifiers: modifiers,
          isLoading: false
        });
      }).catch(_this.handleError);
    };

    _this.handleRowClick = function (row) {
      if (row.selectable) {
        var detail = _this.props.uiSchema.imo.detail;

        _this.searchDetails(row.query, detail);
      } else {
        var changedRow = Object.assign(row);
        delete changedRow.selectable;
        delete changedRow.modifiers;
        delete changedRow.query;
        _this.props.onChange([row]);
        _this.setState(DEFAULT_STATE);
      }
    };

    _this.handleModifiersChange = function (activeModifiers) {
      var modifiers = Object.keys(activeModifiers).map(function (field) {
        return activeModifiers[field];
      }).filter(function (modifier) {
        return modifier !== _Modifiers.ANY_CODE;
      });
      var options = _this.state.origOptions.filter(function (option) {
        return modifiers.every(function (modifier) {
          return option.modifiers.includes(modifier);
        });
      });
      _this.setState({ options: options, activeModifiers: activeModifiers });
    };

    _this.modalBody = function () {
      var _this$state = _this.state,
          isError = _this$state.isError,
          isLoading = _this$state.isLoading,
          options = _this$state.options,
          modifiers = _this$state.modifiers,
          activeModifiers = _this$state.activeModifiers;


      if (isLoading) {
        return _react2.default.createElement(_LoadingScreen2.default, null);
      }

      if (isError) {
        return _react2.default.createElement(_Error2.default, null);
      }

      return _react2.default.createElement(
        "div",
        null,
        _react2.default.createElement(_SearchField2.default, { onSearch: _this.handleSearch }),
        modifiers.length > 0 ? _react2.default.createElement(_Modifiers2.default, {
          modifiers: modifiers,
          activeModifiers: activeModifiers,
          onChange: _this.handleModifiersChange
        }) : _react2.default.createElement("div", null),
        _react2.default.createElement(_SelectTable2.default, {
          data: options,
          schema: _this.props.schema,
          onRowClick: _this.handleRowClick
        })
      );
    };

    _this.api = new _IMOAPI2.default();
    _this.state = DEFAULT_STATE;
    return _this;
  }

  _createClass(IMOModal, [{
    key: "render",
    value: function render() {
      return _react2.default.createElement(
        "div",
        null,
        _react2.default.createElement(
          _reactBootstrapModal2.default.Header,
          null,
          _react2.default.createElement(
            _reactBootstrapModal2.default.Title,
            { id: "ModalHeader" },
            "IMO problem search"
          )
        ),
        _react2.default.createElement(
          _reactBootstrapModal2.default.Body,
          null,
          this.modalBody()
        )
      );
    }
  }]);

  return IMOModal;
}(_react.Component);

exports.default = IMOModal;