import { SupplierWeightCalculator } from "../modules/SupplierWeightCalculator";

/**
 * 重量计算服务
 * 统一管理供应商重量计算逻辑
 */
export class WeightCalculationService {
  /**
   * 计算供应商重量（静态方法）
   * @param suppliers 供应商列表
   * @returns 计算后的供应商列表
   */
  static calculateWeights(suppliers: BusinessAPI.OrderSupplier[]): BusinessAPI.OrderSupplier[] {
    const calculator = new SupplierWeightCalculator(suppliers);
    return calculator.calculate();
  }

  /**
   * 创建重量计算器实例
   * @param suppliers 供应商列表
   * @returns 计算器实例
   */
  static createCalculator(suppliers: BusinessAPI.OrderSupplier[]): SupplierWeightCalculator {
    return new SupplierWeightCalculator(suppliers);
  }

  /**
   * 批量计算多个订单的重量
   * @param orders 订单列表
   * @returns 处理后的订单列表
   */
  static calculateBatchWeights(
    orders: Array<{ orderSupplierList?: BusinessAPI.OrderSupplier[] }>
  ): Array<{ orderSupplierList?: BusinessAPI.OrderSupplier[] }> {
    return orders.map(order => {
      if (order.orderSupplierList?.length) {
        order.orderSupplierList = this.calculateWeights(order.orderSupplierList);
      }
      return order;
    });
  }

  /**
   * 获取重量统计信息
   * @param suppliers 供应商列表
   * @returns 统计信息
   */
  static getWeightStatistics(suppliers: BusinessAPI.OrderSupplier[]) {
    const calculator = new SupplierWeightCalculator(suppliers);
    calculator.calculate();

    return {
      supplierCount: suppliers.length,
      totalNetWeight: calculator.getTotalNetWeight(),
      totalGrossWeight: calculator.getTotalGrossWeight(),
      totalInvoiceAmount: calculator.getTotalInvoiceAmount(),
      averageNetWeight: calculator.getTotalNetWeight() / suppliers.length,
      averageGrossWeight: calculator.getTotalGrossWeight() / suppliers.length,
    };
  }
}
