"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
var uuid = require('node-uuid');
const SingleWebview_1 = require("./SingleWebview");
class SidebarWebview {
    constructor(parser) {
        this.parser = parser;
        this.singleWebview = SingleWebview_1.default.getSingleInstance();
    }
    open() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.singleWebview.openWebview(this.parser);
        });
    }
    triggerWebviewListener(method, params, callback) {
        const id = uuid();
        this.sendMessage({
            id: id,
            listenerName: method,
            type: 'intl-js-vscode.triggerWebviewListener',
            params: params,
        });
        this.singleWebview.callbackCaches[id] = callback;
    }
    sendErrorNode(errorNode) {
        this.triggerWebviewListener('onErrorNode', errorNode);
    }
    addWebviewListener(name, functionConstructorParams) {
        var _a;
        (_a = this.parser) === null || _a === void 0 ? void 0 : _a.webViewHooks.listenerHook.tapPromise(name, (listeners) => __awaiter(this, void 0, void 0, function* () {
            return listeners.concat([
                {
                    name,
                    functionConstructorParams
                }
            ]);
        }));
    }
    addParentListener(name, func) {
        this.parser.processListenerHook.for(name).tapPromise(name, (...args) => __awaiter(this, void 0, void 0, function* () {
            func(...args);
        }));
    }
    sendMessage(params) {
        var _a;
        (_a = this.singleWebview.panel) === null || _a === void 0 ? void 0 : _a.webview.postMessage(Object.assign({ filepath: this.parser.filepath }, params));
    }
}
exports.default = SidebarWebview;
//# sourceMappingURL=WebView.js.map