"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.getHeader = getHeader;
exports.getHeaderVTM = getHeaderVTM;
var _apisauce = _interopRequireWildcard(require("apisauce"));
var Endpoint = _interopRequireWildcard(require("./Endpoint"));
var MyAsyncStorage = _interopRequireWildcard(require("../utils/MyAsyncStorage"));
var _AppStore = _interopRequireDefault(require("../screens/AppStore"));
var _reactNative = require("react-native");
var _ListChatStore = _interopRequireDefault(require("../screens/listchat/ListChatStore"));
var _ChatStore = _interopRequireDefault(require("../screens/chat/ChatStore"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
async function getHeader() {
  const user = await MyAsyncStorage.load(MyAsyncStorage.USER);
  if (user) {
    return {
      headers: {
        ...{
          Authorization: `Bearer ${user.token}`
        },
        ...(_AppStore.default.appId === 'VTPost' ? {
          vtp_token: user.client_token
        } : {
          vtm_token: user.client_token
        })
      }
    };
  }
}
async function getHeaderVTM() {
  const user = await MyAsyncStorage.load(MyAsyncStorage.USER);
  if (user) {
    return {
      headers: {
        'x-access-token': `${user.client_token}`
      }
    };
  }
}
const create = function () {
  let baseURL = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : Endpoint.API_BASE;
  if (_AppStore.default.appId === 'Admin') {
    baseURL = Endpoint.API_BASE_ADMIN;
  }
  const api = _apisauce.default.create({
    baseURL,
    headers: {
      'Content-Type': 'application/json'
    },
    timeout: 30000
  });
  const transform = async response => {
    const {
      config,
      message,
      problem
    } = response;
    if (problem === 'NETWORK_ERROR') {
      _reactNative.Alert.alert('Thông báo', 'Kết nối gián đoạn. Vui lòng kiểm tra lại!', [{
        text: 'Đồng ý',
        onPress: () => {
          _ListChatStore.default.getData({});
          _ChatStore.default.getData({});
        }
      }]);
    } else {
      return response;
    }
  };
  api.addAsyncResponseTransform(transform);
  const apiMultipart = _apisauce.default.create({
    baseURL,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 300000
  });
  apiMultipart.addAsyncResponseTransform(response => async () => {
    const {
      config,
      message,
      problem
    } = response;
    if (problem === 'NETWORK_ERROR') {
      config.retry -= 1;
      const delayRetryRequest = new Promise(resolve => {
        setTimeout(() => {
          resolve();
        }, config.retryDelay || 1000);
      });
      return delayRetryRequest.then(() => _apisauce.default.create(config));
    } else {
      return response;
    }
  });
  const authVTP = data => api.post(Endpoint.AUTH_VTP, data);
  const authVTM = data => api.post(Endpoint.AUTH_VTM, data);
  const getConversations = async data => api.get(Endpoint.CONVERSATION_ME, data, await getHeader());
  const getConversationsAdmin = async data => api.get(Endpoint.CONVERSATION_ADMIN, data, await getHeader());
  const conversationPin = async data => api.post(Endpoint.CONVERSATION_PIN(data.conversation_id), data, await getHeader());
  const conversationUnPin = async data => api.delete(Endpoint.CONVERSATION_PIN(data.conversation_id), data, await getHeader());
  const conversationHide = async data => api.post(Endpoint.CONVERSATION_HIDE(data.conversation_id), data, await getHeader());
  const conversationMessages = async data => api.get(Endpoint.CONVERSATION_MESSAGES(data.conversation_id), data, await getHeader());
  const sendMessage = async data => api.post(Endpoint.SEND_MESSAGE(data.conversation_id), data, await getHeader());
  const uploadFile = async (data, onUploadProgress) => apiMultipart.post(Endpoint.UPLOAD_FILE, data, {
    ...(await getHeader()),
    ...{
      retry: 3,
      onUploadProgress
    }
  });
  const downloadFile = async data => api.get(Endpoint.DOWNLOAD_FILE, data, await getHeader());
  const createConversationVTM = async data => api.post(Endpoint.CREATE_CONVERSATION_WITH_VTM, data, await getHeader());
  const createConversationVTP = async data => api.post(Endpoint.CREATE_CONVERSATION_WITH_VTP, data, await getHeader());
  const onlineState = async data => api.post(Endpoint.ONLINE_STATE, data, await getHeader());
  const orderValidate = async data => api.post(Endpoint.ORDER_VALIDATE, data, await getHeader());
  const getConversationPin = async data => api.get(Endpoint.GET_CONVERSATION_PIN, data, await getHeader());
  const conversationMute = async data => api.post(Endpoint.CONVERSATION_MUTE(data.conversation_id), data, await getHeader());
  const conversationReact = async data => api.post(Endpoint.CONVERSATION_REACT(data.conversation_id, data.message_id), data, await getHeader());
  const createQuickMessage = async data => api.post(Endpoint.QUICK_MESSAGE_CREATE, data, await getHeader());
  const updateQuickMessage = async data => api.put(Endpoint.QUICK_MESSAGE_UPDATE(data._id), data, await getHeader());
  const deleteQuickMessage = async data => api.delete(Endpoint.QUICK_MESSAGE_UPDATE(data._id), data, await getHeader());
  const listQuickMessage = async data => api.get(Endpoint.QUICK_MESSAGE_LIST, data, await getHeader());
  const vtpConversationWithCS = async data => api.post(Endpoint.VTP_CONVERSATION_WITH_CS, data, await getHeader());
  const vtmConversationWithCS = async data => api.post(Endpoint.VTM_CONVERSATION_WITH_CS, data, await getHeader());
  const vtpConversationWithReceiver = async data => api.post(Endpoint.VTP_CREATE_WITH_RECEIVER, data, await getHeader());
  const vtmConversationWithReceiver = async data => api.post(Endpoint.VTM_CREATE_WITH_RECEIVER, data, await getHeader());
  const loginAdmin = async data => api.post(Endpoint.ADMIN_LOGIN, data);
  const loginVTP = async data => api.post(Endpoint.VTP_Login, data);
  const loginVTPClient = async data => api.post(Endpoint.VTP_Login_Client, data);
  const loginVTM = async data => api.post(Endpoint.VTM_Login, data);
  const getConversationDetail = async data => api.get(Endpoint.CONVERSATION(data.conversation_id), null, await getHeader());
  const getConversationAttachments = async data => api.get(Endpoint.CONVERSATION_ATTACHMENTS(data.conversation_id), data, await getHeader());
  const getVTMCustomerByOrder = async data => api.get(Endpoint.VTM_CUSTOMER_BY_ORDER, data, await getHeaderVTM());
  const getStickers = async data => api.get(Endpoint.GET_STICKER, data, await getHeader());
  const getOrderInfoVTM = async data => api.get(Endpoint.GET_ORDER_INFO(data.order_number), null, await getHeader());
  return {
    getOrderInfoVTM,
    getVTMCustomerByOrder,
    vtpConversationWithReceiver,
    vtmConversationWithReceiver,
    getConversationAttachments,
    getConversationDetail,
    vtpConversationWithCS,
    vtmConversationWithCS,
    loginVTP,
    loginVTPClient,
    loginVTM,
    loginAdmin,
    createQuickMessage,
    updateQuickMessage,
    deleteQuickMessage,
    listQuickMessage,
    conversationReact,
    conversationMute,
    getConversationPin,
    orderValidate,
    onlineState,
    createConversationVTM,
    createConversationVTP,
    uploadFile,
    downloadFile,
    authVTP,
    authVTM,
    getConversations,
    getConversationsAdmin,
    conversationPin,
    conversationUnPin,
    conversationHide,
    conversationMessages,
    sendMessage,
    getStickers
  };
};
var _default = {
  create
};
exports.default = _default;
//# sourceMappingURL=index.js.map