// Copyright © 2022 Olo Inc. All rights reserved.
// This software is made available under the Olo Pay SDK License (See LICENSE.md file)
import { NativeEventEmitter, NativeModules } from 'react-native';
import useOloPayNativeModule from './hooks/useOloPayNativeModule';

import { version, buildType } from '../package.json';

import type {
  AndroidInitializationOptions,
  iOSInitializationOptions,
  ChangeGooglePayVendorOptions,
  DigitalWalletPaymentMethodResult,
  DigitalWalletPaymentRequestOptions,
  DigitalWalletStatus,
  GooglePayInitializationOptions,
  InitializationStatus,
  IOloPaySDK,
} from './definitions';

const OlopaysdkReactNative = useOloPayNativeModule();

export * from './definitions';
export * from './components/PaymentCardDetailsView';
export * from './components/PaymentCardDetailsForm';
export * from './components/PaymentCardCvvView';

export const OloPayEvents = new NativeEventEmitter(
  NativeModules.OlopaysdkReactNativeEventEmitter
);

export const OloPaySDK: IOloPaySDK = {
  initialize: async function (
    options: AndroidInitializationOptions | iOSInitializationOptions
  ): Promise<void> {
    if (options.freshInstall) {
      console.warn(
        "@olo/pay-react-native: 'OloPayInitializationConfig.freshInstall' is deprecated and is safe to remove. It is no longer used by the Olo Pay SDK and will be removed in a future version."
      );
    }
    await OlopaysdkReactNative.initializeMetadata({ version, buildType });
    return await OlopaysdkReactNative.initializeOloPay(options);
  },
  initializeGooglePay: async function (
    options: GooglePayInitializationOptions
  ): Promise<void> {
    return await OlopaysdkReactNative.initializeGooglePay(options);
  },
  changeGooglePayVendor: async function (
    options: ChangeGooglePayVendorOptions
  ): Promise<void> {
    return await OlopaysdkReactNative.changeGooglePayVendor(options);
  },
  isDigitalWalletReady: async function (): Promise<DigitalWalletStatus> {
    return await OlopaysdkReactNative.isDigitalWalletReady();
  },
  getDigitalWalletPaymentMethod: async function (
    options: DigitalWalletPaymentRequestOptions
  ): Promise<DigitalWalletPaymentMethodResult | undefined> {
    return await OlopaysdkReactNative.getDigitalWalletPaymentMethod(options);
  },
  isInitialized: async function (): Promise<InitializationStatus> {
    return await OlopaysdkReactNative.isOloPayInitialized();
  },
  isDigitalWalletInitialized: async function (): Promise<InitializationStatus> {
    return await OlopaysdkReactNative.isDigitalWalletInitialized();
  },
};
