import { inject, Injectable } from '@angular/core'; import { Maybe } from '../models'; import { ToastService } from './toast.service'; interface INativeBridgeHost { pay?: (amount: number, id: Maybe) => unknown; print?: (payload: INativePrintRequest) => unknown; isEnabled?: () => boolean; getUUID?: () => Maybe; } export interface INativePayRequest { amount: number; id: Maybe; } export interface INativePrintRequest { title: string; items: { label: string; value: string; }[]; } export interface INativeBridgeResult { success: boolean; data?: T; error?: string; } export interface INativeBridgeDeviceInfo { deviceName: string; androidId: string; } @Injectable({ providedIn: 'root' }) export class NativeBridgeService { private readonly toastService = inject(ToastService); private get host(): INativeBridgeHost | undefined { const globalWindow = window as unknown as { NativeBridge?: INativeBridgeHost; }; return globalWindow.NativeBridge; } isEnabled(): boolean { // @ts-ignore return !!window.NativeBridge; // if (window.NativeBridge) { // } // if (!this.host) return false; // const hostEnabled = this.host?.isEnabled; // if (typeof hostEnabled === 'function') { // try { // return !!hostEnabled(); // } catch { // return false; // } // } // return !!environment.enableNativeBridge && !!this.host; } canPay(): boolean { return this.isEnabled() && typeof this.host?.pay === 'function'; } canPrint(): boolean { return this.isEnabled() && typeof this.host?.print === 'function'; } pay(request: INativePayRequest): any { // if (request.amount <= 1_000) { // const errorMessage = 'برای مقادیر زیر ۱۰ هزار ریال، پرداخت ممکن نیست.'; // this.toastService.warn({ text: errorMessage, life: 3000 }); // return { // success: false, // error: errorMessage, // }; // } this.toastService.info({ text: 'در حال پردازش پرداخت...' }); try { // @ts-ignore window.NativeBridge.pay(request.amount, request.id || ''); return { success: true }; } catch (error) { // this.toastService.info({ text: (error as Error).message }); return { success: false, error: (error as Error).message }; } // const fn = window.NativeBridge.pay(123, 'test'); // if (typeof fn !== 'function') { // this.toastService.error({ text: 'متاسفانه پرداخت امکان‌پذیر نیست.', life: 3000 }); // return { success: false, error: 'متاسفانه پرداخت امکان‌پذیر نیست.' }; // } // try { // fn(123, 'test'); // // fn(request.amount, request.id || ''); // return { success: true }; // // return { success: true, data: parsed ?? raw }; // } catch (error) { // this.toastService.info({ text: (error as Error).message, }); // return { success: false, error: (error as Error).message }; // } } print(request: INativePrintRequest[]): INativeBridgeResult { return this.invokePrint(request); } async getDeviceInfo(): Promise> { if (!this.isEnabled()) { return { success: false, error: 'متاسفانه ارتباط با دستگاه برقرار نیست.' }; } try { // @ts-ignore const deviceInfo = await window.NativeBridge.deviceInfo(); return { success: true, data: JSON.parse(deviceInfo) }; } catch (error) { return { success: false, error: (error as Error).message }; } } private invokePrint(payload: INativePrintRequest[]): INativeBridgeResult { if (!this.isEnabled() || !this.canPrint()) { this.toastService.warn({ text: 'متاسفانه ارتباط با چاپگر برقرار نیست.' }); return { success: false, error: 'متاسفانه ارتباط با چاپگر برقرار نیست.' }; } try { this.toastService.info({ text: 'در حال چاپ ...' }); // @ts-ignore window.NativeBridge.print(JSON.stringify(payload)); return { success: true }; } catch (error) { this.toastService.warn({ text: 'متاسفانه ارتباط با چاپگر برقرار نیست.' }); return { success: false, error: (error as Error).message }; } } getNativeDeviceId(): Maybe { return typeof this.host?.getUUID === 'function' ? this.host.getUUID!() : null; } private tryParse(value: unknown): unknown { if (typeof value !== 'string') return value; try { return JSON.parse(value); } catch { return value; } } }