"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 });
const vscode = require("vscode");
const index_1 = require("../../utils/index");
const bodyHtml_1 = require("./bodyHtml");
const webviewApi_1 = require("./webviewApi");
const path = require("path");
var template = require('art-template');
class WebViewPlugin {
    constructor(options) {
        this.options = options;
    }
    initWebviewApi(parser) {
        parser.webview.addParentListener('errorNodeReplace', (params) => {
        });
    }
    getStaticFileSrc(parser, filepath) {
        var _a;
        const onDiskPath = vscode.Uri.file(filepath);
        // @ts-ignore
        return (_a = parser === null || parser === void 0 ? void 0 : parser.webview.singleWebview.panel) === null || _a === void 0 ? void 0 : _a.webview.asWebviewUri(onDiskPath);
    }
    apply(parser) {
        return __awaiter(this, void 0, void 0, function* () {
            this.initWebviewApi(parser);
            const { webViewHooks, webview } = parser;
            webview.addWebviewListener('onErrorNode', [
                'errorInfo',
                'this.form = errorInfo;'
            ]);
            webViewHooks.titleHook.tapPromise('title', (title) => __awaiter(this, void 0, void 0, function* () {
                return '我可以配置的哦';
            }));
            webviewApi_1.default(parser);
            webViewHooks.jsHook.tapPromise('jss', (jsArr) => __awaiter(this, void 0, void 0, function* () {
                return jsArr.concat([
                    `<script src="${this.getStaticFileSrc(parser, path.join(__dirname, '../../../static/js/uuidv4.js'))}"></script>`,
                    `<script src="${this.getStaticFileSrc(parser, path.join(__dirname, '../../../static/js/vue.js'))}"></script>`,
                    `<script src="${this.getStaticFileSrc(parser, path.join(__dirname, '../../../static/js/element.js'))}"></script>`,
                    `<style type="type/css">
                    .input {
                        width: 100%;
                    }
                </style>`
                ]);
            }));
            webViewHooks.cssHook.tapPromise('css', (cssArr) => __awaiter(this, void 0, void 0, function* () {
                return cssArr.concat([
                    `<link rel="stylesheet" href="${this.getStaticFileSrc(parser, path.join(__dirname, '../../../static/css/vue.css'))}">`
                ]);
            }));
            webViewHooks.metaHook.tapPromise('meta', (metaArr) => __awaiter(this, void 0, void 0, function* () {
                return metaArr;
            }));
            webViewHooks.headHook.tapPromise('headers', (header) => __awaiter(this, void 0, void 0, function* () {
                var jsArr = yield webViewHooks.jsHook.promise([]);
                var cssArr = yield webViewHooks.cssHook.promise([]);
                var metaArr = yield webViewHooks.metaHook.promise([]);
                return metaArr.concat(jsArr).concat(cssArr);
            }));
            bodyHtml_1.default(parser);
            webViewHooks.htmlHook.tapPromise('htmlFile', (html) => __awaiter(this, void 0, void 0, function* () {
                const headers = yield webViewHooks.headHook.promise([]);
                const title = yield webViewHooks.titleHook.promise('');
                const body = yield webViewHooks.bodyHtmlHook.promise([]);
                var htmlData = yield vscode.workspace.fs.readFile(vscode.Uri.file(path.join(index_1.default.extension.extensionPath, 'static/index.html'))).then(data => {
                    return data.toString();
                });
                return template.render(htmlData, {
                    title: title,
                    headers: headers.join('\n'),
                    body: body.join('\n'),
                }, {});
            }));
        });
    }
}
exports.default = WebViewPlugin;
//# sourceMappingURL=index.js.map