// 供货区域调试工具
// 用于调试供货区域选择和回显的问题

/**
 * 调试级联选择器的数据结构
 */
export function debugCascaderData(data: any[], title: string = '级联数据') {
  console.log(`=== ${title} ===`);
  console.log('数据总数:', data.length);
  
  if (data.length > 0) {
    const firstItem = data[0];
    console.log('第一项结构:', {
      areaCode: firstItem.areaCode,
      areaName: firstItem.areaName,
      level: firstItem.level,
      hasChildren: !!firstItem.children,
      childrenCount: firstItem.children?.length || 0
    });
    
    if (firstItem.children && firstItem.children.length > 0) {
      const firstChild = firstItem.children[0];
      console.log('第一个子项结构:', {
        areaCode: firstChild.areaCode,
        areaName: firstChild.areaName,
        level: firstChild.level,
        parentCode: firstChild.parentCode
      });
    }
  }
}

/**
 * 调试选中路径的格式
 */
export function debugSelectedPaths(paths: any[], title: string = '选中路径') {
  console.log(`=== ${title} ===`);
  console.log('路径总数:', paths.length);
  
  paths.forEach((path, index) => {
    if (Array.isArray(path)) {
      console.log(`路径 ${index + 1}:`, {
        length: path.length,
        provinceCode: path[0],
        cityCode: path[1],
        path: path
      });
    } else {
      console.log(`路径 ${index + 1} (非数组):`, path);
    }
  });
}

/**
 * 调试保存的区域数据
 */
export function debugSavedAreaData(data: any[], title: string = '保存的区域数据') {
  console.log(`=== ${title} ===`);
  console.log('数据总数:', data.length);
  
  const provinces = data.filter(item => item.level === 1 || item.level === '1');
  const cities = data.filter(item => item.level === 2 || item.level === '2');
  
  console.log('省份数量:', provinces.length);
  console.log('城市数量:', cities.length);
  
  provinces.forEach(province => {
    const provinceCities = cities.filter(city => city.parentCode === province.areaCode);
    console.log(`${province.areaName} (${province.areaCode}):`, 
      provinceCities.map(city => `${city.areaName}(${city.areaCode})`).join(', ') || '无城市'
    );
  });
}

/**
 * 验证级联选择器配置
 */
export function validateCascaderProps(props: any) {
  console.log('=== 级联选择器配置验证 ===');
  console.log('配置:', props);
  
  const requiredProps = ['value', 'label', 'children'];
  const missingProps = requiredProps.filter(prop => !props[prop]);
  
  if (missingProps.length > 0) {
    console.error('缺少必要配置:', missingProps);
  } else {
    console.log('✓ 配置完整');
  }
  
  if (props.multiple) {
    console.log('✓ 多选模式');
  }
  
  if (props.checkStrictly) {
    console.log('⚠️ 严格模式 - 父子不关联');
  } else {
    console.log('✓ 关联模式 - 父子关联');
  }
}

/**
 * 模拟级联选择器的选择过程
 */
export function simulateSelection(
  supplyAreaOptions: any[], 
  targetProvinces: string[], 
  targetCities: { [provinceCode: string]: string[] }
) {
  console.log('=== 模拟选择过程 ===');
  console.log('目标省份:', targetProvinces);
  console.log('目标城市:', targetCities);
  
  const selectedPaths: any[] = [];
  
  targetProvinces.forEach(provinceName => {
    // 查找省份
    const province = supplyAreaOptions.find(p => p.areaName === provinceName);
    if (!province) {
      console.error(`未找到省份: ${provinceName}`);
      return;
    }
    
    const provinceCode = province.areaCode;
    const cities = targetCities[provinceName] || [];
    
    if (cities.length === 0) {
      // 只选择省份
      selectedPaths.push([provinceCode]);
      console.log(`选择省份: ${provinceName} (${provinceCode})`);
    } else {
      // 选择省份下的城市
      cities.forEach(cityName => {
        const city = province.children?.find((c: any) => c.areaName === cityName);
        if (city) {
          selectedPaths.push([provinceCode, city.areaCode]);
          console.log(`选择城市: ${provinceName} > ${cityName} (${provinceCode}, ${city.areaCode})`);
        } else {
          console.error(`未找到城市: ${provinceName} > ${cityName}`);
        }
      });
    }
  });
  
  console.log('生成的选择路径:', selectedPaths);
  return selectedPaths;
}

/**
 * 完整的调试流程
 */
export function debugSupplyAreaFlow(
  supplyAreaOptions: any[],
  savedAreaData: any[],
  selectedPaths: any[]
) {
  console.log('🔍 === 供货区域完整调试 ===');
  
  // 1. 调试数据源
  debugCascaderData(supplyAreaOptions, '供货区域选项数据');
  
  // 2. 调试保存的数据
  debugSavedAreaData(savedAreaData, '后端保存的数据');
  
  // 3. 调试选中路径
  debugSelectedPaths(selectedPaths, '当前选中路径');
  
  // 4. 验证配置
  validateCascaderProps({
    multiple: true,
    checkStrictly: false,
    value: 'areaCode',
    label: 'areaName',
    children: 'children',
    emitPath: true
  });
  
  console.log('🔍 === 调试完成 ===');
}

// 在开发环境下暴露到全局
if (import.meta.env.DEV) {
  (window as any).debugSupplyArea = {
    debugCascaderData,
    debugSelectedPaths,
    debugSavedAreaData,
    validateCascaderProps,
    simulateSelection,
    debugSupplyAreaFlow
  };
}