import Taro from '@tarojs/taro';
import padEnd from 'lodash/padEnd';

export function formatArea(areaInfo) {
  // 获取省节点
  const proviceArr = areaInfo
    .filter((item) => item.administrativeDivisionType === 1)
    .map((item) => {
      // 获取市节点
      const cityArr = areaInfo
        .filter((i) => i.parentAreaId === item.areaId)
        .map((i) => {
          // 获取区县节点
          const country = areaInfo
            .filter((j) => j.parentAreaId === i.areaId)
            .map((j) => {
              return { label: j.name, value: j.areaId, children: null, isLeaf: false };
            });
          const md = i.administrativeDivisionType === 3 ? { isLeaf: false } : {};
          return {
            label: i.name,
            value: i.areaId,
            children: country.length === 0 ? undefined : country,
            ...md,
          };
        });
      return { label: item.name, value: item.areaId, children: cityArr };
    });
  return [{ label: '全国', value: '10', children: [] }].concat(proviceArr);
}



export function fullZero(areaId) {
  let result = areaId;
  if (areaId.length < 6) {
    // result = padEnd(areaId, 6, 0);
    result = padEnd(areaId, 6, '0');
  }

  return result;
}

interface StorageProps {
  key: string
  data: string | number
  errMsg?: string
}

export function setItem({ key, data }: StorageProps) {
  Taro.setStorageSync(key, data)
}

export function removeItem(key: string) {
  Taro.removeStorage({
    key
  })
}

export function getItem(key: string) {
  return Taro.getStorageSync(key)
}

export function getValue(value: any) {
  if (value === undefined || value === null) {
    return ''
  }
  return value
}

export function isEmpty(value: any) {
  if (value === undefined || value === null || value === '' || value.length === 0) {
    return true
  }
  return false
}

export function uuid() {
  let s: any = [];
  let hexDigits = "0123456789abcdef";
  for (let i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  const uuid = s.join("");
  return uuid
}

export function getTimestamp(date: string) {
  return new Date(date).getTime()
}

export function getTime(timestamp: number) {
  const time = new Date(timestamp).toLocaleDateString().replace(/\//g, "-") + " " + new Date(timestamp).toTimeString().substr(0, 8);
  return time.substring(0, time.length - 3)
}