import parseAttrs from 'md-attr-parser';
import { visit } from 'unist-util-visit';
export function attributesTransformer(root) {
    visit(root, 'paragraph', (node, _, parent) => {
        if ('children' in node &&
            node.children.length === 1 &&
            node.children[0].type === 'attrs') {
            const children = parent.children;
            const index = children.indexOf(node);
            children[index] = node.children[0];
        }
    });
    visit(root, (node, _, parent) => node.type === 'paragraph' && parent?.type === 'listItem', (node, _, parent) => {
        const { children } = node;
        const ids = Object.entries(children)
            .filter(([, child]) => child.type === 'attrs')
            .map(([id, node]) => [Number.parseInt(id, 10), node]);
        if (ids.length === 0) {
            return;
        }
        for (const [index, attrNode] of ids) {
            const sibling = children[index - 1];
            if (sibling.type === 'text') {
                const data = parent.data;
                parent.data = {
                    ...data,
                    hProperties: {
                        ...data?.hProperties,
                        ...parseAttrs(attrNode.value).prop,
                    },
                };
                children.splice(index, 1);
            }
        }
    });
    visit(root, 'attrs', (node, index, parent) => {
        if (index == null || parent == null || parent.children.length <= 1) {
            return;
        }
        const { children } = parent;
        const sibling = children.at(index - 1);
        if (!sibling || sibling.type === 'text') {
            parent.data = {
                ...parent.data,
                hProperties: {
                    ...parent.data?.hProperties,
                    ...parseAttrs(node.value).prop,
                },
            };
        }
        else {
            sibling.data = {
                ...sibling.data,
                hProperties: {
                    ...sibling.data?.hProperties,
                    ...parseAttrs(node.value).prop,
                },
            };
        }
        children.splice(index, 1);
    });
}
