"use strict";
/*
 * edge
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.newErrorTag = void 0;
const edge_parser_1 = require("edge-parser");
const utils_1 = require("../utils");
/**
 * newError tag to raise exceptions inside your templates. They will point
 * back to the exact line:col in the template
 */
exports.newErrorTag = {
    block: false,
    seekable: true,
    tagName: 'newError',
    noNewLine: true,
    compile(parser, buffer, token) {
        const parsed = (0, utils_1.parseJsArg)(parser, token);
        let message = '';
        let line = token.loc.start.line;
        let col = token.loc.start.col;
        let filename = '$filename';
        if (parsed.type === edge_parser_1.expressions.SequenceExpression) {
            message = parser.utils.stringify(parsed.expressions[0]);
            filename = parsed.expressions[1] ? parser.utils.stringify(parsed.expressions[1]) : '$filename';
            line = parsed.expressions[2]
                ? parser.utils.stringify(parsed.expressions[2])
                : token.loc.start.line;
            col = parsed.expressions[3]
                ? parser.utils.stringify(parsed.expressions[3])
                : token.loc.start.col;
        }
        else {
            message = parser.utils.stringify(parsed);
        }
        /**
         * Raise the exception with the correct filename and the line number
         */
        buffer.writeStatement(`template.newError(${message}, ${filename}, ${line}, ${col})`, token.filename, token.loc.start.line);
    },
};
