"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processCardStackAttributes = processCardStackAttributes;
const html_entities_1 = require("html-entities");
function isTag(child) {
    return child.type === 'tag' && child.name === 'tag';
}
function isTags(child) {
    return child.type === 'tag' && child.name === 'tags';
}
function processCardStackAttributes(node) {
    // Look for a <tags> child element
    if (!node.children) {
        return;
    }
    const tagsNodeIndex = node.children.findIndex(child => isTags(child));
    if (tagsNodeIndex === -1) {
        return;
    }
    const tagsNode = node.children[tagsNodeIndex];
    const tagConfigs = [];
    // Parse each <tag> element
    if (tagsNode.children) {
        tagsNode.children.forEach((child) => {
            var _a;
            if (isTag(child)) {
                if ((_a = child.attribs) === null || _a === void 0 ? void 0 : _a.name) {
                    const config = Object.assign({ name: child.attribs.name }, (child.attribs.color && { color: child.attribs.color }));
                    tagConfigs.push(config);
                }
            }
        });
    }
    // Add tag-configs as a prop if we found any tags
    if (tagConfigs.length > 0) {
        const jsonString = JSON.stringify(tagConfigs);
        // Replace double quotes with HTML entities to avoid SSR warnings
        const escapedJson = (0, html_entities_1.encode)(jsonString);
        node.attribs['data-tag-configs'] = escapedJson;
    }
    // Remove the <tags> node from the DOM tree
    node.children.splice(tagsNodeIndex, 1);
}
//# sourceMappingURL=cardStackProcessor.js.map