"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.prePaserCurlyBracket = exports.preDisappearAnnotation = exports._deOperationGtLt = exports._operationMustache = exports.preParseMustache = exports.preParseSpecialAttr = exports._operationGtLt = exports.preParseGtLt = void 0;
// 处理 {{}} 里面的 >  < ==>
const preParseGtLt = (content) => {
    const reg = /{{([\s\S]*?)}}/g;
    return content.replace(reg, (match) => {
        return (0, exports._operationGtLt)(match);
    });
};
exports.preParseGtLt = preParseGtLt;
const _operationGtLt = (content) => {
    const gtltReg = />|</g;
    return content.replace(gtltReg, (match) => {
        if (match === '>') {
            return '_cml_gt_lmc_';
        }
        if (match === '<') {
            return '_cml_lt_lmc_';
        }
        return match;
    });
};
exports._operationGtLt = _operationGtLt;
const preParseSpecialAttr = (content) => {
    let newContent;
    newContent = content.replace(/\s\:([\S]*?)\=/g, (match, key) => {
        return ` v-bind:${key}=`;
    });
    newContent = newContent.replace(/\s\@([\S]*?)\=/g, (match, key) => {
        return ` v-on:${key}=`;
    });
    return newContent;
};
exports.preParseSpecialAttr = preParseSpecialAttr;
// 预处理 标签内的 {{item.id}} 这种语法jsx无法识别，转化为 {item.id}
const preParseMustache = (content) => {
    const reg = />([\s\S]*?)<[a-zA-Z\/\-_]+?/g;
    return content.replace(reg, (match, key) => {
        return (0, exports._operationMustache)(match);
    });
};
exports.preParseMustache = preParseMustache;
// 模块内置方法
// 这里主要处理1  >{{}}< 双花括号之间的 ==> {}, 因为jsx无法识别 {{}}
// 2 同时将 {{}}内的 _cml_gt_lmc_  _cml_lt_lmc_ 复原  < >
const _operationMustache = (content) => {
    const mustacheReg = /{{([\s\S]*?)}}/g;
    return content.replace(mustacheReg, (match, key) => {
        const newKey = (0, exports._deOperationGtLt)(key);
        return `{${newKey}}`;
    });
};
exports._operationMustache = _operationMustache;
const _deOperationGtLt = (content) => {
    const deGtLtReg = /_cml_gt_lmc_|_cml_lt_lmc_/g;
    return content.replace(deGtLtReg, (match) => {
        if (match === '_cml_gt_lmc_') {
            return '>';
        }
        if (match === '_cml_lt_lmc_') {
            return '<';
        }
        return match;
    });
};
exports._deOperationGtLt = _deOperationGtLt;
const preDisappearAnnotation = (content) => {
    const annotionReg = /<!--[\s\S]*?-->/g;
    return content.replace(annotionReg, (match) => {
        return '';
    });
};
exports.preDisappearAnnotation = preDisappearAnnotation;
/**
 * // 预处理，JSX会执行text内容里的花括号，转换成HTML character codes, 注意要过滤掉{{}}
 * @param {string} content - dsl code
 * @returns
 */
const prePaserCurlyBracket = (code) => {
    const mustacheReg = /{{([\s\S]*?)}}/;
    const textReg = /<text [\s\S]*?>((.|\n)*?)<\/text>/g;
    const contentRegExp = />((.|\n)*?)<\//g;
    const wrapChar = /<br \/>/g;
    const mappingList = [
        {
            glyph: ';',
            code: '\u003B'
        },
        {
            glyph: '&',
            code: '\u0026'
        },
        {
            glyph: /{/g,
            code: '&#123;',
        },
        {
            glyph: /}/g,
            code: '&#125;',
        },
        {
            glyph: /</g,
            code: '&lt;'
        },
        {
            glyph: />/g,
            code: '&gt;'
        },
        {
            glyph: '/',
            code: '&#47;'
        },
        {
            glyph: '"',
            code: '&quot;'
        },
        {
            glyph: "'",
            code: '&apos;'
        },
        {
            glyph: '|',
            code: '&#124;'
        },
        {
            glyph: '~',
            code: '&#126;'
        },
    ];
    return code.replace(textReg, (match) => {
        // 过滤掉{{}}
        if (mustacheReg.test(match)) {
            return match;
        }
        return match.replace(contentRegExp, (content) => {
            let replacedContent = content.substring(1, content.length - 2); // 去掉前面的>和最后的</，只保留内容部分
            replacedContent = replacedContent.replace(wrapChar, 'wrapChar'); // 支持<br />不被转义
            mappingList.forEach((item) => {
                replacedContent = replacedContent.replace(item.glyph, item.code);
            });
            replacedContent = replacedContent.replace(/wrapChar/g, '<br />');
            return `>${replacedContent}<\/`;
        });
    });
};
exports.prePaserCurlyBracket = prePaserCurlyBracket;
