/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat {y}-{m}-{d} {h}:{i}:{s}
 * @returns {string | null}
 */
export function parseTime(time: any, cFormat: string) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (_result, key) => {
    // @ts-ignore
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

export function optionsFilter(value: string, options = [], emptyFill = '', valueKey = 'value', returnKey = 'label') {
  const item = options.find(it => it[valueKey] === value)
  if (item) {
    return item[returnKey]
  } else {
    return emptyFill
  }
}

export function splitDates(dateStr: string) {
  return (dateStr.split(',').length > 1
    ? (dateStr.split(',')[1] == 'undefined'
      ? parseTime(dateStr.split(',')[0], '{y}-{m}-{d}')
      : parseTime(dateStr.split(',')[0], '{y}-{m}-{d}') + '~' + dateStr.split(',')[1])
    : parseTime(dateStr.split(',')[0], '{y}-{m}-{d}'))
}

export function toarrary(value: string) {
  return (value || '').split(',')
}

export function nationList() {
  return [
    { id: "01", name: "汉族" },
    { id: "02", name: "蒙古族" },
    { id: "03", name: "回族" },
    { id: "04", name: "藏族" },
    { id: "05", name: "维吾尔族" },
    { id: "06", name: "苗族" },
    { id: "07", name: "彝族" },
    { id: "08", name: "壮族" },
    { id: "09", name: "布依族" },
    { id: "10", name: "朝鲜族" },
    { id: "11", name: "满族" },
    { id: "12", name: "侗族" },
    { id: "13", name: "瑶族" },
    { id: "14", name: "白族" },
    { id: "15", name: "土家族" },
    { id: "16", name: "哈尼族" },
    { id: "17", name: "哈萨克族" },
    { id: "18", name: "傣族" },
    { id: "19", name: "黎族" },
    { id: "20", name: "傈僳族" },
    { id: "21", name: "佤族" },
    { id: "22", name: "畲族" },
    { id: "23", name: "高山族" },
    { id: "24", name: "拉祜族" },
    { id: "25", name: "水族" },
    { id: "26", name: "东乡族" },
    { id: "27", name: "纳西族" },
    { id: "28", name: "景颇族" },
    { id: "29", name: "柯尔克孜族" },
    { id: "30", name: "土族" },
    { id: "31", name: "达斡尔族" },
    { id: "32", name: "仫佬族" },
    { id: "33", name: "羌族" },
    { id: "34", name: "布朗族" },
    { id: "35", name: "撒拉族" },
    { id: "36", name: "毛难族" },
    { id: "37", name: "仡佬族" },
    { id: "38", name: "锡伯族" },
    { id: "39", name: "阿昌族" },
    { id: "40", name: "普米族" },
    { id: "41", name: "塔吉克族" },
    { id: "42", name: "怒族" },
    { id: "43", name: "乌孜别克族" },
    { id: "44", name: "俄罗斯族" },
    { id: "45", name: "鄂温克族" },
    { id: "46", name: "崩龙族" },
    { id: "47", name: "保安族" },
    { id: "48", name: "裕固族" },
    { id: "49", name: "京族" },
    { id: "50", name: "塔塔尔族" },
    { id: "51", name: "独龙族" },
    { id: "52", name: "鄂伦春族" },
    { id: "53", name: "赫哲族" },
    { id: "54", name: "门巴族" },
    { id: "55", name: "珞巴族" },
    { id: "56", name: "基诺族" }
  ];
}

export function hideInformation(value: string, start: number, length: number|undefined) {
  const prefix = value.substring(0, start)
  let suffixLen = value.length - start
  if (length) {
    suffixLen = length - start
  }
  const suffix = new Array(suffixLen).fill('*').join('')
  return prefix + suffix
}