"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 });
function initBodyCreateJs({ webViewHooks, webview }) {
    webViewHooks.vueHooks.createdHook.tapPromise('webviewListener', (createds) => __awaiter(this, void 0, void 0, function* () {
        const listeners = yield webViewHooks.listenerHook.promise([]);
        return createds.concat([
            [
                `
                var webviewListenerArr = ${JSON.stringify(listeners, null, 4)};
                webviewListenerArr.forEach(item => {
                    window.webviewListener[item.name] = new Function(...item.functionConstructorParams);
                });
                `
            ]
        ]);
    }));
    webViewHooks.vueHooks.createdHook.tapPromise('webviewListeneEvent', (createds) => __awaiter(this, void 0, void 0, function* () {
        return createds.concat([
            [
                'app',
                `
                window.addEventListener('message', (data) => {
                    const { listenerName, id, type, params } = data.data;
                    if (type === 'intl-js-vscode.triggerWebviewListener') {
                        if (window.webviewListener[listenerName]) {
                            window.webviewListener[listenerName].call(app, params, (callbackParams) => {
                                vscode.postMessage({
                                    type: 'intl-js-vscode.parent_callback',
                                    id: id,
                                    params: callbackParams
                                });
                            });
                        }
                    } else if (type === 'intl-js-vscode.webview_callback') {
                        if (window.parentCallbacks[id]) {
                            window.parentCallbacks[id](params);
                        }
                    }
                });
                `
            ]
        ]);
    }));
    webViewHooks.vueHooks.createdHook.tapPromise('ready', (jsArr) => __awaiter(this, void 0, void 0, function* () {
        return jsArr.concat([
            [
                `
                setTimeout(() => {
                    vscode.postMessage({
                        type: 'intl-js-vscode.document_ready',
                    });
                }, 0);
                `
            ]
        ]);
    }));
}
exports.default = initBodyCreateJs;
//# sourceMappingURL=createdHook.js.map