"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

require("core-js/modules/es.array.join");

require("core-js/modules/es.regexp.exec");

require("core-js/modules/es.string.replace");

require("core-js/modules/es.string.split");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.getLocalizedFieldProp = getLocalizedFieldProp;
exports.fieldKeyToLabel = fieldKeyToLabel;
exports.sentenceCase = sentenceCase;
exports.titleCase = titleCase;
exports.capitalize = capitalize;

var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/toConsumableArray"));

// @flow

/*:: import type { Field, Key } from '../types'*/

/**
 * Either returns the translated user-defined label for a field, or creates a
 * label from the field key by replacing _ and - with spaces and formatting in
 * title case
 */
function getLocalizedFieldProp(field
/*: Field*/
, prop
/*: 'label' | 'placeholder'*/
)
/*: string*/
{
  var languageTag
  /*: string*/
  = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'en';
  // two-letter or three-letter ISO language code
  var languageCode = languageTag.split('-')[0]; // choose most specific label translation available e.g. language tag with
  // country code first, then just language code, then label without language
  // specified

  var label = field[prop + ':' + languageTag] || field[prop + ':' + languageCode] || field[prop];
  return label;
}

function fieldKeyToLabel(key
/*: Key*/
)
/*: string | string[]*/
{
  var fieldkey = typeof key === 'string' ? [key] : (0, _toConsumableArray2.default)(key);
  var labelArray = (0, _map.default)(fieldkey).call(fieldkey, function (s) {
    return titleCase(s + '');
  });
  return labelArray.length === 1 ? labelArray[0] : labelArray;
}

function sentenceCase() {
  var str
  /*: string*/
  = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  // Matches the first letter in the string and the first letter that follows a
  // period (and 1 or more spaces) and transforms that letter to uppercase.
  return str.replace(/(^[a-z])|(\.\s*[a-z])/g, function (str) {
    return str.toUpperCase();
  });
}
/**
 * For a string written in camel_case or snake-case, or space-separated, return
 * string formatted in title case
 */


function titleCase(str
/*: string*/
) {
  var _context;

  return (0, _map.default)(_context = str.toLowerCase().split(/\s|_|-/)).call(_context, function (word) {
    return capitalize(word);
  }).join(' ');
}

function capitalize(str
/*: string*/
) {
  return str.charAt(0).toUpperCase() + (0, _slice.default)(str).call(str, 1);
}
//# sourceMappingURL=strings.js.map