"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = void 0;
const vscode = __importStar(require("vscode-languageserver-types"));
function register() {
    return (pugDoc) => {
        const result = [];
        const docEndPos = pugDoc.pugTextDocument.positionAt(pugDoc.pugTextDocument.getText().length);
        if (pugDoc.ast) {
            visitNode(pugDoc.ast, findMinimalEndLine(docEndPos.line));
        }
        return result;
        function visitNode(node, endLine) {
            if (node.type === 'Block') {
                for (let i = 0; i < node.nodes.length; i++) {
                    const child = node.nodes[i];
                    const next = i + 1 < node.nodes.length ? node.nodes[i + 1] : undefined;
                    visitNode(child, next ? findMinimalEndLine(next.line - 2) : endLine);
                }
            }
            else if (node.type === 'Tag' || node.type === 'BlockComment') {
                const nodeLine = node.line - 1; // one base to zero base
                if (nodeLine !== endLine) {
                    result.push(vscode.FoldingRange.create(nodeLine, endLine, undefined, undefined, node.type === 'BlockComment' ? vscode.FoldingRangeKind.Comment : undefined));
                }
                visitNode(node.block, endLine);
            }
        }
        function findMinimalEndLine(endLine) {
            while (endLine > 0 && getLineText(endLine).trim() === '') {
                endLine--;
            }
            return endLine;
        }
        function getLineText(line) {
            if (line === docEndPos.line) {
                return pugDoc.pugTextDocument.getText({
                    start: { line: line, character: 0 },
                    end: docEndPos,
                });
            }
            const text = pugDoc.pugTextDocument.getText({
                start: { line: line, character: 0 },
                end: { line: line + 1, character: 0 },
            });
            return text.substring(0, text.length - 1);
        }
    };
}
exports.register = register;
//# sourceMappingURL=foldingRanges.js.map