"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 vscode_1 = require("vscode");
const index_1 = require("../utils/index");
const ParserManager_1 = require("./ParserManager");
class SingleWebview {
    constructor() {
        this.callbackCaches = {};
        this.webviewReady = false;
    }
    static getSingleInstance() {
        if (SingleWebview.instance) {
            return SingleWebview.instance;
        }
        SingleWebview.instance = new SingleWebview();
        return SingleWebview.instance;
    }
    openWebview(parser) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            this.parser = parser;
            this.webviewReady = false;
            if (!this.panel) {
                this.panel = vscode_1.window.createWebviewPanel('transView', '上传到服务器', vscode_1.ViewColumn.Beside, {
                    enableCommandUris: true,
                    enableScripts: true
                });
                this.panel.onDidDispose((webview) => {
                    this.panel = undefined;
                    this.webviewReady = false;
                });
                (_a = this.panel) === null || _a === void 0 ? void 0 : _a.onDidChangeViewState((webview) => {
                    if (webview.webviewPanel.active) {
                        // resolve();
                    }
                });
                const { webview } = this.panel;
                webview.onDidReceiveMessage(this.onMessage.bind(this));
            }
            if (index_1.default.extension) {
                const { webview } = this.panel;
                parser === null || parser === void 0 ? void 0 : parser.webViewHooks.jsHook.tapPromise('globalFilePath', (jsArr) => __awaiter(this, void 0, void 0, function* () {
                    return jsArr.concat([
                        `
                    <script>
                        window.globalParserFilePath = '${parser.filepath}';
                    </script>
                    `
                    ]);
                }));
                webview.html = '';
                const html = yield (parser === null || parser === void 0 ? void 0 : parser.webViewHooks.htmlHook.promise(''));
                webview.html = html;
                yield new Promise(resolve => {
                    var _run = () => {
                        setTimeout(() => {
                            if (!this.webviewReady) {
                                _run();
                            }
                            else {
                                resolve(null);
                            }
                        }, 60);
                    };
                    _run();
                });
            }
            else {
                return Promise.reject();
            }
        });
    }
    sendMessage(params) {
        var _a;
        (_a = this.panel) === null || _a === void 0 ? void 0 : _a.webview.postMessage(params);
    }
    onMessage(data) {
        var _a;
        const { listenerName, id, type, params, filepath } = data;
        if (type === 'intl-js-vscode.triggerParentListener') {
            const parser = ParserManager_1.default.getSingleInstance().caches[filepath];
            (_a = parser === null || parser === void 0 ? void 0 : parser.processListenerHook.get(listenerName)) === null || _a === void 0 ? void 0 : _a.promise(params).then(() => __awaiter(this, void 0, void 0, function* () {
                this.sendMessage({
                    id,
                    type: 'intl-js-vscode.webview_callback',
                    params: {}
                });
            }));
        }
        else if (type === 'intl-js-vscode.parent_callback') {
            if (this.callbackCaches[id]) {
                this.callbackCaches[id](params);
            }
        }
        else if (type === 'intl-js-vscode.document_ready') {
            this.webviewReady = true;
        }
    }
}
exports.default = SingleWebview;
//# sourceMappingURL=SingleWebview.js.map