const factor = 1;
export const formatValueWithWan = (value: number) => {
  return Number(value*factor) >= 10000 ? Number((value*factor / 10000).toFixed(2)) : Number(value);
};

export const formatDecimalsWithWan = (value: number) => {
  return Number(value*factor) >= 10000 ? 2 : 0;
};

export const formatUnitWithWan = (value: number) => {
  return Number(value*factor) >= 10000 ? '万' : '';
};

// 处理区域树数据的函数，将区域树转换为按钮组数据结构
export const processRegionTree = (treeData: any[]) => {
  const level2Data: any[] = [];
  const level3Data: Record<string, any[]> = {};
  
  // 查找成都市节点
  const chengduNode = treeData.find(node => node.id === 5101);
  
  if (chengduNode && chengduNode.children) {
    // 处理区级 (level2)
    chengduNode.children.forEach((district: any) => {
      // 判断是否有子节点（街道）
      const hasChildren = district.children && district.children.length > 0;
      
      level2Data.push({
        key: `district_${district.id}`,
        label: district.gridName,
        hasChildren,
        regionCode: district.id.toString(),
        isDataTypeButton: false
      });
      
      // 如果有子节点，处理街道 (level3)
      if (hasChildren) {
        const streetData: any[] = [];
        
        district.children.forEach((street: any) => {
          streetData.push({
            key: `street_${street.id}`,
            label: street.gridName,
            regionCode: street.id.toString(),
            isDataTypeButton: false
          });
        });
        
        level3Data[`district_${district.id}`] = streetData;
      }
    });
  }
  
  return {
    level2: level2Data,
    level3: level3Data
  };
};

// 扁平化区域树，返回 regionCode 到区域名称的映射
export const flattenRegionTree = (treeData: any[]): Record<string, string> => {
  const mapping: Record<string, string> = {};
  
  // 添加成都市
  const chengduNode = treeData.find(node => node.id === 5101);
  if (chengduNode) {
    mapping['5101'] = chengduNode.gridName || '成都市';
    
    // 添加区县
    if (chengduNode.children) {
      chengduNode.children.forEach((district: any) => {
        mapping[district.id] = district.gridName;
        
        // 添加街道
        if (district.children) {
          district.children.forEach((street: any) => {
            mapping[street.id] = street.gridName;
          });
        }
      });
    }
  }
  
  return mapping;
};