import stringWidth from 'string-width';
import { lintRule } from 'unified-lint-rule';
import { visit } from 'unist-util-visit';
import { visitParents } from 'unist-util-visit-parents';
import { extractTextAndId } from "../shared/helpers.js";
const MAX_LENGTH = 40;
export const maximumLinkContentLength = lintRule('doom-lint:maximum-link-content-length', (root, vfile) => {
    visit(root, ['link', 'lintReference'], (node_) => {
        const node = node_;
        visitParents(node, 'text', (text, parents) => {
            let value = text.value;
            try {
                new URL(value);
                return;
            }
            catch {
                ;
                [value] = extractTextAndId(value);
            }
            const length = stringWidth(value);
            if (length > MAX_LENGTH) {
                vfile.message(`Link content length \`${length}\` of \`${value}\` is too long, maximum is ${MAX_LENGTH} characters, please shorten it`, {
                    ancestors: [...parents, text],
                    place: text.position,
                });
            }
        });
    });
});
