import { Base64 } from 'js-base64'

function randomWord (randomFlag, min, max) {
  let str = ''
  let range = min
  let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min
  }
  for (var i = 0; i < range; i++) {
    let pos = Math.round(Math.random() * (arr.length - 1))
    str += arr[pos]
  }
  return str
}



function encodeStr (str) {
  const NUM = 2
  const front = randomWord(false, 8)
  const middle = randomWord(false, 8)
  const end = randomWord(false, 8)

  let str1 = str.substring(0, NUM)
  let str2 = str.substring(NUM)

  return front + str2 + middle + str1 + end
}

export default function (password) {
  return encodeStr(Base64.encode(password))
}