'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
/**
 * Created by baoyinghai on 10/25/16.
 */
var reg = /\${\w*}/g;
var dic = /^[A-Z_]*$/;

var getKey = function getKey(key) {
  if (key.length > 2) {
    return key.substring(2, key.length - 1);
  }
  return '';
};

var firstUpperCase = exports.firstUpperCase = function firstUpperCase(str) {
  return str.replace(/\b(\w)(\w*)/g, function ($0, $1, $2) {
    return $1.toUpperCase() + $2.toLowerCase();
  });
};

var transformValue = function transformValue(value, mappings) {
  var result = mappings && mappings.reduce(function (obj, item) {
    obj[item.dictionaryItemName] = item.serviceName;
    return obj;
  }, {})[value];
  if (result) {
    return result;
  }
  if (dic.test(value)) {
    return value.split('_').map(function (str) {
      return firstUpperCase(str);
    }).join('');
  }
  return value;
};

var toLowerFirstCase = exports.toLowerFirstCase = function toLowerFirstCase(str) {
  return str && str.replace(/(\w)/, function (v) {
    return v.toLowerCase();
  });
};

var convertTemplate = exports.convertTemplate = function convertTemplate() {
  var template = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var mappings = arguments[2];
  return (template.match(reg) || []).reduce(function () {
    var temp = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var key = arguments[1];
    return temp.replace(key, transformValue(params[getKey(key || '')], mappings || []) || '');
  }, template);
};

var isArray = exports.isArray = function isArray(obj) {
  return Object.prototype.toString.call(obj) === '[object Array]';
};

var trimStart = exports.trimStart = function trimStart(str, chars) {
  if (str[0] === chars) {
    return str.substring(1);
  }
  return str;
};

var templateReg = exports.templateReg = reg;

var getTemplateFields = exports.getTemplateFields = function getTemplateFields() {
  var template = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  return (template.match(reg) || []).map(function (item) {
    return item.substring(2, item.length - 1);
  });
};

var trimStartAndEnd = exports.trimStartAndEnd = function trimStartAndEnd(str) {
  return str.replace(/(^\s*)|(\s*$)/g, '');
};