/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types -- ESLint fixer API callback signatures. */
import { arrayFirst, arrayJoin, arrayLast } from "ts-extras";
import { ruleCreator } from "../_internal/rule-creator.js";
const commentText = (comment) => comment.value.trim();
const buildReplacement = (sourceCode, comments) => arrayJoin(comments
    .map((comment) => sourceCode.getText(comment))
    // eslint-disable-next-line unicorn/no-array-sort -- Node >=16.0 support baseline
    .sort((a, b) => a.localeCompare(b)), "\n");
/**
 * Enforce alphabetical ordering of top-of-file comments.
 */
const rule = ruleCreator({
    create: (context) => ({
        Program: (node) => {
            const firstNode = arrayFirst(node.body);
            if (firstNode === undefined) {
                return;
            }
            const comments = context.sourceCode
                .getCommentsBefore(firstNode)
                .filter((comment) => comment.loc.end.line < firstNode.loc.start.line);
            if (comments.length < 2) {
                return;
            }
            const firstComment = arrayFirst(comments);
            if (firstComment === undefined) {
                return;
            }
            const lastComment = arrayLast(comments) ?? firstComment;
            // eslint-disable-next-line unicorn/no-array-sort -- Node >=16.0 support baseline
            const sorted = [...comments].sort((a, b) => commentText(a).localeCompare(commentText(b)));
            const isSorted = comments.every((comment, index) => comment === sorted[index]);
            if (isSorted) {
                return;
            }
            context.report({
                fix: (fixer) => fixer.replaceTextRange([arrayFirst(firstComment.range), lastComment.range[1]], buildReplacement(context.sourceCode, comments)),
                messageId: "incorrectSorting",
                node: firstComment,
            });
        },
    }),
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "enforce alphabetical ordering of top-of-file comments.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/sort-top-comments",
        },
        fixable: "code",
        hasSuggestions: false,
        messages: {
            incorrectSorting: "Top-level comments should be sorted alphabetically.",
        },
        schema: [],
        type: "suggestion",
    },
    name: "sort-top-comments",
});
export default rule;
/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types -- Re-enable after file-scoped fixer callback implementations. */
//# sourceMappingURL=sort-top-comments.js.map