//
//  ResultDelegate.swift
//  Plugin
//
//  Copyright © 2024 Scanbot SDK GmbH. All rights reserved.
//

import Capacitor
import Foundation
import ScanbotSDKNativeWrapper

public class ResultDelegate: 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?()
    }
}
