export default class TreeViewRequest {

    public key: string;
    public children: TreeViewRequest[];
    public displayName: string;
    expand: boolean;

    constructor(
        key: string,
        children: TreeViewRequest[],
        expand: boolean = false,
    ) {
        if (key.startsWith("Page")) {
            this.displayName = "Page " + key.slice(4);
        } else {
            this.displayName = key;
        }
        this.key = key;
        this.children = children;
        this.expand = expand;
    }

    static fromPageCount(pageCount: number) {
        let roots = [];
        for (let i = 0; i < pageCount; i++) {
            roots.push(new TreeViewRequest("Page" + (i + 1), []));
        }
        return roots;
    }

    static TRAILER = new TreeViewRequest("Trailer", [new TreeViewRequest("Root", [], true)], true);

    public clone(): TreeViewRequest {
        return new TreeViewRequest(this.key, this.children.map(child => child.clone()), this.expand);
    }

    public getChild(key: string) {
        return this.children.find(child => child.key === key);
    }

    public addChild(key: string) {
        this.expand = true;
        let child = new TreeViewRequest(key, [], true)
        this.children.push(child);
        return child;
    }

    public removeChild(key: string) {
        this.children = this.children.filter(child => child.key !== key);
    }

    public setExpand(expand: boolean) {
        this.expand = expand;
    }
}
