import { aliasWalletConnectQuery } from '../mock-rest';

declare global {
  // eslint-disable-next-line @typescript-eslint/no-namespace
  namespace Cypress {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    interface Chainable<Subject> {
      connectVegaWallet(isMobile?: boolean): void;
    }
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    interface Chainable<Subject> {
      setVegaWallet(): void;
    }
  }
}

export const mockConnectWallet = () => {
  cy.mockWallet((req) => {
    aliasWalletConnectQuery(req, Cypress.env('VEGA_WALLET_API_TOKEN'));
  });
};

export function addVegaWalletConnect() {
  Cypress.Commands.add('connectVegaWallet', (isMobile) => {
    mockConnectWallet();
    cy.highlight(`Connecting Vega Wallet`);
    cy.get(
      `[data-testid=connect-vega-wallet${isMobile ? '-mobile' : ''}]`
    ).click();
    cy.get('[data-testid=connectors-list]')
      .find('[data-testid="connector-jsonRpc"]')
      .click();
    cy.wait('@walletReq');
    cy.get('[data-testid=dialog-content]').should(
      'contain.text',
      'Successfully connected'
    );
    cy.getByTestId('dialog-close').click();
    cy.get('[data-testid=dialog-content]').should('not.exist');
  });
}

export function addSetVegaWallet() {
  Cypress.Commands.add('setVegaWallet', () => {
    cy.window().then((win) => {
      win.localStorage.setItem(
        'vega_wallet_config',
        JSON.stringify({
          token: `VWT ${Cypress.env('VEGA_WALLET_API_TOKEN')}`,
          connector: 'jsonRpc',
          url: 'http://localhost:1789',
        })
      );
    });
  });
}
