"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.Atom = void 0;
const tslib_1 = require("tslib");
const protractor_1 = require("protractor");
const EC = protractor_1.protractor.ExpectedConditions;
const byCssWithRoot = (selector) => {
    if (!Object.hasOwnProperty.call(protractor_1.by, "cssWithRoot")) {
        protractor_1.by.addLocator("cssWithRoot", 
        // This function is executed inside browser, needs to be inline
        (selector, rootElement) => {
            if (!rootElement) {
                rootElement = window.document.body;
            }
            if (!rootElement) {
                return;
            }
            if (rootElement.matches(selector)) {
                return rootElement;
            }
            return rootElement.querySelectorAll(selector);
        });
    }
    return protractor_1.by.cssWithRoot(selector);
};
class Atom {
    /**
     * Finds given component based on unique css id
     */
    static find(atomClass, id) {
        return atomClass.findIn(atomClass, (0, protractor_1.element)(protractor_1.by.id(id)));
    }
    static getSelector(atomClass) {
        if (atomClass.CSS_CLASS) {
            return `.${atomClass.CSS_CLASS}`;
        }
        if (atomClass.CSS_SELECTOR) {
            return atomClass.CSS_SELECTOR;
        }
        throw new ReferenceError(`expected atom class ${atomClass.name} to have either CSS_CLASS or CSS_SELECTOR nonempty`);
    }
    static getLocator(atomClass) {
        return byCssWithRoot(Atom.getSelector(atomClass));
    }
    /**
     * Finds given component inside provided element
     * - Will match provided element directly if it's a component
     * - Will provide a warning if more child components are found
     * - Uses static CSS_CLASS property in component's class
     */
    static findIn(atomClass, parentElement, index) {
        if (atomClass.findIn !== Atom.findIn) {
            return atomClass.findIn(atomClass, parentElement, index);
        }
        const locator = Atom.getLocator(atomClass);
        const create = (root) => new atomClass(root);
        if (index !== undefined) {
            if (!parentElement) {
                return create(protractor_1.element.all(locator).get(index));
            }
            return create(parentElement.all(locator).get(index));
        }
        if (!parentElement) {
            return create((0, protractor_1.element)(locator));
        }
        return create(parentElement.element(locator));
    }
    static findCount(atomClass, parentElement) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const locator = Atom.getLocator(atomClass);
            return yield (parentElement !== null && parentElement !== void 0 ? parentElement : protractor_1.element).all(locator).count();
        });
    }
    static getClasses(el) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return (yield el.getAttribute("class"))
                .split(/\s+/)
                .filter((name) => !!name);
        });
    }
    static hasClass(el, className) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const classes = yield Atom.getClasses(el);
            return classes.includes(className);
        });
    }
    static hasAnyClass(el, classNamesToSearch) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const classes = yield Atom.getClasses(el);
            return classes.some((name) => classNamesToSearch.includes(name));
        });
    }
    static wait(callback_1) {
        return tslib_1.__awaiter(this, arguments, void 0, function* (callback, timeout = 5000) {
            return yield protractor_1.browser.wait(callback, timeout);
        });
    }
    constructor(element) {
        this.scrollTo = (options) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            return protractor_1.browser.executeScript("arguments[0].scrollIntoView(arguments[1])", this.getElement().getWebElement(), options || null);
        });
        this.element = element;
    }
    isDisabled() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return !(yield this.element.isEnabled());
        });
    }
    isDisplayed() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this.element.isDisplayed();
        });
    }
    isPresent() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this.element.isPresent();
        });
    }
    hasClass(className) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return Atom.hasClass(this.element, className);
        });
    }
    getElement() {
        return this.element;
    }
    waitElementVisible() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield protractor_1.browser.wait(EC.visibilityOf(this.getElement()), 5000, "Element is not visible");
        });
    }
    isChildElementPresent(locator) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return yield this.element.isElementPresent(locator);
        });
    }
    /**
     * If no arguments are provided, will hover over center of the current element.
     * If only an element argument is provided, will hover over center of provided element.
     * If null element argument and location is provided, will offset from the top left corner of current element.
     * If an element and location is provided, will offset from top left of provided element.
     * @param {ElementFinder} [el] - accepts an element to mouse over center
     * @param {ILocation} [location] - offsets the mouse from top left of element
     */
    hover(el, location) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return protractor_1.browser
                .actions()
                .mouseMove(el !== null && el !== void 0 ? el : this.getElement(), location)
                .perform();
        });
    }
    wait(callback_1) {
        return tslib_1.__awaiter(this, arguments, void 0, function* (callback, timeout = 5000) {
            return yield Atom.wait(callback, timeout);
        });
    }
}
exports.Atom = Atom;
//# sourceMappingURL=atom.js.map