import axios from 'axios';
import randomstring from 'randomstring';
import fetchAdapter from '@vespaiach/axios-fetch-adapter';
import { SHOPEE_VIDEO_CREATOR_HOST, SHOPEE_VIDEO_MMS_HOST, SHOPEE_VIDEO_REGION } from './video.constant';

export const apiVideoCreator = axios.create({
  baseURL: SHOPEE_VIDEO_CREATOR_HOST,
  responseType: 'json',
  withCredentials: true,
  adapter: fetchAdapter,
});

apiVideoCreator.interceptors.response.use((response) => response.data);
apiVideoCreator.interceptors.request.use((request) => {
  const region = String(SHOPEE_VIDEO_REGION).toLowerCase();

  request.headers = {
    accept: 'application/json',
    language: 'en',
    'x-env': 'live',
    'x-region': region,
    'x-region-domain': region,
    'x-region-timezone': '+0700',
    'x-traceid': randomstring.generate(21),
    ...(request.headers || {}),
  };

  return request;
});

export const apiVideoMms = axios.create({
  baseURL: SHOPEE_VIDEO_MMS_HOST,
  responseType: 'json',
  withCredentials: true,
  adapter: fetchAdapter,
});

apiVideoMms.interceptors.response.use((response) => response.data);
apiVideoMms.interceptors.request.use((request) => {
  request.headers = {
    accept: 'application/json, text/plain, */*',
    ...(request.headers || {}),
  };

  return request;
});
