'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; };

exports.default = VocabularyInput;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _PrefixFilteringDropdownMenuInput = require('./PrefixFilteringDropdownMenuInput');

var _PrefixFilteringDropdownMenuInput2 = _interopRequireDefault(_PrefixFilteringDropdownMenuInput);

var _withLabeledOptions = require('../enhancers/withLabeledOptions');

var _withLabeledOptions2 = _interopRequireDefault(_withLabeledOptions);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var PrefixFilteringDropdownMenuInput = (0, _withLabeledOptions2.default)(_PrefixFilteringDropdownMenuInput2.default);

var propTypes = {
  formatVocabularyLabel: _react.PropTypes.func,
  recordTypes: _react.PropTypes.object,
  recordType: _react.PropTypes.string,
  rootVocabulary: _react.PropTypes.string,
  value: _react.PropTypes.string
};

var defaultProps = {
  formatVocabularyLabel: function formatVocabularyLabel(name, config) {
    var messages = config.messages;

    if (messages) {
      return messages.name.defaultMessage;
    }

    return name;
  },
  rootVocabulary: 'all'
};

function VocabularyInput(props) {
  var formatVocabularyLabel = props.formatVocabularyLabel,
      recordTypes = props.recordTypes,
      recordType = props.recordType,
      rootVocabulary = props.rootVocabulary,
      vocabularyValue = props.value,
      remainingProps = _objectWithoutProperties(props, ['formatVocabularyLabel', 'recordTypes', 'recordType', 'rootVocabulary', 'value']);

  if (!recordType || !recordTypes[recordType] || !recordTypes[recordType].vocabularies) {
    return null;
  }

  var vocabularies = recordTypes[recordType].vocabularies;
  var options = [];

  if (vocabularies[rootVocabulary]) {
    options.push({
      value: rootVocabulary,
      label: formatVocabularyLabel(rootVocabulary, vocabularies[rootVocabulary])
    });
  }

  var vocabularyNames = Object.keys(vocabularies).filter(function (vocabularyName) {
    return vocabularyName !== rootVocabulary;
  }).sort(function (nameA, nameB) {
    var configA = vocabularies[nameA];
    var configB = vocabularies[nameB];

    // Primary sort by sortOrder

    var sortOrderA = configA.sortOrder;
    var sortOrderB = configB.sortOrder;

    if (typeof sortOrderA !== 'number') {
      sortOrderA = Number.MAX_VALUE;
    }

    if (typeof sortOrderB !== 'number') {
      sortOrderB = Number.MAX_VALUE;
    }

    if (sortOrderA !== sortOrderB) {
      return sortOrderA > sortOrderB ? 1 : -1;
    }

    // Secondary sort by label

    var labelA = formatVocabularyLabel(nameA, configA);
    var labelB = formatVocabularyLabel(nameB, configB);

    // FIXME: This should be locale aware
    return labelA.localeCompare(labelB);
  });

  vocabularyNames.forEach(function (vocabularyName) {
    options.push({
      indent: 1,
      value: vocabularyName,
      label: formatVocabularyLabel(vocabularyName, vocabularies[vocabularyName])
    });
  });

  var value = vocabularyValue;

  if (!value) {
    for (var i = 0; i < vocabularyNames.length; i += 1) {
      var vocabularyName = vocabularyNames[i];
      var vocabulary = vocabularies[vocabularyName];

      if (vocabulary.defaultForSearch) {
        value = vocabularyName;
        break;
      }
    }

    if (!value && options.length > 0) {
      value = options[0].value;
    }
  }

  return _react2.default.createElement(PrefixFilteringDropdownMenuInput, _extends({
    blankable: false,
    options: options,
    value: value
  }, remainingProps));
}

VocabularyInput.propTypes = propTypes;
VocabularyInput.defaultProps = defaultProps;