import { NativeModules, Platform } from 'react-native';
import { Buffer } from 'buffer';
import { TextDecoder } from 'text-encoding';
import { parseString } from 'react-native-xml2js';
const LINKING_ERROR = `The package 'react-native-cloud-ca' 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 CloudCa = NativeModules.CloudCa ? NativeModules.CloudCa : new Proxy({}, {
  get() {
    throw new Error(LINKING_ERROR);
  }
});
export function sdkSetup(params) {
  const {
    baseUrl
  } = params ?? {
    baseUrl: ''
  };
  const urlValidated = validateUrl(baseUrl);
  return CloudCa.sdkSetup(urlValidated);
}
function validateUrl(params) {
  if (!params.startsWith('http://') && !params.startsWith('https://')) {
    return 'https://' + params;
  }
  return params;
}
export function initData(params) {
  const {
    clientId,
    clientSecret,
    grantType,
    userId,
    baseURL,
    biometricTitle
  } = params;
  if (Platform.OS === 'ios') {
    return CloudCa.initData(clientId, clientSecret, grantType, userId);
  }
  return CloudCa.initData(baseURL, biometricTitle, clientId, clientSecret, grantType, userId);
}

// 4.1 AuthenticateClient
export function authenticateClient() {
  return CloudCa.authenticateClient();
}

// 4.2 AuthenticateUser
export function authenticateUser() {
  return CloudCa.authenticateUser();
}

// 4.3 Verify OTP
export function verifyOTP(params) {
  const {
    otpMail,
    otpSms,
    biometricApiType
  } = params;
  if (Platform.OS === 'ios') {
    return CloudCa.verifyOTP(otpSms, otpMail);
  }
  return CloudCa.verifyOTP(otpSms, otpMail, biometricApiType);
}

// 4.4 Renew Access Token
export function renewAccessToken(params) {
  const {
    refresh_token
  } = params;
  if (Platform.OS === 'ios') {
    return CloudCa.renewAccessToken(refresh_token);
  }
  return CloudCa.renewAccessToken();
}

// 4.5 DeviceRegistration
export function registerDevice(params) {
  const {
    biometricApiType,
    localizedReason
  } = params ?? {};
  if (Platform.OS === 'ios') {
    return CloudCa.registerDevice(localizedReason);
  }
  return CloudCa.registerDevice(biometricApiType);
}

// 4.6 List Registered Devices
export function listRegisteredDevices() {
  return CloudCa.listRegisteredDevices();
}

// 4.7 Delete Device
export function deleteDevice(params) {
  const {
    deviceId
  } = params;
  return CloudCa.deleteDevice(deviceId);
}

// 4.8 Get Pending Authorisation Request
export function getPendingAuthorisationRequest() {
  return CloudCa.getPendingAuthorisationRequest();
}

// 4.9 Authorise a Pending Request
export function authorisationPendingRequest(params) {
  const {
    biometricApiType,
    hashAlgorithm,
    localizedReason,
    request,
    transactionID
  } = params ?? {};
  if (Platform.OS === 'ios') {
    return CloudCa.authorisationPendingRequest(localizedReason, transactionID, request, hashAlgorithm);
  }
  return CloudCa.authorisationPendingRequest(biometricApiType, transactionID, request, hashAlgorithm);
}

// 4.10 Cancel a Pending Authorisation Request
export function cancelPendingRequest(params) {
  const {
    hashAlgorithm,
    request,
    transactionID
  } = params ?? {};
  return CloudCa.cancelPendingRequest(transactionID, request, hashAlgorithm);
}

// 4.11 Users Profile
export function getUserProfile() {
  return CloudCa.getUserProfile();
}

// 4.12 Get Device Registration Settings
export function getDeviceRegistrationSettings() {
  return CloudCa.getDeviceRegistrationSettings();
}

// 4.13 Generate QR Code
export function generateQRCode(params) {
  const {
    clientId,
    format,
    size
  } = params;
  if (Platform.OS === 'ios') {
    return CloudCa.generateQRCode(clientId);
  }
  return CloudCa.generateQRCode(format, size);
}

// 4.14 Verify QR Code
export function verifyQRCode(params) {
  const {
    qrCode
  } = params;
  console.log('params', params);
  return CloudCa.verifyQRCode(qrCode);
}

// 4.15 Register Device for Push Notification
export function registerDeviceForPushNotification(params) {
  const {
    deviceToken
  } = params;
  return CloudCa.registerDeviceForPushNotification(deviceToken);
}

// 4.16 Delete Device for Push Notification
export function deleteDeviceForPushNotification(params) {
  const {
    deviceToken
  } = params;
  return CloudCa.deleteDeviceForPushNotification(deviceToken);
}

// decode base64 from 4.8 response
export function decodeRequestBase64(encodedString) {
  const binaryString = Buffer.from(encodedString, 'base64').toString('binary');
  const decoder = new TextDecoder('utf-8');
  const decodedString = decoder.decode(new Uint8Array(binaryString.length).map((_, i) => binaryString.charCodeAt(i)));
  let jsonData = {};
  const options = {
    explicitArray: false
  };
  parseString(decodedString, options, function (_err, result) {
    jsonData = result;
  });
  return jsonData;
}
export function validateToken() {
  return CloudCa.validateToken();
}
export * from './hooks';
export * from './types';
export * from './screens';
export * from './components';
export * from './context';
//# sourceMappingURL=index.js.map