"use strict";
// © 2022 SolarWinds Worldwide, LLC. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to
//  deal in the Software without restriction, including without limitation the
//  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
//  sell copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeAtom = void 0;
const tslib_1 = require("tslib");
const protractor_1 = require("protractor");
const atom_1 = require("../atom");
class TreeAtom extends atom_1.Atom {
    constructor() {
        super(...arguments);
        this.root = this.getElement();
        this.getCollapsedExpanders = () => this.root.all(protractor_1.by.css("cdk-nested-tree-node[aria-expanded=false] .nui-tree__header"));
    }
    getNestedNodes() {
        return tslib_1.__awaiter(this, arguments, void 0, function* (el = this.root) {
            return yield this.getAllNestedNodes(el).reduce((acc, item) => tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield item.isDisplayed()) ? acc.concat(item) : acc; }), []);
        });
    }
    isExpanded(el) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return (yield el.getAttribute("aria-expanded")) === "true";
        });
    }
    getNodesByName(name) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this.getAllNestedNodes().filter((item) => tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield item.getText()) === name; }));
        });
    }
    expandAll() {
        return tslib_1.__awaiter(this, arguments, void 0, function* (el = this.root) {
            for (const expander of yield this.getNestedNodes(el)) {
                const expanded = yield this.isExpanded(expander);
                if (!expanded) {
                    yield expander.click();
                    const nestedNodes = yield this.getNestedNodes(expander);
                    if (nestedNodes.length > 0) {
                        yield this.expandAll(expander);
                    }
                }
            }
        });
    }
    expandLevel() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.getCollapsedExpanders().each((i) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                const displayed = yield (i === null || i === void 0 ? void 0 : i.isDisplayed());
                if (displayed) {
                    yield (i === null || i === void 0 ? void 0 : i.click());
                }
            }));
        });
    }
    getBranchCheckboxNodes() {
        return this.root.all(protractor_1.by.css(".branch-control"));
    }
    getLeafCheckboxNodes() {
        return this.root.all(protractor_1.by.css(".leaf-control"));
    }
    getAllHeaders(context = this.root) {
        return context.all(protractor_1.by.css("[cdkTreeNodeToggle]"));
    }
    getAllNestedNodes(context = this.root) {
        return context.all(protractor_1.by.tagName(TreeAtom.NESTED_NODE_TAG));
    }
}
exports.TreeAtom = TreeAtom;
TreeAtom.CSS_CLASS = "cdk-tree";
TreeAtom.NESTED_NODE_TAG = "cdk-nested-tree-node";
//# sourceMappingURL=tree.atom.js.map