{"version":3,"file":"formatTemplateSource.cjs","sources":["../../src/utils/formatTemplateSource.js"],"sourcesContent":["import repeat from './repeat.js';\nimport padStart from './padStart.js';\n\n/**\n * Converts an expression to its string representation for display.\n * @param {any} expr The expression to convert.\n * @return {string} String representation of the expression.\n * @private\n */\nfunction expressionToString(expr) {\n    if (typeof expr.toString === 'function') {\n        const source = expr.toString();\n        // Keep single line or show first line for multi-line.\n        return '${' + (source.includes('\\n') ? source.split('\\n')[0] + '...' : source) + '}';\n    }\n    return '${' + String(expr) + '}';\n}\n\n/**\n * Formats the template source with line numbers and highlights the specific error expression.\n * @param {Object} source The original template source object with strings and expressions.\n * @param {any} errorExpression The expression that caused the error.\n * @return {string} Formatted template source.\n * @module\n * @private\n */\nexport default function formatTemplateSource(source, errorExpression) {\n    if (!source || !source.strings || !source.expressions || !errorExpression) return '';\n\n    const { strings, expressions } = source;\n\n    // Find the index of the error expression in the expressions array.\n    const expressionIndex = expressions.indexOf(errorExpression);\n    if (expressionIndex === -1) return '';\n\n    // Build the template source and calculate the error position directly.\n    let templateSource = '';\n    let errorPosition = -1;\n\n    for (let i = 0; i < strings.length; i++) {\n        templateSource += strings[i];\n\n        if (i < expressions.length) {\n            // Mark the position before adding the error expression.\n            if (i === expressionIndex) {\n                errorPosition = templateSource.length;\n            }\n            templateSource += expressionToString(expressions[i]);\n        }\n    }\n\n    if (errorPosition === -1) return '';\n\n    // Find the line and column of the error position.\n    const lines = templateSource.split('\\n');\n    const formattedLines = [];\n    const maxLineNumWidth = String(lines.length).length;\n\n    let errorLineNum = -1;\n    let errorStartCol = -1;\n    let charCount = 0;\n\n    for (let i = 0; i < lines.length; i++) {\n        const lineLength = lines[i].length + 1; // +1 for newline character.\n        if (charCount + lineLength > errorPosition) {\n            errorLineNum = i;\n            errorStartCol = errorPosition - charCount;\n            break;\n        }\n        charCount += lineLength;\n    }\n\n    if (errorLineNum === -1) return '';\n\n    // Get the expression string to determine marker length.\n    const expressionStr = expressionToString(errorExpression);\n\n    // Format output with context.\n    const contextStart = Math.max(0, errorLineNum - 2);\n    const contextEnd = Math.min(lines.length - 1, errorLineNum + 2);\n\n    for (let i = contextStart; i <= contextEnd; i++) {\n        const lineNum = i + 1;\n        const lineNumStr = padStart(String(lineNum), maxLineNumWidth, ' ');\n        const isErrorLine = i === errorLineNum;\n        const linePrefix = isErrorLine ? ` ${lineNumStr} > ` : ` ${lineNumStr} | `;\n\n        formattedLines.push(linePrefix + lines[i]);\n\n        // Add pointer on error line.\n        if (isErrorLine) {\n            const markerPos = linePrefix.length + errorStartCol;\n            const markerLen = expressionStr.length;\n            const pointerLine = repeat(' ', markerPos) + repeat('^', markerLen) + ' <-- Error here!';\n            formattedLines.push(pointerLine);\n        }\n    }\n\n    // If error expression is multi-line, show full details.\n    if (typeof errorExpression === 'function') {\n        const fullSource = errorExpression.toString();\n        if (fullSource.match(/\\n/)) {\n            formattedLines.push('');\n            formattedLines.push('     | Expression details:');\n            fullSource.split('\\n').forEach(line => {\n                formattedLines.push('     |     ' + line);\n            });\n        }\n    }\n\n    return formattedLines.join('\\n');\n}\n"],"names":["padStart","repeat"],"mappings":";;;;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS,kBAAkB,CAAC,IAAI,EAAE;AAClC,IAAI,IAAI,OAAO,IAAI,CAAC,QAAQ,KAAK,UAAU,EAAE;AAC7C,QAAQ,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE;AACtC;AACA,QAAQ,OAAO,IAAI,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,MAAM,CAAC,GAAG,GAAG;AAC5F,IAAI;AACJ,IAAI,OAAO,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG;AACpC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAAS,oBAAoB,CAAC,MAAM,EAAE,eAAe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