/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types -- ESLint fixer API callback signatures. */
import { arrayAt, arrayFirst, arrayJoin, isDefined, isPresent, } from "ts-extras";
import { ruleCreator } from "../_internal/rule-creator.js";
const getSortableValue = (element) => {
    if (element.type === "Literal") {
        return String(element.value);
    }
    return undefined;
};
const buildFix = (sourceCode, node, sorted) => (fixer) => {
    const [first, last] = [
        arrayFirst(node.elements),
        arrayAt(node.elements, -1),
    ];
    if (!isPresent(first) || !isPresent(last)) {
        return fixer.replaceText(node, sourceCode.getText(node));
    }
    return fixer.replaceTextRange([arrayFirst(first.range), last.range[1]], arrayJoin(sorted.map((element) => sourceCode.getText(element)), ", "));
};
/**
 * Enforce alphabetical sorting for literal array elements.
 */
const rule = ruleCreator({
    create: (context) => ({
        ArrayExpression: (node) => {
            let elements = [];
            for (const element of node.elements) {
                if (element !== null) {
                    elements = [...elements, element];
                }
            }
            if (elements.length < 2) {
                return;
            }
            const sortKeys = elements.map((element) => getSortableValue(element));
            if (sortKeys.some((sortKey) => !isDefined(sortKey))) {
                return;
            }
            // eslint-disable-next-line unicorn/no-array-sort -- Node >=16.0 support baseline
            const sorted = [...elements].sort((a, b) => (getSortableValue(a) ?? "").localeCompare(getSortableValue(b) ?? ""));
            const unchanged = elements.every((element, index) => element === sorted[index]);
            if (unchanged) {
                return;
            }
            context.report({
                fix: buildFix(context.sourceCode, node, sorted),
                messageId: "incorrectSorting",
                node,
            });
        },
    }),
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "enforce alphabetical sorting for literal array elements.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/sort-array",
        },
        fixable: "code",
        hasSuggestions: false,
        messages: {
            incorrectSorting: "Array literal elements should be sorted alphabetically.",
        },
        schema: [],
        type: "suggestion",
    },
    name: "sort-array",
});
export default rule;
/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types -- Re-enable after file-scoped fixer callback implementations. */
//# sourceMappingURL=sort-array.js.map