import axios from 'axios';
import fetchAdapter from '@vespaiach/axios-fetch-adapter';

const gptApi = axios.create({
  baseURL: 'https://gpt-api.autoshopee.com/api',
  adapter: fetchAdapter,
  timeout: 60000,
});

gptApi.interceptors.response.use((response) => response.data);

gptApi.interceptors.request.use((request) => request);

export const gptWriteContent = async (text) => {
  try {
    const result = await gptApi.post('/writer/title', {
      text,
    });

    return result.data ? result.data.content : text;
  } catch (error) {
    return text;
  }
};

/**
 *
 * @param {string} text
 * @returns {Promise<text>}
 */
export const gptWriterProductTitle = async (text) => {
  try {
    const result = await gptApi.post('/shopee/product/writer/title', {
      text,
    });

    return result.data ? result.data.content : text;
  } catch (error) {
    return text;
  }
};

/**
 *
 * @param {string} text
 * @returns {Promise<text>}
 */
export const gptWriterProductDescription = async (text) => {
  try {
    const result = await gptApi.post('/shopee/product/writer/description', {
      text,
    });

    return result.data ? result.data.content : text;
  } catch (error) {
    return text;
  }
};
