"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.BaseSelectV2Atom = void 0;
const tslib_1 = require("tslib");
const protractor_1 = require("protractor");
const select_v2_option_atom_1 = require("./select-v2-option.atom");
const atom_1 = require("../../atom");
const overlay_atom_1 = require("../overlay/overlay.atom");
class BaseSelectV2Atom extends atom_1.Atom {
    constructor() {
        super(...arguments);
        this.popup = atom_1.Atom.findIn(overlay_atom_1.OverlayAtom, (0, protractor_1.element)(protractor_1.by.tagName("body")));
        this.input = this.getElement().element(protractor_1.by.className("nui-combobox-v2__input"));
        this.click = () => tslib_1.__awaiter(this, void 0, void 0, function* () { return this.getElement().click(); });
    }
    getPopupElement() {
        return this.popup.getElement();
    }
    /**
     * Note: Despite its name, this method will only OPEN the dropdown. To toggle it closed, use this
     * Atom's "click" method.
     */
    toggle() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.getElement().click();
            yield this.waitForPopup();
        });
    }
    getOption(index) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!(yield this.popup.isOpened())) {
                yield this.toggle();
            }
            return atom_1.Atom.findIn(select_v2_option_atom_1.SelectV2OptionAtom, this.popup.getElement(), index);
        });
    }
    getFirstOption() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this.getOption(0);
        });
    }
    getLastOption() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const count = yield atom_1.Atom.findCount(select_v2_option_atom_1.SelectV2OptionAtom, this.getElement());
            return this.getOption(count - 1);
        });
    }
    countOptions() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!(yield this.popup.isOpened())) {
                yield this.toggle();
            }
            return atom_1.Atom.findCount(select_v2_option_atom_1.SelectV2OptionAtom, this.popup.getElement());
        });
    }
    /**
     * Note: This method checks whether ANY 'cdk-overlay-pane' on the document body is present
     * (not just this dropdown instance). Close any other cdk-overlay-pane instances before invoking this
     * method to ensure an accurate return value.
     */
    isOpened() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.waitElementVisible();
            return this.popup.isOpened();
        });
    }
    getActiveItemsCount() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this.popup.getElement().all(protractor_1.by.className("active")).count();
        });
    }
    type(text) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.getElement().click();
            return protractor_1.browser.actions().sendKeys(text).perform();
        });
    }
    isSelectDisabled() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const classAttr = yield this.getElement().getAttribute("class");
            return classAttr.includes("disabled");
        });
    }
    select(title) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!(yield this.popup.isPresent())) {
                yield this.toggle();
            }
            const options = this.popup
                .getElement()
                .all(protractor_1.by.css("nui-select-v2-option"));
            const optionsText = yield options.map((option) => tslib_1.__awaiter(this, void 0, void 0, function* () { return yield (option === null || option === void 0 ? void 0 : option.getText()); }));
            const titleIndex = optionsText.indexOf(title);
            if (titleIndex !== -1) {
                yield (yield this.getOption(titleIndex)).click();
            }
        });
    }
    waitForPopup() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return yield protractor_1.browser.wait(protractor_1.ExpectedConditions.visibilityOf(this.popup.getElement()));
        });
    }
}
exports.BaseSelectV2Atom = BaseSelectV2Atom;
//# sourceMappingURL=base-select-v2.atom.js.map