//
//  ScanbotBarcodeSDKPluginResultDelegate.swift
//  CapacitorPluginScanbotBarcodeScannerSdk
//
//  Created by Marko Jakov on 08.04.24.
//

import Foundation
import Capacitor
import ScanbotBarcodeSDKWrapper

class ScanbotBarcodeSDKPluginResultDelegate: PluginResultDelegate {
  
  private let call: CAPPluginCall
  private let onPromiseHandled: (() -> Void)?

  init(_ call: CAPPluginCall, onPromiseHandled: (() -> Void)? = nil) {
      self.call = call
      self.onPromiseHandled = onPromiseHandled
  }

  public func didResolvePromise(_ result: [String: Any]) {
      call.resolve(result)
      onPromiseHandled?()
  }

  public func didRejectPromise(_ error: [String: Any]) {
      call.reject((error["message"] as? String) ?? "Error", nil, nil, error)
      onPromiseHandled?()
  }
}
