import { NativeModules, Platform } from 'react-native';

const LINKING_ERROR =
  `The package 'react-native-tink-sdk' doesn't seem to be linked. Make sure: \n\n` +
  Platform.select({ ios: '- You have run pod install\n', default: '' }) +
  '- You rebuilt the app after installing the package\n' +
  '- You are not using Expo Go\n';

const ReactNativeTink = NativeModules.ReactNativeTink
  ? NativeModules.ReactNativeTink
  : new Proxy(
      {},
      {
        get() {
          throw new Error(LINKING_ERROR);
        },
      }
    );

/**
 * This is the method for connecting bank accounts for continuous access to Business Transactions using Tink Link SDK.
 *
 * @param clientId - client ID retrieved from Tink Console.
 * @param redirectUri - configured in Tink Console - the page to which the end-user is redirected, along with the response parameters, when completing the flow.
 * @param authorizationCode - the authorization code generated using API's.
 * @param market - The market code for which providers should be listed.
 * @param locale - Locale to be used for end-user facing text.
 *
 */

export function connectAccount(
  clientId: string,
  redirectUri: string,
  authorizationCode: string,
  market: string,
  locale: string
): Promise<any> {
  return ReactNativeTink.connectAccount(clientId, redirectUri, authorizationCode, market, locale);
}

/**
 * This is the method for updating consent using Tink Link SDK.
 *
 * @param clientId - client ID retrieved from Tink Console.
 * @param redirectUri - configured in Tink Console - the page to which the end-user is redirected, along with the response parameters, when completing the flow.
 * @param authorizationCode - the authorization code generated using API's.
 * @param credentialsId - The credentials id is obtained either from provider-consents API call or actionable insights(updateCredentials/renewConsent)
 *
 */
export function updateConsent({
  clientId,
  redirectUri,
  authorizationCode,
  credentialID,
}: {
  clientId: string;
  redirectUri: string;
  authorizationCode: string;
  credentialID: string;
}): Promise<any> {
  return ReactNativeTink.updateConsent(clientId, redirectUri, authorizationCode, credentialID);
}

export function displayInsights({
  clientId,
  accessToken,
  userId,
  idHint,
}: {
  clientId: string;
  accessToken: string;
  userId: string;
  idHint: string;
}): Promise<any> {
  return ReactNativeTink.displayInsights(clientId, accessToken, userId, idHint);
}
