// 将BoxBrand转换为OrderPackage数组
import { BoxBrand, BoxProduct, BoxSpec } from "@/types/typings";
import { generateShortId } from "@/utils";

// 添加一个辅助函数用于分组
const groupBy = <T>(
  array: T[],
  keyFn: (item: T) => string,
): Record<string, T[]> => {
  return array.reduce(
    (groups, item) => {
      const key = keyFn(item);
      if (!groups[key]) {
        groups[key] = [];
      }
      groups[key].push(item);
      return groups;
    },
    {} as Record<string, T[]>,
  );
};

export const convertBoxBrandToOrderPackages = (
  boxBrand: BoxBrand,
  boxType: BusinessAPI.OrderPackage["boxType"] = "USED",
): BusinessAPI.OrderPackage[] => {
  const orderPackages: BusinessAPI.OrderPackage[] = [];

  boxBrand.boxSpecList?.forEach((category) => {
    category.boxProductList.forEach((product) => {
      orderPackages.push({
        orderPackageId: product.id,
        boxBrandId: product.boxBrandId,
        boxBrandName: product.boxBrandName,
        boxBrandImage: boxBrand.boxBrandImage,
        boxSpecId: product.boxSpecId,
        boxSpecName: product.boxSpecName,
        boxProductId: product.boxProductId,
        boxBrandType: boxBrand.boxBrandType,
        boxProductName: product.boxProductName,
        boxProductWeight: product.boxProductWeight,
        boxCount: product.boxCount || 0,
        boxCostPrice: product.boxCostPrice,
        boxSalePrice: product.boxSalePrice,
        boxType: boxType,
      });
    });
  });

  return orderPackages;
};

// 将OrderPackage数组转换为BoxBrand数组
export const convertOrderPackagesToBoxBrands = (
  orderPackages: BusinessAPI.OrderPackage[],
): BoxBrand[] => {
  // 按品牌ID分组
  const packagesByBrand = groupBy(orderPackages, (pkg) => pkg.boxBrandId);

  // 转换为BoxBrand数组
  return Object.entries(packagesByBrand).map(([brandId, packages]) => {
    const firstPackage = packages[0];

    // 按分类ID分组
    const packagesBySpec = groupBy(packages, (pkg) => pkg.boxSpecId);

    // 创建BoxCategory数组
    const boxSpecList: BoxSpec[] = Object.entries(packagesBySpec).map(
      ([specId, specList]) => {
        const firstCategoryPackage = specList[0];

        // 创建BoxProduct数组
        const boxProducts: BoxProduct[] = specList.map(
          (pkg) =>
            ({
              id: pkg.orderPackageId || generateShortId(),
              boxBrandId: pkg.boxBrandId,
              boxBrandName: pkg.boxBrandName,
              boxProductId: pkg.boxProductId,
              boxProductName: pkg.boxProductName,
              boxProductWeight: pkg.boxProductWeight,
              boxSpecId: pkg.boxSpecId,
              boxSpecName: pkg.boxSpecName,
              boxCostPrice: pkg.boxCostPrice,
              boxSalePrice: pkg.boxSalePrice,
              boxCount: pkg.boxCount,
            }) as BoxProduct,
        );

        return {
          id: generateShortId(),
          boxSpecId: specId,
          boxSpecName: firstCategoryPackage.boxSpecName,
          boxProductList: boxProducts,
        };
      },
    );

    return {
      id: brandId,
      boxBrandId: brandId,
      boxType: firstPackage.boxType,
      boxBrandName: firstPackage.boxBrandName,
      boxBrandImage: firstPackage.boxBrandImage!,
      boxBrandType: firstPackage.boxBrandType,
      boxSpecList: boxSpecList,
    };
  });
};
