import UPLogger from './UPLogger';
export const UPWebViewJavascriptBridge = '_WebViewJavascriptBridge';
export const UPWebViewJavascriptBridgeMethodList = ["_handleMessageFromJs"];
export class UPWebViewJsBridge {
    constructor(d18, e18) {
        this.webviewController = d18;
        this.messageHandlers = new Map();
        this.responseCallbacks = new Map();
        this.messageHandler = e18;
        this.uniqueId = 0;
    }
    registerHandler(b18, c18) {
        this.messageHandlers.set(b18, c18);
    }
    evaluateJavascript(a18) {
        this.webviewController.runJavaScript(a18);
    }
    _handleMessageFromJs(p17, q17, r17, s17, t17) {
        try {
            if (q17) {
                let z17 = this.responseCallbacks.get(q17);
                if (z17) {
                    z17(r17);
                    this.responseCallbacks.delete(q17);
                }
            }
            else {
                let v17;
                if (s17) {
                    v17 = (y17) => {
                        this._callbackJs(s17, y17);
                    };
                }
                let w17;
                if (t17) {
                    w17 = this.messageHandlers.get(t17);
                    if (!w17) {
                        UPLogger.error("WVJB Warning: No handler for " + t17);
                        return;
                    }
                }
                else {
                    w17 = this.messageHandler;
                }
                if (w17) {
                    w17(p17, v17);
                }
            }
        }
        catch (u17) {
            UPLogger.error("WebViewJavascriptBridge: WARNING: java handler threw error: " + JSON.stringify(u17));
        }
    }
    callHandler(m17, n17, o17) {
        this._sendData(m17, n17, o17);
    }
    _sendData(h17, i17, j17) {
        let k17 = {};
        if (i17) {
            k17['data'] = i17;
        }
        if (j17) {
            let l17 = 'js_cb_' + (++this.uniqueId);
            this.responseCallbacks.set(l17, j17);
            k17['callbackId'] = l17;
        }
        if (h17) {
            k17['handlerName'] = h17;
        }
        this._dispatchMessage(k17);
    }
    _handleMessageFromNative(g17) {
        this.evaluateJavascript(g17);
    }
    _dispatchMessage(c17) {
        let d17 = JSON.stringify(c17);
        let e17 = this.doubleEscapeString(d17);
        let f17 = `javascript:WebViewJavascriptBridge._handleMessageFromJava('${e17}')`;
        this._handleMessageFromNative(f17);
    }
    doubleEscapeString(r16) {
        let s16 = '';
        try {
            let u16 = new RegExp('\\\\', 'g');
            let v16 = new RegExp('\\\"', 'g');
            let w16 = new RegExp('\\\'', 'g');
            let x16 = new RegExp('\\\n', 'g');
            let y16 = new RegExp('\\\r', 'g');
            let z16 = new RegExp('\\\f', 'g');
            let a17 = new RegExp('\\\u2028', 'g');
            let b17 = new RegExp('\\\u2029', 'g');
            r16 = r16.replace(u16, '\\\\');
            r16 = r16.replace(v16, '\\\"');
            r16 = r16.replace(w16, '\\\'');
            r16 = r16.replace(x16, '\\n');
            r16 = r16.replace(y16, '\\r');
            r16 = r16.replace(z16, '\\f');
            r16 = r16.replace(a17, '\\u2028');
            r16 = r16.replace(b17, '\\u2029');
            s16 = r16;
        }
        catch (t16) {
            UPLogger.error(JSON.stringify(t16));
        }
        return s16;
    }
    _callbackJs(o16, p16) {
        let q16 = {};
        if (o16) {
            q16['responseId'] = o16;
        }
        if (p16) {
            q16['responseData'] = p16;
        }
        this._dispatchMessage(q16);
    }
}
