// <wxs module="filters" src="../../utils/filters.wxs"></wxs>

var BASE_URL = "https://api.vip.shairport.com/weixin";

/**
 * filters.ImgUrl(item.BANNER)
 * @param str
 */
var ImgUrl = function(str) {
  if (!str || str.indexOf("/") !== 0) return str;
  return BASE_URL + str;
};

/**
 * padStart
 * @param {number} string
 * @param {number} length
 * @param {string} pad
 * @returns
 */
function padStart(string, length, pad) {
  var s = string.toString();
  if (!s || s.length >= length) return string;

  var len = length - s.length;

  var result = "";

  for (var i = 0; i < len; i++) {
    result += pad;
  }

  return result + string;
}

/**
 * filters.DateTime(item.CREATETIME, "YYYY-MM-DD")
 * @param dateStr
 * @param formatStr
 */
var DateTime = function(dateStr, formatStr) {
  if (!dateStr) return dateStr;

  var date = getDate(dateStr);

  var my_y = date.getFullYear().toString();
  var my_M = date.getMonth();
  var my_D = date.getDate();
  var my_H = date.getHours();
  var my_m = date.getMinutes();
  var my_s = date.getSeconds();

  var matches = {
    YY: my_y.slice(-2),
    YYYY: my_y,
    M: my_M + 1,
    MM: padStart(my_M + 1, 2, "0"),
    D: my_D,
    DD: padStart(my_D, 2, "0"),
    H: my_H.toString(),
    HH: padStart(my_H, 2, "0"),
    m: my_m.toString(),
    mm: padStart(my_m, 2, "0"),
    s: my_s.toString(),
    ss: padStart(my_s, 2, "0")
  };

  var keys = [
    "YYYY",
    "YY",
    "MM",
    "M",
    "DD",
    "D",
    "HH",
    "H",
    "mm",
    "m",
    "ss",
    "s"
  ];

  var str = formatStr || "YYYY-MM-DD HH:mm:ss";

  for (var i = 0; i < keys.length; i++) {
    var key = keys[i];

    while (str.indexOf(key) !== -1) {
      str = str.replace(key, matches[key]);
    }
  }

  return str;

  // var REGEX_FORMAT = getRegExp(
  //   "Y{2,4}|M{1,4}|D{1,2}|H{1,2}|m{1,2}|s{1,2}",
  //   "g"
  // );
  // return str.replace(REGEX_FORMAT, function(match, str1) {
  //   return str1 || matches[match];
  // });
};

module.exports = {
  ImgUrl: ImgUrl,
  DateTime: DateTime
};
