import { apiShopeeSeller, sellerFetchPost } from '@/api/shopee/seller/init';
import { adapterCreateProduct } from '@/adapter/shopee/adapter';
import { validatorProductCreate } from '@/validator/shopee/seller/product';
import { isDevMode } from '@/config/env';

export const shopeeSellerUpdateProductId = async (productId, payload) => {
  const uri = '/api/v3/product/update_product_info/';

  const payloadUpdate = {
    is_draft: false,
    product_id: productId,
    product_info: payload,
  };

  if (false) {
    const result = await sellerFetchPost(uri, payloadUpdate);

    return result;
  }

  const result = await apiShopeeSeller.post(uri, payloadUpdate);

  return result;
};

/**
 *
 * @param {ShopeePayloadProductCreate} payloadProduct
 * @returns
 */
export const shopeeSellerCreateProduct = async (payloadProduct) => {
  if (isDevMode) {
    payloadProduct = await validatorProductCreate.validateAsync(payloadProduct, {
      allowUnknown: true,
    });
  }

  const productCreate = await adapterCreateProduct(payloadProduct);

  const uri = '/api/v3/product/create_product_info';

  const payloadCreate = {
    is_draft: false,
    product_info: productCreate,
  };

  if (false) {
    const result = await sellerFetchPost(uri, payloadCreate);

    return result;
  }

  return (await apiShopeeSeller.post(uri, payloadCreate));
};
