import { NitroModules } from 'react-native-nitro-modules';
import type { RnFawryPaySdk } from './RnFawryPaySdk.nitro';
import type { CustomerInfo, FawryLanguages, FawryLaunchModel, MerchantInfo } from './types';

const RnFawryPaySdkHybridObject = NitroModules.createHybridObject<RnFawryPaySdk>('RnFawryPaySdk');

export function multiply(a: number, b: number): number {
  return RnFawryPaySdkHybridObject.multiply(a, b);
}

export function startPayment(fawryLaunchModel: FawryLaunchModel): void {
  try {
    RnFawryPaySdkHybridObject.startPayment(fawryLaunchModel);
  } catch (error) {
    console.error('startPayment error:', error);
  }
}

export function openCardsManager(
  baseUrl: string,
  lang: FawryLanguages,
  merchantInfo: MerchantInfo,
  customerInfo: CustomerInfo,
  tokenizationSignature?: string
): void {
  RnFawryPaySdkHybridObject.openCardsManager(baseUrl, lang, merchantInfo, customerInfo, tokenizationSignature);
}

export function addFawryListener(listener: (event: string, payload: string) => void): () => void {
  try {
    return RnFawryPaySdkHybridObject.addListener(listener);
  } catch (error) {
    console.error('addListener error:', error);
    return () => {};
  }
}

export const FawryEvents = {
  EVENT_PAYMENT_COMPLETED: 'EVENT_PAYMENT_COMPLETED',
  EVENT_ON_SUCCESS: 'EVENT_ON_SUCCESS',
  EVENT_ON_FAIL: 'EVENT_ON_FAIL',
  EVENT_COMPLETION_BLOCK: 'EVENT_COMPLETION_BLOCK',
  EVENT_PRE_COMPLETION: 'EVENT_PRE_COMPLETION',
  EVENT_CardManager_FAIL: 'EVENT_CardManager_FAIL',
  EVENT_CARD_ADDED: 'EVENT_CARD_ADDED',
  EVENT_CARD_MANAGER_DISMISSED: 'EVENT_CARD_MANAGER_DISMISSED',
  EVENT_READY: 'EVENT_READY',
};
