'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 = RecordTypeInput;

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 = {
  formatRecordTypeLabel: _react.PropTypes.func,
  indentItems: _react.PropTypes.bool,
  recordTypes: _react.PropTypes.object,
  rootType: _react.PropTypes.string,
  serviceTypes: _react.PropTypes.arrayOf(_react.PropTypes.string),
  value: _react.PropTypes.string
};

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

    if (messages) {
      return messages.record.collectionName.defaultMessage;
    }

    return name;
  },
  indentItems: true,
  recordTypes: {},
  rootType: 'all',
  serviceTypes: ['object', 'procedure', 'authority']
};

function RecordTypeInput(props) {
  var formatRecordTypeLabel = props.formatRecordTypeLabel,
      indentItems = props.indentItems,
      recordTypes = props.recordTypes,
      rootType = props.rootType,
      serviceTypes = props.serviceTypes,
      recordTypeValue = props.value,
      remainingProps = _objectWithoutProperties(props, ['formatRecordTypeLabel', 'indentItems', 'recordTypes', 'rootType', 'serviceTypes', 'value']);

  var options = [];

  if (recordTypes[rootType]) {
    options.push({
      value: rootType,
      label: formatRecordTypeLabel(rootType, recordTypes[rootType])
    });
  }

  serviceTypes.forEach(function (serviceType) {
    var recordTypeNames = Object.keys(recordTypes).filter(function (recordTypeName) {
      return recordTypes[recordTypeName].serviceConfig.serviceType === serviceType;
    }).sort(function (nameA, nameB) {
      var configA = recordTypes[nameA];
      var configB = recordTypes[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 = formatRecordTypeLabel(nameA, configA);
      var labelB = formatRecordTypeLabel(nameB, configB);

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

    var indent = 0;

    if (recordTypeNames.length > 1 && recordTypes[serviceType]) {
      // If there is more than one record type within this service type, and the service type is
      // itself a record type, add the service type as a parent option.

      if (indentItems) {
        indent += 1;
      }

      options.push({
        indent: indent,
        value: serviceType,
        label: formatRecordTypeLabel(serviceType, recordTypes[serviceType])
      });
    }

    if (indentItems) {
      indent += 1;
    }

    recordTypeNames.forEach(function (recordTypeName) {
      options.push({
        indent: indent,
        value: recordTypeName,
        label: formatRecordTypeLabel(recordTypeName, recordTypes[recordTypeName])
      });
    });
  });

  var value = recordTypeValue;

  if (!value) {
    for (var i = 0; i < options.length; i += 1) {
      var recordTypeName = options[i].value;
      var recordType = recordTypes[recordTypeName];

      if (recordType.defaultForSearch) {
        value = recordTypeName;
        break;
      }
    }

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

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

RecordTypeInput.propTypes = propTypes;
RecordTypeInput.defaultProps = defaultProps;