/*
 * @Author: tengmingxue 1473375109@qq.com
 * @Date: 2023-08-15 11:20:48
 * @LastEditors: tengmingxue 1473375109@qq.com
 * @LastEditTime: 2023-09-19 08:48:16
 * @FilePath: \xld-gis-admin\src\utils\uuid.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const hexList: string[] = [];
for (let i = 0; i <= 15; i++) {
  hexList[i] = i.toString(16);
}

export function buildUUID(): string {
  let uuid = '';
  for (let i = 1; i <= 36; i++) {
    if (i === 9 || i === 14 || i === 19 || i === 24) {
      uuid += '-';
    } else if (i === 15) {
      uuid += 4;
    } else if (i === 20) {
      uuid += hexList[(Math.random() * 4) | 8];
    } else {
      uuid += hexList[(Math.random() * 16) | 0];
    }
  }
  return uuid.replace(/-/g, '');
}

export function getBuildUUID(): string {
  let uuid = '';
  for (let i = 1; i <= 36; i++) {
    if (i === 9 || i === 14 || i === 19 || i === 24) {
      uuid += '-';
    } else if (i === 15) {
      uuid += 4;
    } else if (i === 20) {
      uuid += hexList[(Math.random() * 4) | 8];
    } else {
      uuid += hexList[(Math.random() * 16) | 0];
    }
  }
  return uuid;
}

let unique = 0;
export function buildShortUUID(prefix = ''): string {
  const time = Date.now();
  const random = Math.floor(Math.random() * 1000000000);
  unique++;
  return prefix + '_' + random + unique + String(time);
}

export function getlowerCaseChars() {
  return Array.from({ length: 26 }).map((_, index) => String.fromCharCode(index + 97));
}

export function getUpperCaseChars() {
  return Array.from({ length: 26 }).map((_, index) => String.fromCharCode(index + 65));
}

export function randomString(length = 20) {
  const upperCaseChars = getUpperCaseChars();
  const lowerCaseChars = getlowerCaseChars();
  const numberChars = Array.from({ length: 10 }).map((_, index) => index);
  const allChars = [...numberChars, ...upperCaseChars, ...lowerCaseChars].sort(
    () => 0.5 - Math.random()
  );

  const rangeFn = () => (Math.random() * 63) | 0;

  return Array.from({ length })
    .map(() => allChars[rangeFn()])
    .join('');
}
