function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
    try {
        var info = gen[key](arg);
        var value = info.value;
    } catch (error) {
        reject(error);
        return;
    }
    if (info.done) {
        resolve(value);
    } else {
        Promise.resolve(value).then(_next, _throw);
    }
}
function _async_to_generator(fn) {
    return function() {
        var self = this, args = arguments;
        return new Promise(function(resolve, reject) {
            var gen = fn.apply(self, args);
            function _next(value) {
                asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
            }
            function _throw(err) {
                asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
            }
            _next(undefined);
        });
    };
}
function _ts_generator(thisArg, body) {
    var f, y, t, _ = {
        label: 0,
        sent: function() {
            if (t[0] & 1) throw t[1];
            return t[1];
        },
        trys: [],
        ops: []
    }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
    return d(g, "next", {
        value: verb(0)
    }), d(g, "throw", {
        value: verb(1)
    }), d(g, "return", {
        value: verb(2)
    }), typeof Symbol === "function" && d(g, Symbol.iterator, {
        value: function() {
            return this;
        }
    }), g;
    function verb(n) {
        return function(v) {
            return step([
                n,
                v
            ]);
        };
    }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while(g && (g = 0, op[0] && (_ = 0)), _)try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [
                op[0] & 2,
                t.value
            ];
            switch(op[0]){
                case 0:
                case 1:
                    t = op;
                    break;
                case 4:
                    _.label++;
                    return {
                        value: op[1],
                        done: false
                    };
                case 5:
                    _.label++;
                    y = op[1];
                    op = [
                        0
                    ];
                    continue;
                case 7:
                    op = _.ops.pop();
                    _.trys.pop();
                    continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
                        _ = 0;
                        continue;
                    }
                    if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
                        _.label = op[1];
                        break;
                    }
                    if (op[0] === 6 && _.label < t[1]) {
                        _.label = t[1];
                        t = op;
                        break;
                    }
                    if (t && _.label < t[2]) {
                        _.label = t[2];
                        _.ops.push(op);
                        break;
                    }
                    if (t[2]) _.ops.pop();
                    _.trys.pop();
                    continue;
            }
            op = body.call(thisArg, _);
        } catch (e) {
            op = [
                6,
                e
            ];
            y = 0;
        } finally{
            f = t = 0;
        }
        if (op[0] & 5) throw op[1];
        return {
            value: op[0] ? op[1] : void 0,
            done: true
        };
    }
}
/**
 * @fileoverview 本地预览工具函数
 * 支持在新窗口打开 HTML 或 Markdown（转为 HTML 后）内容
 */ import { markdownToHtml } from "../../../MarkdownEditor/editor/utils/markdownToHtml";
var MARKDOWN_PREVIEW_STYLE = "\n  body {\n    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;\n    line-height: 1.6;\n    color: #24292e;\n    max-width: 900px;\n    margin: 0 auto;\n    padding: 32px 24px;\n    background: #fff;\n  }\n  h1, h2, h3, h4, h5, h6 {\n    border-bottom: 1px solid #eaecef;\n    padding-bottom: 0.3em;\n    margin-top: 24px;\n    margin-bottom: 16px;\n    font-weight: 600;\n    line-height: 1.25;\n  }\n  pre {\n    background: #f6f8fa;\n    border-radius: 6px;\n    padding: 16px;\n    overflow: auto;\n    font-size: 85%;\n    line-height: 1.45;\n  }\n  code {\n    background: rgba(175, 184, 193, 0.2);\n    border-radius: 3px;\n    font-size: 85%;\n    padding: 0.2em 0.4em;\n  }\n  pre code {\n    background: transparent;\n    padding: 0;\n    font-size: inherit;\n  }\n  blockquote {\n    border-left: 4px solid #d0d7de;\n    color: #57606a;\n    margin: 0;\n    padding: 0 1em;\n  }\n  table {\n    border-collapse: collapse;\n    width: 100%;\n  }\n  th, td {\n    border: 1px solid #d0d7de;\n    padding: 6px 13px;\n  }\n  tr:nth-child(even) {\n    background: #f6f8fa;\n  }\n  img {\n    max-width: 100%;\n  }\n  a {\n    color: #0969da;\n    text-decoration: none;\n  }\n  a:hover {\n    text-decoration: underline;\n  }\n  hr {\n    border: none;\n    border-top: 1px solid #d0d7de;\n  }\n";
function buildHtmlDocument(body, title) {
    return '<!DOCTYPE html>\n<html lang="en">\n<head>\n  <meta charset="UTF-8" />\n  <meta name="viewport" content="width=device-width, initial-scale=1.0" />\n  <title>'.concat(title, '</title>\n  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" crossorigin="anonymous" />\n</head>\n<body>\n').concat(body, "\n</body>\n</html>");
}
function openBlobInNewTab(html) {
    var blob = new Blob([
        html
    ], {
        type: 'text/html'
    });
    var url = URL.createObjectURL(blob);
    var win = window.open(url, '_blank');
    if (win) {
        // 在新窗口加载完毕后释放 Blob URL
        win.addEventListener('load', function() {
            return URL.revokeObjectURL(url);
        }, {
            once: true
        });
    } else {
        // 弹窗被拦截时延迟释放
        setTimeout(function() {
            return URL.revokeObjectURL(url);
        }, 10000);
    }
}
/**
 * 在新标签页打开 HTML 内容的本地预览（允许执行 JavaScript）
 */ export function openHtmlLocalPreview(htmlCode) {
    var needsDocumentWrapper = !/<html[\s>]/i.test(htmlCode) && !/<!DOCTYPE/i.test(htmlCode);
    var fullHtml = needsDocumentWrapper ? buildHtmlDocument(htmlCode, 'HTML Preview') : htmlCode;
    openBlobInNewTab(fullHtml);
}
/**
 * 将 Markdown 转换为 HTML 后在新标签页打开本地预览
 */ export function openMarkdownLocalPreview(markdownCode) {
    return _async_to_generator(function() {
        var bodyHtml, fullHtml;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    return [
                        4,
                        markdownToHtml(markdownCode, undefined, {
                            openLinksInNewTab: true
                        })
                    ];
                case 1:
                    bodyHtml = _state.sent();
                    fullHtml = '<!DOCTYPE html>\n<html lang="en">\n<head>\n  <meta charset="UTF-8" />\n  <meta name="viewport" content="width=device-width, initial-scale=1.0" />\n  <title>Markdown Preview</title>\n  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" crossorigin="anonymous" />\n  <style>'.concat(MARKDOWN_PREVIEW_STYLE, "</style>\n</head>\n<body>\n").concat(bodyHtml, "\n</body>\n</html>");
                    openBlobInNewTab(fullHtml);
                    return [
                        2
                    ];
            }
        });
    })();
}
