import '@/types/Store/Shopee/Product';
import '@/types/Shopee/Seller/Payload';
import clone from 'clone';
import sumBy from 'sum-by';

export const adapterDetailProduct = async (raw) => {
  const product = {
    ...raw.product_price,
    ...raw.product_images,
    ...raw.product_attributes,
    ...raw.product_review,
    ...raw.product_meta,
    ...raw.item,
    shop_data: raw.shop_detailed,
    account_data: raw.account,
  };

  product.name = product.title;
  product.itemid = product.item_id;
  product.shopid = product.shop_id;
  product.models = (product.models || []).map((model) => {
    model.modelid = model.model_id || 0;
    model.itemid = model.item_id || product.item_id || 0;
    model.promotionid = model.promotion_id || 0;

    return model;
  });

  product.video_info_list = [product.video];

  const totalSold = sumBy(product.models, (model) => model.sold);

  product.sold = totalSold;
  product.historical_sold = product.global_sold;

  product.view_count = product.view_count || 0;
  product.liked_count = product.liked_count || 0;
  product.cmt_count = product.cmt_count || 0;

  return product;
};

/**
 *
 * @param {ShopeePayloadProductCreate} payload
 * @returns {ShopeePayloadProductCreate}
 */
export const adapterCreateProduct = async (payload) => {
  /** @type {ShopeePayloadProductCreate} */
  const product = clone({
    ...payload,
    weight: {
      unit: 2,
      value: payload.weight.toString(),
    },
    description_info: {
      description: payload.description,
      description_type: 'normal',
    },
    pre_order_info: {
      pre_order: payload.pre_order,
      days_to_ship: payload.days_to_ship,
    },
  });

  if (payload.description_json) {
    const listField = payload.description_json.map((item) => {
      if (item.img_id) {
        return {
          type: 1,
          value: item.img_id,
        };
      }

      if (!item.text) {
        return {
          type: 0,
          value: '\n\n',
        };
      }

      return {
        type: 0,
        value: `${item.text} \n`,
      };
    });

    product.description_info.description_type = 'json';

    product.description_info.description = JSON.stringify({
      field_list: listField,
    });
  }

  product.dimension.width = payload.dimension.width.toString();
  product.dimension.length = payload.dimension.length.toString();
  product.dimension.height = payload.dimension.height.toString();

  product.std_tier_variation_list = payload.tier_variation.map((item, index) => ({
    id: 0,
    group_id: index.toString(),
    custom_value: item.name,
    value_list: item.options.map((value, indexOption) => ({
      id: 0,
      custom_value: value,
      image_id: item.images[indexOption],
    })),
  }));

  product.model_list = payload.model_list.sort((a, b) => (a.name < b.name ? -1 : 1));

  product.enable_model_level_dts = false;
  product.attributes = payload.attributes.map((item) => {
    const selected = {};

    if (item.attribute_value_id) {
      selected.value_id = Number(item.attribute_value_id);
    } else if (item.custom_value && item.custom_value.raw_value) {
      selected.raw_value = item.custom_value.raw_value;
      selected.unit = item.custom_value.unit;
    }

    return {
      attribute_id: item.attribute_id,
      attribute_values: [selected],
    };
  });

  return product;
};
