/* IMPORT */
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const vscode = require("vscode");
const config_1 = require("../config");
const utils_1 = require("../utils");
const group_1 = require("./items/group");
const todo_1 = require("./items/todo");
const view_1 = require("./view");
/* FILE */
//TODO: Collapse/Expand without rebuilding the tree https://github.com/Microsoft/vscode/issues/54192
class File extends view_1.default {
    constructor() {
        super(...arguments);
        this.id = 'todo.views.1file';
        this.clear = false;
        this.expanded = false;
    }
    getTreeItem(item) {
        if (item.collapsibleState !== vscode.TreeItemCollapsibleState.None) {
            item.collapsibleState = this.expanded ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed;
        }
        return item;
    }
    getChildren(item) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.clear) {
                setTimeout(this.refresh.bind(this), 0);
                return [];
            }
            if (!item) {
                this.textEditor = yield this.getFileEditor();
            }
            if (!this.textEditor)
                return [];
            const items = [], lineNr = item ? item.obj.line.lineNumber : -1;
            utils_1.default.ast.walkChildren(this.textEditor, lineNr, obj => {
                obj.filePath = this.textEditor.uri.fsPath;
                obj.lineNr = obj.line.lineNumber;
                let isGroup = false;
                utils_1.default.ast.walkChildren(this.textEditor, obj.line.lineNumber, () => {
                    isGroup = true;
                    return false;
                });
                const item = isGroup ? new group_1.default(obj, obj.line.text) : new todo_1.default(obj, obj.line.text);
                items.push(item);
            });
            return items;
        });
    }
    getFileEditor() {
        return __awaiter(this, void 0, void 0, function* () {
            const config = config_1.default.get(), { activeTextEditor } = vscode.window, editorPath = activeTextEditor && activeTextEditor.document.uri.fsPath, rootPath = utils_1.default.folder.getRootPath(editorPath);
            if (!rootPath)
                return;
            const projectPath = ((yield utils_1.default.folder.getWrapperPathOf(rootPath, editorPath || rootPath, config.file.name)) || rootPath), todo = utils_1.default.todo.get(projectPath);
            if (!todo)
                return;
            return yield vscode.workspace.openTextDocument(todo.path);
        });
    }
    refresh(clear) {
        this.clear = !!clear;
        super.refresh();
    }
}
/* EXPORT */
exports.default = new File();
//# sourceMappingURL=file.js.map