const strategy = {
  checkEmpty: (value, defaultValue) => {
    return value ?? defaultValue;
  },
  dateyyyyMMdd: (value, parttern = /^(\d{4})[^\d]*(\d{2})[^\d]*(\d{2})[^\d]*$/) => {
    return value.replace(parttern, '$1$2$3');
  },
  getType: (value) => {
    return Object.prototype.toString.call(value).replace(/^\[object.(.+)\]$/, '$1');
  },
  /**
   * form.dealStatus|生效|@info.dcs.amount|1,000,000.00USD|
   * parse to
   * { 'form.dealStatus': '生效', 'info.dcs.amount': '1,000,000.00USD'}
   * @param {*} str
   * @param {*} splitUnit
   * @returns
   */
  parseMapstrToObject: (str, splitUnit) => {
    const array = str.split(splitUnit);
    const obj = {};
    array.forEach((item, index) => {
      if (item && index + 1 >= 2 && (index + 1) % 2 == 0) { obj[array[index - 1].replace(/^@/, '')] = item; }
    });
    return obj;
  },
};

/**
 * 表单工具策略类
 */
export default function FormTools() {

}

FormTools.checkEmpty = function(value, defaultValue) {
  return strategy.checkEmpty(value, defaultValue);
};

FormTools.format = function(formatPattern, value, defaultValue) {
  console.log('FormTools format', ...arguments);
  if (!value) return strategy.checkEmpty(value, defaultValue);
  const array = formatPattern.split(':');
  return strategy[`${array[0]}${array[1]}`](value);
};

FormTools.getNowStr = function() {
  const date = new Date();
  return `${date.getFullYear()}${String(date.getMonth() + 1).padStart(2, '0')}${String(date.getDate()).padStart(2, '0')}`;
};

FormTools.parseMapStr = function(str) {
  return strategy.parseMapstrToObject(str, '|');
};
