'use strict';

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

var _case = require('case');

var _case2 = _interopRequireDefault(_case);

var _roundTo = require('round-to');

var _roundTo2 = _interopRequireDefault(_roundTo);

var _reactIntl = require('react-intl');

var _constants = require('../constants');

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

var messages = (0, _reactIntl.defineMessages)({
  true: {
    'id': 'field_value.true',
    'defaultMessage': 'Yes'
  },
  false: {
    'id': 'field_value.false',
    'defaultMessage': 'No'
  },
  null: {
    'id': 'field_value.no_value',
    'defaultMessage': '[Null]'
  },
  undefined: {
    'id': 'field_value.undefined',
    'defaultMessage': '[No Value]'
  }
});

var createMessage = exports.createMessage = function createMessage(section) {
  return function formatMsg(value) {
    var msg = void 0;
    var id = void 0;
    if (value === _constants.UNDEFINED_KEY) {
      return messages.undefined;
    } else if (typeof value === 'string') {
      if (value.length === 0) return messages.null;
      if (value.indexOf('_') > -1 && value.indexOf(' ') < 0) {
        msg = _case2.default.capital(value);
      } else {
        msg = toSentenceCase(value);
      }
      id = value.replace(' ', '_').toLowerCase();
    } else if (Array.isArray(value)) {
      msg = value.map(function (v) {
        if (typeof v === 'number') return (0, _roundTo2.default)(v, 5);
        return formatMsg(v).defaultMessage;
      }).join(', ');
      id = value.join('_');
    } else if (typeof value === 'boolean') {
      if (value) return messages.true;else return messages.false;
    } else {
      if (value == null) {
        return messages.null;
      } else {
        msg = value.toString();
        id = value.toString();
      }
    }
    return {
      id: section + '.' + id,
      defaultMessage: msg
    };
  };
};

/**
 * toCase.sentence() lower cases strings before turning to sentence case.
 * toSentenceCase() uses the same logic, but without lowercasing first.
 */
function toSentenceCase(s) {
  var sentenceRegExp = _case2.default._.re.sentence;
  return s.replace(sentenceRegExp, function (m, prelude, letter) {
    return prelude + letter.toUpperCase();
  });
}
//# sourceMappingURL=intl_helpers.js.map