const MUSTACHE_REGEX = /\{\{([^}]+)\}\}/g;
// Matches a <span data-variable-token="..."> that may contain one level of
// nested <span>…</span> (the bold category label emitted by TipTap's renderHTML).
const PILL_SPAN_REGEX = /<span[^>]*\bdata-variable-token="([^"]*)"[^>]*>(?:[^<]*(?:<span[^>]*>[^<]*<\/span>)?[^<]*)<\/span>/g;
/** Escapes characters that would break HTML attribute values. */
const escapeAttr = (s) => s.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;');
/**
 * Normalizes legacy variable shapes ({ id, token?, children? }) into
 * the canonical shape ({ key, children: [] }).
 */
export const normalizeVariables = (nodes) => nodes.map(node => {
    const legacy = node;
    const key = (legacy.key ?? legacy.id ?? '');
    const children = Array.isArray(legacy.children)
        ? normalizeVariables(legacy.children)
        : [];
    return { key, label: node.label, children };
});
/**
 * Recursively builds a map of { "parent.child": "Parent - Child" }
 * from the variable tree. Tokens are derived by concatenating keys
 * along the path (e.g. key "collaborator" → key "firstName" = "collaborator.firstName").
 */
export const buildTokenLabelMap = (variables, parentKey, parentLabel) => {
    const map = {};
    const normalized = normalizeVariables(variables);
    for (const node of normalized) {
        const currentKey = parentKey ? `${parentKey}.${node.key}` : node.key;
        const currentLabel = parentLabel
            ? `${parentLabel} - ${node.label}`
            : node.label;
        if (node.children.length === 0) {
            map[currentKey] = currentLabel;
        }
        else {
            Object.assign(map, buildTokenLabelMap(node.children, currentKey, currentLabel));
        }
    }
    return map;
};
export const prepareForEditor = (html, variables) => {
    const labelMap = buildTokenLabelMap(variables);
    return html.replace(MUSTACHE_REGEX, (fullMatch, token) => {
        const label = labelMap[token];
        if (!label)
            return fullMatch;
        return `<span data-variable-token="${escapeAttr(token)}" data-variable-label="${escapeAttr(label)}" class="variable-pill">${escapeAttr(label)}</span>`;
    });
};
export const prepareForBackend = (html) => html.replace(PILL_SPAN_REGEX, (_match, token) => `{{${token}}}`);
