import { Network } from '../../../src/clients/constants';
import { IndexerClient } from '../../../src/clients/indexer-client';
import { TEST_ADDRESS } from './constants';

describe('IndexerClient', () => {
  const client = new IndexerClient(Network.testnet().indexerConfig);

  describe('Private Endpoints', () => {
    it('Subaccounts', async () => {
      const response = await client.account.getSubaccounts(TEST_ADDRESS);
      const subaccounts = response.subaccounts;
      expect(subaccounts.size).not.toBeNull();
      const subaccount0 = subaccounts[0];
      const subaccountNumber = subaccount0.subaccountNumber;
      expect(subaccountNumber).not.toBeNull();
    });

    it('Subaccount 0', async () => {
      const response = await client.account.getSubaccount(TEST_ADDRESS, 0);
      const subaccount = response.subaccount;
      expect(subaccount).not.toBeNull();
      const subaccountNumber = subaccount.subaccountNumber;
      expect(subaccountNumber).not.toBeNull();
    });

    it('Asset Positions', async () => {
      const response = await client.account.getSubaccountAssetPositions(TEST_ADDRESS, 0);
      expect(response).not.toBeNull();
      const positions = response.positions;
      expect(positions).not.toBeNull();
      if (positions.length > 0) {
        const position = positions[0];
        expect(position).not.toBeNull();
      }
    });

    it('Perpetual Positions', async () => {
      const response = await client.account.getSubaccountPerpetualPositions(TEST_ADDRESS, 0);
      expect(response).not.toBeNull();
      const positions = response.positions;
      expect(positions).not.toBeNull();
      if (positions.length > 0) {
        const position = positions[0];
        expect(position).not.toBeNull();
      }
    });

    it('Transfers', async () => {
      const response = await client.account.getSubaccountTransfers(TEST_ADDRESS, 0);
      expect(response).not.toBeNull();
      const transfers = response.transfers;
      expect(transfers).not.toBeNull();
      if (transfers.length > 0) {
        const transfer = transfers[0];
        expect(transfer).not.toBeNull();
      }
    });

    it('Transfers ParentSubaccountNumber', async () => {
      const response = await client.account.getParentSubaccountNumberTransfers(TEST_ADDRESS, 0);
      expect(response).not.toBeNull();
      const transfers = response.transfers;
      expect(transfers).not.toBeNull();
      if (transfers.length > 0) {
        const transfer = transfers[0];
        expect(transfer).not.toBeNull();
      }
    });

    it('Transfers Pagination', async () => {
      const response = await client.account.getSubaccountTransfers(
        TEST_ADDRESS,
        0,
        1,
        undefined,
        undefined,
        1,
      );
      expect(response).not.toBeNull();
      const transfers = response.transfers;

      expect(transfers).not.toBeNull();
      if (transfers.length > 0) {
        const transfer = transfers[0];
        expect(transfer).not.toBeNull();

        expect(response.totalResults).toBeGreaterThanOrEqual(1);
      }

      expect(response.pageSize).toStrictEqual(1);
      expect(response.offset).toStrictEqual(0);
    });

    it('Transfers ParentSubaccountNumber Pagination', async () => {
      const response = await client.account.getParentSubaccountNumberTransfers(
        TEST_ADDRESS,
        0,
        1,
        undefined,
        undefined,
        1,
      );
      expect(response).not.toBeNull();
      const transfers = response.transfers;

      expect(transfers).not.toBeNull();
      if (transfers.length > 0) {
        const transfer = transfers[0];
        expect(transfer).not.toBeNull();

        expect(response.totalResults).toBeGreaterThanOrEqual(1);
      }

      expect(response.pageSize).toStrictEqual(1);
      expect(response.offset).toStrictEqual(0);
    });

    it('Orders', async () => {
      const response = await client.account.getSubaccountOrders(TEST_ADDRESS, 0);
      expect(response).not.toBeNull();
      const orders = response;
      expect(orders).not.toBeNull();
      if (orders.length > 0) {
        const order = orders[0];
        expect(order).not.toBeNull();
      }
    });

    it('Fills', async () => {
      const response = await client.account.getSubaccountFills(TEST_ADDRESS, 0);
      expect(response).not.toBeNull();
      const fills = response.fills;
      expect(fills).not.toBeNull();
      if (fills.length > 0) {
        const fill = fills[0];
        expect(fill).not.toBeNull();
      }
    });

    it('Fills ParentSubaccountNumber', async () => {
      const response = await client.account.getParentSubaccountNumberFills(TEST_ADDRESS, 0);
      expect(response).not.toBeNull();
      const fills = response.fills;
      expect(fills).not.toBeNull();
      if (fills.length > 0) {
        const fill = fills[0];
        expect(fill).not.toBeNull();
      }
    });

    it('Fills Pagination', async () => {
      const response = await client.account.getSubaccountFills(
        TEST_ADDRESS,
        0,
        undefined,
        undefined,
        1,
        undefined,
        undefined,
        1,
      );

      expect(response).not.toBeNull();
      const fills = response.fills;
      expect(fills).not.toBeNull();
      if (fills.length > 0) {
        const fill = fills[0];
        expect(fill).not.toBeNull();
        expect(response.totalResults).toBeGreaterThanOrEqual(1);
      }

      expect(response.pageSize).toStrictEqual(1);
      expect(response.offset).toStrictEqual(0);
    });

    it('Fills ParentSubaccountNumber Pagination', async () => {
      const response = await client.account.getParentSubaccountNumberFills(
        TEST_ADDRESS,
        0,
        undefined,
        undefined,
        1,
        undefined,
        undefined,
        1,
      );

      expect(response).not.toBeNull();
      const fills = response.fills;
      expect(fills).not.toBeNull();
      if (fills.length > 0) {
        const fill = fills[0];
        expect(fill).not.toBeNull();
        expect(response.totalResults).toBeGreaterThanOrEqual(1);
      }

      expect(response.pageSize).toStrictEqual(1);
      expect(response.offset).toStrictEqual(0);
    });

    it('Historical PNL', async () => {
      const response = await client.account.getSubaccountHistoricalPNLs(TEST_ADDRESS, 0);
      expect(response).not.toBeNull();
      const historicalPnl = response.historicalPnl;
      expect(historicalPnl).not.toBeNull();
      if (historicalPnl.length > 0) {
        const historicalPnl0 = historicalPnl[0];
        expect(historicalPnl0).not.toBeNull();
      }
    });

    it('Historical PNL Pagination', async () => {
      const response = await client.account.getSubaccountHistoricalPNLs(
        TEST_ADDRESS,
        0,
        undefined,
        undefined,
        undefined,
        undefined,
        1,
        1,
      );
      expect(response).not.toBeNull();
      const historicalPnl = response.historicalPnl;
      expect(historicalPnl).not.toBeNull();
      if (historicalPnl.length > 0) {
        const historicalPnl0 = historicalPnl[0];
        expect(historicalPnl0).not.toBeNull();
        expect(response.totalResults).toBeGreaterThanOrEqual(1);
      }

      expect(response.pageSize).toStrictEqual(1);
      expect(response.offset).toStrictEqual(0);
    });
  });
});
