"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WxTransformLike = exports.EVENT_PREFIX_REGEXP = void 0;
const core_1 = require("@angular-devkit/core");
const transform_base_1 = require("../transform.base");
const wx_container_1 = require("./wx-container");
exports.EVENT_PREFIX_REGEXP = /^(bind|catch|mut-bind|capture-bind|capture-catch)(.*)$/;
class WxTransformLike extends transform_base_1.TemplateTransformBase {
    constructor() {
        super();
        this.seq = ':';
        this.templateInterpolation = ['{{', '}}'];
        this.eventListConvert = (list) => {
            const eventMap = new Map();
            list.forEach((eventName) => {
                const result = this.eventNameConvert(eventName);
                const prefix = core_1.strings.camelize(result.prefix);
                const bindEventName = `${prefix}Event`;
                if (eventMap.has(result.name)) {
                    if (eventMap.get(result.name) === bindEventName) {
                        return;
                    }
                    else {
                        throw new Error(`事件名[${result.name}]解析异常,原绑定${eventMap.get(result.name)},现绑定${bindEventName}`);
                    }
                }
                eventMap.set(result.name, bindEventName);
            });
            return Array.from(eventMap.entries())
                .map(([key, value]) => `${key}="${value}"`)
                .join(' ');
        };
    }
    init() {
        wx_container_1.WxContainer.initWxContainerFactory({
            seq: this.seq,
            directivePrefix: this.directivePrefix,
            eventListConvert: this.eventListConvert,
            templateInterpolation: this.templateInterpolation,
        });
    }
    compile(nodes) {
        const container = new wx_container_1.WxContainer();
        nodes.forEach((node) => {
            container.compileNode(node);
        });
        const result = container.export();
        const metaCollectionGroup = container.exportMetaCollectionGroup();
        const inlineMetaCollection = metaCollectionGroup.$inline;
        delete metaCollectionGroup.$inline;
        return {
            content: `${inlineMetaCollection.templateList
                .map((item) => item.content)
                .join('')}<block ${this.directivePrefix}${this.seq}if="{{hasLoad}}">${result.wxmlTemplate}</block>`,
            useComponentPath: {
                localPath: [...inlineMetaCollection.localPath],
                libraryPath: [...inlineMetaCollection.libraryPath],
            },
            otherMetaGroup: metaCollectionGroup,
        };
    }
    getData() {
        return { directivePrefix: this.directivePrefix };
    }
    eventNameConvert(tagEventMeta) {
        const result = tagEventMeta.match(exports.EVENT_PREFIX_REGEXP);
        let prefix = 'bind';
        let type = tagEventMeta;
        if (result) {
            prefix = result[1];
            type = result[2];
        }
        return {
            prefix,
            type,
            name: `${prefix}:${type}`,
        };
    }
}
exports.WxTransformLike = WxTransformLike;
