import '@/types/Tiktok/Seller/FlashSale';
import { apiTiktokSeller } from '@/api/tiktok/seller/init';

const DEFAULT_PROMOTION_ITEM_DATA_QUERY = [
  {
    collection: {
      module: 'Promotions',
      business: 'promotion',
    },
  },
  {
    collection: {
      module: 'Campaigns',
      business: 'promotion',
    },
  },
];

/**
 *
 * @param {TiktokFlashSaleListPayload} options
 * @returns {Promise<TiktokFlashSaleListResponse>}
 */
export const listTiktokFlashSale = async (options = {}) => {
  const payload = {
    index: 0,
    size: 10,
    status: 1,
    promotion_type: 5,
    display_type: 4,
    diagnosis_type: 1,
    ...options,
  };

  const result = await apiTiktokSeller.post('/api/v1/promotion/list', payload);

  return result.data || null;
};

/**
 *
 * @param {string} promotionId
 * @returns {Promise<TiktokFlashSaleGetResponse>}
 */
export const getTiktokFlashSale = async (promotionId) => {
  const result = await apiTiktokSeller.get('/api/v1/promotion/flash_sale/get', {
    params: {
      promotion_id: promotionId,
    },
  });

  return result.data || null;
};

/**
 *
 * @param {string} promotionId
 * @param {TiktokFlashSaleProductPayload} options
 * @returns {Promise<TiktokFlashSaleProductResponse>}
 */
export const listTiktokFlashSaleProduct = async (promotionId, options = {}) => {
  const payload = {
    promotion_id: promotionId,
    cursor: 0,
    limit: 50,
    extra_info_list: [],
    ...options,
  };

  const result = await apiTiktokSeller.post('/api/v1/promotion/list_products_by_cursor', payload);

  return result.data || null;
};

/**
 *
 * @param {string} promotionId
 * @param {TiktokFlashSaleAuditLogPayload} options
 * @returns {Promise<TiktokFlashSaleAuditLogResponse>}
 */
export const queryTiktokFlashSaleAuditLog = async (promotionId, options = {}) => {
  const payload = {
    object_id: promotionId,
    status_type: 1,
    sort_type: 1,
    page_index: 0,
    page_size: 5,
    ...options,
  };

  const result = await apiTiktokSeller.post('/api/v1/promotion/seller_audit_log/query', payload);

  return result.data || null;
};

/**
 *
 * @param {TiktokFlashSaleCreatePayload} payload
 * @returns {Promise<TiktokFlashSaleCreateResponse>}
 */
export const createTiktokFlashSale = async (payload) => {
  const result = await apiTiktokSeller.post('/api/v1/promotion/flash_sale/create', payload);

  return result.data || null;
};

/**
 *
 * @param {TiktokPromotionItemDataQuery[]} itemDataQuery
 * @param {boolean} useSimplifiedItem
 * @returns {Promise<TiktokPromotionItemDataResponse>}
 */
export const getTiktokPromotionItemData = async (
  itemDataQuery = DEFAULT_PROMOTION_ITEM_DATA_QUERY,
  useSimplifiedItem = true,
) => {
  const payload = {
    use_simplified_item: useSimplifiedItem,
    item_data_query: itemDataQuery,
  };

  const result = await apiTiktokSeller.post('/api/v1/promotion/mget_item_data', payload);

  return result.data || null;
};
