import { __awaiter } from 'tslib';
import { element, by, browser, ExpectedConditions, protractor } from 'protractor';

class AlArchipeligo17PageObject {
    constructor() {
        this.archipeligo17UserMenu = element(by.css('al-archipeligo17-user-menu'));
        this.accountSelector = this.archipeligo17UserMenu.element(by.css('div.entity-wrapper.textual-label div.user-menu-block'));
        this.ellipsisUserMenu = this.archipeligo17UserMenu.element(by.css('div.stacked-vertical i'));
    }
    /**
     * It allows changing the account by passing a string with the id or the name of the account,
     * if there is more than one account that matches the string, the first one will be selected.
     */
    changeAccount(account) {
        return __awaiter(this, void 0, void 0, function* () {
            yield browser.wait(ExpectedConditions.visibilityOf(this.accountSelector));
            yield this.accountSelector.click();
            const accountInput = this.archipeligo17UserMenu.element(by.css('p-dropdown input.ui-dropdown-label'));
            yield accountInput.sendKeys(account);
            const dropdownPanel = this.archipeligo17UserMenu.element(by.css('p-dropdown div.ui-dropdown-panel'));
            yield browser.wait(ExpectedConditions.visibilityOf(dropdownPanel));
            const elmAccount = dropdownPanel.element(by.css('p-dropdownitem:first-child li'));
            yield browser.wait(ExpectedConditions.presenceOf(elmAccount));
            yield elmAccount.click();
            yield browser.sleep(5000);
        });
    }
    goToUserOption(option) {
        return __awaiter(this, void 0, void 0, function* () {
            yield browser.wait(ExpectedConditions.presenceOf(this.ellipsisUserMenu));
            yield this.ellipsisUserMenu.click();
            const items = this.archipeligo17UserMenu.all(by.xpath(`//p-menu[contains(@class, 'elipsis-menu')]//li[not(contains(@class, 'ui-helper-hidden'))]`));
            yield browser.wait(ExpectedConditions.presenceOf(items.get(0)));
            const elem = items.filter((item) => __awaiter(this, void 0, void 0, function* () { return (yield item.getText()) === option; }));
            yield elem.get(0).click().then(() => browser.sleep(500));
        });
    }
}

class AlArchipeligo19NavHeaderPageObject {
    constructor() {
        this.archipeligo19Header = element(by.css('al-archipeligo19-app-header'));
        this.archipeligoCustomerSelector = this.archipeligo19Header.element(by.css('.al-account-selector'));
        this.menuToggle = element(by.className('menu-toggle'));
    }
    toggleMenu() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.menuToggle.click();
        });
    }
    /**
     * It allows changing the account by passing a string with the id or the name of the account,
     * if there is more than one account that matches the string, the first one will be selected.
     */
    changeAccount(account) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.archipeligoCustomerSelector.click();
            const customerSearchInput = this.archipeligoCustomerSelector.element(by.css('.ng-dropdown-panel input'));
            yield browser.wait(ExpectedConditions.presenceOf(customerSearchInput));
            yield customerSearchInput.sendKeys(account);
            yield customerSearchInput.sendKeys(protractor.Key.ENTER);
        });
    }
}

function waitForPresenceOfNumberOfElements(elementArrayFinder, numberOfElements) {
    return browser.wait(() => {
        return elementArrayFinder.count().then((actualCount) => {
            return actualCount === numberOfElements;
        });
    }, 60000, `Could not find exactly ${numberOfElements} elements on the page`);
}
function waitForPresenceOfMultiple(elementArrayFinder, minNumber = 0) {
    return browser.wait(() => {
        return elementArrayFinder.count().then((actualCount) => {
            return actualCount > minNumber;
        });
    }, 60000, 'Could not find any elements on the page');
}

class AlArchipeligo19SidenavPageObject {
    constructor() {
        this.sideNavContainer = element(by.className('sidenav-container'));
        this.EC = protractor.ExpectedConditions;
        this.navHeader = new AlArchipeligo19NavHeaderPageObject();
    }
    clickNavItem(parentItemIconName, menuItemCaption) {
        return __awaiter(this, void 0, void 0, function* () {
            yield browser.wait(this.EC.visibilityOf(this.sideNavContainer));
            const iconEl = element(by.cssContainingText('i', parentItemIconName));
            const parentEl = iconEl.element(by.xpath('..')); // I hate that im having to use xpath here :-(
            yield parentEl.click();
            if (menuItemCaption) {
                const childItem = element(by.cssContainingText('a', menuItemCaption));
                yield browser.wait(this.EC.visibilityOf(childItem));
                yield element(by.cssContainingText('a', menuItemCaption)).click();
            }
        });
    }
    /**
    *  Expands and clicks on a given sidenav menu item
    *  Note - for the Dashboards menu item just provide the first parentItemIconName parameter
    *
    *  @param {string} parentItemIconName The icon name for the parent menu item.
    *  @param {string} menuItemCaption The caption of the menu item to be clicked on (optional).
    *
    */
    expandMenuAndNavigateToItem(parentItemIconName, menuItemCaption) {
        return __awaiter(this, void 0, void 0, function* () {
            yield browser.wait(this.EC.visibilityOf(this.navHeader.menuToggle));
            yield this.navHeader.toggleMenu();
            yield browser.wait(waitForPresenceOfMultiple(element.all(by.className('menu-item-parent'))));
            yield this.clickNavItem(parentItemIconName, menuItemCaption);
        });
    }
}

/**
 * Generated bundle index. Do not edit.
 */

export { AlArchipeligo17PageObject, AlArchipeligo19NavHeaderPageObject, AlArchipeligo19SidenavPageObject };
//# sourceMappingURL=al-ng-navigation-components-protractor.js.map
