'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 _parseAuthoritySpec = require('../helpers/parseAuthoritySpec');

var _parseAuthoritySpec2 = _interopRequireDefault(_parseAuthoritySpec);

var _QuickAdd = require('../../styles/cspace-input/QuickAdd.css');

var _QuickAdd2 = _interopRequireDefault(_QuickAdd);

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 propTypes = {
  add: _react.PropTypes.func,
  authority: _react.PropTypes.string,
  displayName: _react.PropTypes.string,
  formatVocabName: _react.PropTypes.func,
  recordTypes: _react.PropTypes.object
};

var defaultProps = {
  formatVocabName: function formatVocabName(vocab) {
    return vocab.messages.collectionName.defaultMessage;
  }
};

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

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

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

    _this.handleButtonClick = _this.handleButtonClick.bind(_this);
    return _this;
  }

  _createClass(QuickAdd, [{
    key: 'handleButtonClick',
    value: function handleButtonClick(event) {
      event.preventDefault();

      var _props = this.props,
          add = _props.add,
          displayName = _props.displayName;


      if (add) {
        var _event$currentTarget$ = event.currentTarget.dataset,
            authorityName = _event$currentTarget$.authorityname,
            vocabularyName = _event$currentTarget$.vocabularyname;


        add(authorityName, vocabularyName, displayName);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props2 = this.props,
          authority = _props2.authority,
          displayName = _props2.displayName,
          formatVocabName = _props2.formatVocabName,
          recordTypes = _props2.recordTypes;


      var authorities = (0, _parseAuthoritySpec2.default)(authority);

      var buttons = authorities.map(function (authoritySpec) {
        var authorityName = authoritySpec.authorityName,
            vocabularyName = authoritySpec.vocabularyName;


        var vocabularyConfig = recordTypes[authorityName].vocabularies[vocabularyName];

        if (!vocabularyConfig) {
          return null;
        }

        return _react2.default.createElement(
          'li',
          { key: authorityName + '/' + vocabularyName },
          _react2.default.createElement(
            'button',
            {
              'data-authorityname': authorityName,
              'data-vocabularyname': vocabularyName,
              onClick: _this2.handleButtonClick
            },
            formatVocabName(vocabularyConfig)
          )
        );
      });

      // TODO: i18n of add message

      return _react2.default.createElement(
        'div',
        { className: _QuickAdd2.default.common },
        _react2.default.createElement(
          'div',
          null,
          'Add ',
          _react2.default.createElement(
            'strong',
            null,
            displayName
          ),
          ' to'
        ),
        _react2.default.createElement(
          'ul',
          null,
          buttons
        )
      );
    }
  }]);

  return QuickAdd;
}(_react.Component);

exports.default = QuickAdd;


QuickAdd.propTypes = propTypes;
QuickAdd.defaultProps = defaultProps;