// © 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.
import { Atom } from "../../atom";
import { OverlayAtom } from "../overlay/overlay.atom";
import { SelectV2Atom } from "../select-v2/select-v2.atom";
export class PaginatorAtom extends Atom {
    static { this.CSS_CLASS = "nui-paginator"; }
    get popup() {
        return Atom.findIn(OverlayAtom);
    }
    get select() {
        return Atom.findIn(SelectV2Atom, this.getLocator());
    }
    get total() {
        return this.getLocator().locator(".nui-paginator__total");
    }
    get status() {
        return this.getLocator().locator(".nui-paginator__info");
    }
    async setItemsPerPage(itemsPerPage) {
        await this.select.select(`${itemsPerPage}`);
    }
    async getItemsRange() {
        const statusText = await this.status.textContent();
        if (!statusText) {
            return "";
        }
        const match = statusText.match(/(\d+)-(\d+) of \d+/);
        if (!match) {
            return "";
        }
        const start = parseInt(match[1], 10);
        const end = parseInt(match[2], 10);
        return (end - start + 1).toString();
    }
    async pageLinkClick(pageNumber) {
        const li = this.getLocator()
            .locator(".nui-paginator__list li")
            .nth(pageNumber);
        await li.click();
    }
    async pageLinkVisible(pageNumber) {
        const li = this.getLocator().locator(`.nui-paginator__list li[value='${pageNumber}']`);
        return await li.isVisible();
    }
    async ellipsedPageLinkClick(pageNumber) {
        const page = this.getLocator()
            .locator(".nui-paginator__page-cell")
            .filter({ hasText: String(pageNumber) });
        await page.click();
    }
    ellipsisLink(index) {
        return this.getLocator().locator(".nui-paginator__dots").nth(index);
    }
    async ellipsisLinkClick(index) {
        await this.ellipsisLink(index).click();
    }
    async ellipsisLinkDisplayed(index) {
        const dots = this.getLocator().locator(".nui-paginator__dots");
        return (await dots.count()) > index;
    }
    prevLink() {
        return this.getLocator().locator(".move-icon").first();
    }
    nextLink() {
        return this.getLocator().locator(".move-icon").last();
    }
    async arePrevNextLinksDisplayed() {
        const icons = this.getLocator().locator(".nui-paginator__list li .move-icon");
        return (await icons.count()) === 2;
    }
    async activePage() {
        const activeLi = this.getLocator()
            .locator(".nui-paginator__list li.active")
            .first();
        const text = await activeLi.textContent();
        return text ? parseInt(text, 10) : -1;
    }
    async isActivePage(page) {
        return (await this.activePage()) === page;
    }
    async pageCount() {
        const lis = this.getLocator().locator(".nui-paginator__list li");
        const count = await lis.count();
        let max = 0;
        for (let i = 0; i < count; i++) {
            const value = await lis.nth(i).getAttribute("value");
            if (value) {
                const num = parseInt(value, 10);
                if (num > max) {
                    max = num;
                }
            }
        }
        return max;
    }
    async ellipsisHasTopClass() {
        await this.ellipsisLinkClick(0);
        const dropdown = this.getLocator().locator(".nui-popup__area").first();
        const classList = await dropdown.getAttribute("class");
        return classList?.includes("nui-popup__area--top") ?? false;
    }
    async itemsDispHasTopClass() {
        const itemsShownElem = this.getLocator()
            .locator(".nui-paginator__items-shown")
            .first();
        await itemsShownElem.click();
        const dropdown = this.getLocator().locator(".nui-popup__area").first();
        const classList = await dropdown.getAttribute("class");
        return classList?.includes("nui-popup__area--top") ?? false;
    }
    async getTotal() {
        const totalText = await this.total.textContent();
        return totalText ? parseInt(totalText, 10) : 0;
    }
    getPageNumberButton(pageNumber) {
        return this.getLocator().locator(`button.nui-button:has-text('${pageNumber}')`);
    }
}
//# sourceMappingURL=paginator.atom.js.map