import { visit } from 'unist-util-visit';
// Rehype plugin to add language information to code block props
// Per Eric, Ideally any toggle buttons that can be navigated to would have unique accessible names
// For the purposes of our examples this should suffice, but in a real-world use case they may need to be more unique
export const rehypeCodeBlockToggle = () => (tree) => {
    visit(tree, 'element', (node) => {
        var _a;
        if (node.tagName === 'code' && ((_a = node.properties) === null || _a === void 0 ? void 0 : _a.className)) {
            const className = node.properties.className;
            const languageMatch = className.find((cls) => cls.startsWith('language-'));
            if (languageMatch) {
                const language = languageMatch.replace('language-', '').toUpperCase();
                // Add the language and toggle text as data attributes
                node.properties['data-language'] = language;
                node.properties['data-expanded-text'] = `Show less ${language} code`;
                node.properties['data-collapsed-text'] = `Show more ${language} code`;
            }
        }
    });
};
