import { arrayFirst, arrayJoin, arrayLast, isEmpty, stringSplit, } from "ts-extras";
import { ruleCreator } from "../_internal/rule-creator.js";
const isMultilineTemplate = (node) => node.loc.start.line !== node.loc.end.line;
const hasExpectedBoundaryNewlines = (node) => {
    const first = arrayFirst(node.quasis)?.value.raw ?? "";
    const last = arrayLast(node.quasis)?.value.raw ?? "";
    return first.startsWith("\n") && last.endsWith("\n");
};
const normalizeTemplate = (sourceText) => {
    const lines = stringSplit(sourceText.replaceAll(/\r\n?/gu, "\n"), "\n");
    const contentLines = lines.slice(1, -1);
    const indents = contentLines
        .filter((line) => line.trim().length > 0)
        .map((line) => /^\s*/u.exec(line)?.[0].length ?? 0);
    const minIndent = isEmpty(indents) ? 0 : Math.min(...indents);
    const normalizedContent = arrayJoin(contentLines
        .map((line) => line.length >= minIndent ? line.slice(minIndent) : line)
        .map((line) => `  ${line}`), "\n");
    const lastLine = arrayLast(lines) ?? "";
    return arrayJoin([
        arrayFirst(lines),
        normalizedContent,
        lastLine,
    ], "\n");
};
const buildFix = (sourceCode, node) => (fixer) => {
    const text = sourceCode.getText(node);
    return fixer.replaceText(node, normalizeTemplate(text));
};
/**
 * Enforce newline boundary formatting for multiline template literals.
 */
const rule = ruleCreator({
    create: (context) => ({
        TemplateLiteral: (node) => {
            if (!isMultilineTemplate(node) ||
                hasExpectedBoundaryNewlines(node)) {
                return;
            }
            context.report({
                fix: buildFix(context.sourceCode, node),
                messageId: "invalidFormat",
                node,
            });
        },
    }),
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "enforce newline boundary formatting for multiline template literals.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/template-literal-format",
        },
        fixable: "code",
        hasSuggestions: false,
        messages: {
            invalidFormat: "Multiline template literals must start and end on their own lines.",
        },
        schema: [],
        type: "layout",
    },
    name: "template-literal-format",
});
export default rule;
//# sourceMappingURL=template-literal-format.js.map