'use strict';

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

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 React = require('react');
var PropTypes = require('prop-types');
var _ = require('lodash');
var semver = require('semver');

var _require = require('hadron-react-components'),
    OptionSelector = _require.OptionSelector;

// const debug = require('debug')('mongodb-compass:bson-type-selector');

var BSON_TYPES = [{ name: 'Double', number: 1, alias: 'double' }, { name: 'Decimal128', number: 19, alias: 'decimal128' }, { name: 'String', number: 2, alias: 'string' }, { name: 'Object', number: 3, alias: 'object' },
// {name: 'Array', number: 4, alias: 'array'},  // COMPASS-275 server does not support this
{ name: 'Binary data', number: 5, alias: 'binData' }, { name: 'Undefined', number: 6, alias: 'undefined' }, { name: 'ObjectId', number: 7, alias: 'objectid' }, { name: 'Boolean', number: 8, alias: 'bool' }, { name: 'Date', number: 9, alias: 'date' }, { name: 'Null', number: 10, alias: 'null' }, { name: 'Regular Expression', number: 11, alias: 'regex' }, { name: 'DBPointer', number: 12, alias: 'dbPointer' }, { name: 'JavaScript', number: 13, alias: 'javascript' }, { name: 'Symbol', number: 14, alias: 'symbol' }, { name: 'JavaScript (with scope)', number: 15, alias: 'javascriptWithScope' }, { name: '32-bit integer', number: 16, alias: 'int' }, { name: 'Timestamp', number: 17, alias: 'timestamp' }, { name: '64-bit integer', number: 18, alias: 'long' }, { name: 'Min key', number: -1, alias: 'minKey' }, { name: 'Max key', number: 127, alias: 'maxKey' }];

var BSONTypeSelector = function (_React$Component) {
  _inherits(BSONTypeSelector, _React$Component);

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

    // try catch block in case semver typeerrors while remove decimal128
    // if server is < 3.4.x
    var _this = _possibleConstructorReturn(this, (BSONTypeSelector.__proto__ || Object.getPrototypeOf(BSONTypeSelector)).call(this, props));

    var canRemoveDecimal = false;
    try {
      canRemoveDecimal = semver.gt('3.4.0-rc0', _this.props.serverVersion);
    } catch (e) {
      canRemoveDecimal = true;
    }

    if (canRemoveDecimal) {
      _.remove(BSON_TYPES, function (type) {
        return type.number === 19;
      });
    }

    _this.state = {
      type: null
    };
    return _this;
  }

  _createClass(BSONTypeSelector, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      if (this.props.typeNumber) {
        this.setState({
          type: BSONTypeSelector.getTypeByNumber(this.props.typeNumber)
        });
      } else if (this.props.typeAlias) {
        this.setState({
          type: BSONTypeSelector.getTypeByAlias(this.props.typeAlias)
        });
      } else if (this.props.typeName) {
        this.setState({
          type: BSONTypeSelector.getTypeByName(this.props.typeName)
        });
      }
    }
  }, {
    key: 'onTypeClicked',
    value: function onTypeClicked(typeAlias, evt) {
      var type = BSONTypeSelector.getTypeByAlias(typeAlias);
      this.setState({
        type: type
      });
      this.props.onTypeClicked(type, evt);
    }
  }, {
    key: 'render',


    /**
     * Render BSONSelector component.
     *
     * @returns {React.Component} The view component.
     */
    value: function render() {
      var selectedTypeName = _.get(this.state.type, 'alias', '');
      var typeOptions = _.zipObject(_.map(BSON_TYPES, function (type) {
        return [type.alias, type.name];
      }));

      return React.createElement(OptionSelector, {
        id: 'bson-type-selector',
        options: typeOptions,
        label: 'BSON Type',
        title: typeOptions[selectedTypeName],
        onSelect: this.onTypeClicked.bind(this),
        disabled: this.props.isDisabled
      });
    }
  }], [{
    key: 'getTypeByName',
    value: function getTypeByName(typeName) {
      return _.find(BSON_TYPES, 'name', typeName);
    }
  }, {
    key: 'getTypeByNumber',
    value: function getTypeByNumber(typeNumber) {
      return _.find(BSON_TYPES, 'number', typeNumber);
    }
  }, {
    key: 'getTypeByAlias',
    value: function getTypeByAlias(typeAlias) {
      return _.find(BSON_TYPES, 'alias', typeAlias);
    }
  }]);

  return BSONTypeSelector;
}(React.Component);

BSONTypeSelector.propTypes = {
  typeNumber: PropTypes.number,
  typeAlias: PropTypes.string,
  typeName: PropTypes.string,
  onTypeClicked: PropTypes.func,
  serverVersion: PropTypes.string,
  isDisabled: PropTypes.bool
};

BSONTypeSelector.displayName = 'BSONTypeSelector';

module.exports = BSONTypeSelector;