import apiClient from './client';

export const walletAPI = {
  createNewAccount: async () => {
    const response = await apiClient.get('/new-account/');
    return response.data;
  },

  // server.js: app.post('/session/init', ...) expects { mnemonic, sessionLogin, password, isImport }
  initSession: async (mnemonic, password, usernameOrSessionLogin, isImport = false) => {
    const response = await apiClient.post('/session/init', {
      mnemonic,
      sessionLogin: usernameOrSessionLogin,
      password,
      isImport,
    });
    return response.data;
  },

  // server.js: app.post('/session/get', ...) expects { sessionLogin }
  getSession: async (sessionLogin) => {
    const response = await apiClient.post('/session/get', {
      sessionLogin,
    });
    return response.data;
  },

  // server.js: app.post('/session/activewallet/change', ...) expects { sessionLogin, id? or mnemonic? }
  changeActiveWallet: async (sessionLogin, mnemonic) => {
    const response = await apiClient.post('/session/activewallet/change', {
      sessionLogin,
      mnemonic,
    });
    return response.data;
  },

  getAllWallets: async () => {
    const response = await apiClient.get('/api/wallets/all');
    return response.data;
  },

  deleteWallet: async (walletId) => {
    const response = await apiClient.delete(`/api/wallets/${walletId}`);
    return response.data;
  },

  getTokenVisibility: async (userId) => {
    const response = await apiClient.get(`/api/token/visibility/${userId}`);
    return response.data;
  },

  toggleToken: async (userId, tokenSymbol) => {
    const response = await apiClient.post('/api/token/toggle', {
      userId,
      tokenSymbol,
    });
    return response.data;
  },

  getSessions: async () => {
    const response = await apiClient.get('/api/sessions');
    return response.data;
  },

  deleteWalletFromSession: async (sessionLogin, mnemonic) => {
    const response = await apiClient.post('/api/sessionwallet/delete', {
      sessionLogin,
      mnemonic,
    });
    return response.data;
  },

  executeSwap: async ({ sessionLogin, fromSymbol, toSymbol, amount }) => {
    const response = await apiClient.post('/api/swap/execute', {
      sessionLogin,
      fromSymbol,
      toSymbol,
      amount,
    });
    return response.data;
  },

  getPendingSwaps: async (sessionLogin) => {
    const response = await apiClient.post('/api/swap/pending', {
      sessionLogin,
    });
    return response.data;
  },

  getTokenAddress: async ({ sessionLogin, tokenSymbol, walletId }) => {
    const response = await apiClient.post('/api/token/address', {
      sessionLogin,
      tokenSymbol,
      walletId,
    });
    return response.data;
  },
};

export default walletAPI;
