"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.PaginatorAtom = void 0;
const tslib_1 = require("tslib");
const protractor_1 = require("protractor");
const atom_1 = require("../../atom");
const popup_atom_1 = require("../popup/popup.atom");
const select_v2_atom_1 = require("../select-v2/select-v2.atom");
class PaginatorAtom extends atom_1.Atom {
    constructor(rootElement) {
        super(rootElement);
        this.popup = atom_1.Atom.findIn(popup_atom_1.PopupAtom, this.getElement());
        this.prevNextClass = "move-icon";
        this.getItemsRange = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this.getStatusText().then((test) => {
                const pages = test.split(" ")[0].split("-");
                const start = parseInt(pages[0], 10);
                const end = parseInt(pages[1], 10);
                return (end - start + 1).toString();
            });
        });
        this.ellipsisLinkClick = (index) => tslib_1.__awaiter(this, void 0, void 0, function* () { return this.ellipsisLink(index).click(); });
        this.isActivePage = (page) => tslib_1.__awaiter(this, void 0, void 0, function* () { return this.activePage().then((activePage) => activePage === page); });
        this.total = rootElement.element(protractor_1.by.className("nui-paginator__total"));
    }
    getStatusText() {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return _super.getElement.call(this)
                .all(protractor_1.by.className("nui-paginator__info"))
                .get(0)
                .getText();
        });
    }
    setItemsPerPage(itemsPerPage) {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const selectV2Atom = atom_1.Atom.findIn(select_v2_atom_1.SelectV2Atom, _super.getElement.call(this));
            return selectV2Atom.select(itemsPerPage.toString());
        });
    }
    pageLinkClick(pageNumber) {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return _super.getElement.call(this)
                .all(protractor_1.by.css(".nui-paginator__list li"))
                .get(pageNumber)
                .click();
        });
    }
    pageLinkVisible(pageNumber) {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return _super.getElement.call(this)
                .all(protractor_1.by.css(".nui-paginator__list li[value='" + pageNumber + "']"))
                .then((elements) => elements.length === 1);
        });
    }
    ellipsedPageLinkClick(pageNumber) {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const allIttems = yield _super.getElement.call(this)
                .all(protractor_1.by.className("nui-paginator__page-cell"))
                .getText();
            const index = allIttems.findIndex((itemText) => itemText === pageNumber.toString());
            return _super.getElement.call(this)
                .all(protractor_1.by.className("nui-paginator__page-cell"))
                .get(index)
                .click();
        });
    }
    ellipsisLink(index) {
        return super
            .getElement()
            .all(protractor_1.by.className("nui-paginator__dots"))
            .get(index);
    }
    ellipsisLinkDisplayed(index) {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return _super.getElement.call(this)
                .all(protractor_1.by.className("nui-paginator__dots"))
                .count()
                .then((count) => count > index);
        });
    }
    prevLink() {
        return super.getElement().all(protractor_1.by.className(this.prevNextClass)).get(0);
    }
    nextLink() {
        return super.getElement().all(protractor_1.by.className(this.prevNextClass)).get(1);
    }
    arePrevNextLinksDisplayed() {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return _super.getElement.call(this)
                .all(protractor_1.by.css(".nui-paginator__list li .move-icon"))
                .then((elements) => elements.length === 2);
        });
    }
    activePage() {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return _super.getElement.call(this)
                .all(protractor_1.by.css(".nui-paginator__list li.active"))
                .get(0)
                .getText()
                .then((text) => parseInt(text, 10));
        });
    }
    pageCount() {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return _super.getElement.call(this)
                .all(protractor_1.by.css(".nui-paginator__list li"))
                .getAttribute("value")
                .then((arrVal) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                const resultArr = yield arrVal.map((value) => parseInt(value, 10));
                return Math.max(...resultArr);
            }));
        });
    }
    ellipsisHasTopClass() {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.ellipsisLinkClick(0);
            const dropdownElement = _super.getElement.call(this)
                .all(protractor_1.by.css(".nui-popup__area"))
                .get(0);
            return yield atom_1.Atom.hasClass(dropdownElement, "nui-popup__area--top");
        });
    }
    itemsDispHasTopClass() {
        const _super = Object.create(null, {
            getElement: { get: () => super.getElement }
        });
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const itemsShownElem = _super.getElement.call(this)
                .all(protractor_1.by.css(".nui-paginator__items-shown"))
                .get(0);
            yield itemsShownElem.click();
            const dropdownElement = _super.getElement.call(this)
                .all(protractor_1.by.css(".nui-popup__area"))
                .get(0);
            return yield atom_1.Atom.hasClass(dropdownElement, "nui-popup__area--top");
        });
    }
    getTotal() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const totalText = yield this.total.getText();
            return Number.parseInt(totalText, 10);
        });
    }
    getPageNumberButton(pageNumber) {
        return this.getElement().element(protractor_1.by.cssContainingText("button.nui-button", pageNumber));
    }
}
exports.PaginatorAtom = PaginatorAtom;
PaginatorAtom.CSS_CLASS = "nui-paginator";
//# sourceMappingURL=paginator.atom.js.map