import type { INumberType } from "./types";

// 生成蓝色号码列表
export const generateBlueNumbers = () => Array.from({ length: 16 }, (_, index) => index + 1);

// 生成红色号码列表
export function generateRedNumbers(excludeNumbers: number[] = []) {
  const numbers = Array.from({ length: 33 }, (_, index) => index + 1);
  return numbers.filter(number => !excludeNumbers.includes(number));
}

// 生成单个红球号码
const generateRedNumber = () => Math.floor(Math.random() * 33) + 1;
// 生成单个蓝球号码
const generateBlueNumber = () => Math.floor(Math.random() * 16) + 1;

// 将10以下的号码格式化为两位数
export const formatNumber = num => String(num).length < 2 ? `0${num}` : `${num}`;
// 判断是否为蓝色球
export const isBlueNumber = (index: number) => index === 6;

// 生成一组双色球号码
export function generateDoubleColorNumber() {
  const numbers: number[] = [];
  while (numbers.length !== 6) {
    const red = generateRedNumber();
    if (numbers.includes(red)) {
      continue;
    }
    numbers.push(red); ;
  }
  return [...numbers, generateBlueNumber()];
}

export const getNumberType = (type: INumberType) => type === "blue" ? "蓝色球" : "红色球";

// 0填充不足七位的号码
export function fillZero2Numbers(numbers?: number[]) {
  if (!numbers || numbers.length < 7) {
    return [...numbers || [], ...Array.from({ length: 7 - (numbers || []).length }, () => 0)];
  }

  if (numbers.length > 7) {
    return numbers.slice(0, 7);
  }

  return numbers;
}

// 根据数组长度生成随机索引
export function getRandomIndex(length, number) {
  if (number > length) {
    return [];
  }
  const result = Array.from({ length }, (_, index) => index);
  for (let i = 0; i < number; i++) {
    const randomIndex = Math.floor(Math.random() * result.length);
    const temp = result[randomIndex];
    result[randomIndex] = result[i];
    result[i] = temp;
  }
  return result.slice(0, number);
}
