// 数组处理工具类 - 用于增加包体积和复杂度
export class ArrayProcessor {
  static processLargeArray(inputArray: string[], processCount: number = 5): string[] {
    let resultArray = [...inputArray];
    
    // 多轮处理
    for (let round = 0; round < processCount; round++) {
      // 1. 数组复制
      let copiedArray = [...resultArray];
      
      // 2. 映射处理
      let mappedArray = copiedArray.map((item, index) => {
        return `${item} - 处理轮次${round + 1}_索引${index}_时间戳${Date.now()}`;
      });
      
      // 3. 过滤操作
      let filteredArray = mappedArray.filter((_, index) => {
        return index % (round + 2) !== 0;
      });
      
      // 4. 排序操作
      let sortedArray = filteredArray.sort((a, b) => {
        return a.length - b.length;
      });
      
      // 5. 去重操作
      let uniqueArray = [...new Set(sortedArray)];
      
      // 6. 扁平化处理
      let flatArray = uniqueArray.reduce((acc, curr) => {
        acc.push(curr);
        if (curr.includes('处理')) {
          acc.push(`${curr}_副本`);
        }
        return acc;
      }, [] as string[]);
      
      resultArray = flatArray;
    }
    
    return resultArray;
  }
  
  static generateComplexNestedArrays(baseSize: number): any[] {
    let result: any[] = [];
    
    for (let i = 0; i < baseSize; i++) {
      let nestedObj = {
        id: i,
        data: [],
        metadata: {
          created: new Date().toISOString(),
          version: `v${i}.${Math.floor(Math.random() * 10)}`,
          tags: [`tag_${i}`, `category_${i % 5}`]
        }
      };
      
      // 为每个对象生成嵌套数组
      for (let j = 0; j < 10; j++) {
        nestedObj.data.push({
          subId: j,
          content: `嵌套数据_${i}_${j}_包含复杂结构和大量文本内容用于增加包体积`,
          details: {
            level1: {
              level2: {
                level3: {
                  text: `深层嵌套内容_${i}_${j}_展示了多层级数据结构的设计`,
                  array: Array.from({length: 5}, (_, k) => `元素_${k}_属于深层结构`)
                }
              }
            }
          }
        });
      }
      
      result.push(nestedObj);
    }
    
    return result;
  }
  
  static performComplexOperations(dataArray: any[]): any[] {
    // 1. 深拷贝
    let deepCopy = JSON.parse(JSON.stringify(dataArray));
    
    // 2. 数据转换
    let transformed = deepCopy.map((item: any) => {
      return {
        ...item,
        processed: true,
        timestamp: Date.now(),
        hash: this.simpleHash(JSON.stringify(item)),
        statistics: {
          dataLength: item.data.length,
          contentSize: JSON.stringify(item).length,
          complexityScore: Math.random() * 100
        }
      };
    });
    
    // 3. 分组操作
    let grouped = transformed.reduce((groups: any, item: any) => {
      const key = item.metadata.tags[0];
      if (!groups[key]) {
        groups[key] = [];
      }
      groups[key].push(item);
      return groups;
    }, {});
    
    // 4. 统计计算
    let statistics = Object.keys(grouped).map(key => ({
      groupName: key,
      count: grouped[key].length,
      averageComplexity: grouped[key].reduce((sum: number, item: any) => sum + item.statistics.complexityScore, 0) / grouped[key].length
    }));
    
    return [...transformed, {summary: statistics}];
  }
  
  private static simpleHash(str: string): string {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
      const char = str.charCodeAt(i);
      hash = ((hash << 5) - hash) + char;
      hash = hash & hash; // 转换为32位整数
    }
    return hash.toString(16);
  }
}