"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");
class Atom {
    constructor(elementFinder) {
        this.scrollTo = (options) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            return protractor_1.browser.executeScript("arguments[0].scrollIntoView(arguments[1])", this.getElement(), options || null);
        });
        this.element = elementFinder;
    }
    /**
     * Finds given component based on unique css id
     */
    static find(atomClass, id) {
        return atomClass.findIn(atomClass, protractor_1.element(protractor_1.by.id(id)));
    }
    static addProperLocator() {
        // We need locator which can match root element
        protractor_1.by.addLocator("properClassName", 
        // This function is executed inside browser, needs to be inline
        (className, rootElement) => {
            const classString = rootElement.getAttribute("class");
            if (classString) {
                const allClasses = " " + classString.replace(/\s+/g, " ") + " ";
                if (allClasses.indexOf(" " + className + " ") > -1) {
                    // Class is set in root element, let's return it
                    return rootElement;
                }
            }
            // Return matching sub-elements
            return rootElement.getElementsByClassName(className);
        });
    }
    /**
     * 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);
        }
        if (protractor_1.by.properClassName === undefined) {
            Atom.addProperLocator();
        }
        let componentRootElement;
        if (index !== undefined) {
            if (!parentElement) {
                componentRootElement = protractor_1.element
                    .all(protractor_1.by.className(atomClass.CSS_CLASS))
                    .get(index);
            }
            else {
                componentRootElement = parentElement
                    .all(protractor_1.by.className(atomClass.CSS_CLASS))
                    .get(index);
            }
        }
        else {
            if (!parentElement) {
                componentRootElement = protractor_1.element(protractor_1.by.properClassName(atomClass.CSS_CLASS));
            }
            else {
                componentRootElement = parentElement.element(protractor_1.by.properClassName(atomClass.CSS_CLASS));
            }
        }
        return new atomClass(componentRootElement);
    }
    static findCount(atomClass, parentElement) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!parentElement) {
                return protractor_1.element.all(protractor_1.by.className(atomClass.CSS_CLASS)).count();
            }
            return (parentElement.all(protractor_1.by.className(atomClass.CSS_CLASS)).count());
        });
    }
    static hasClass(el, className) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return el
                .getAttribute("class")
                .then((classes) => classes.split(" ").indexOf(className) !== -1);
        });
    }
    static hasAnyClass(el, classNamesToSearch) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return el.getAttribute("class").then((classesInElement) => {
                let found = false;
                const classesInElementArray = classesInElement.split(" ");
                classNamesToSearch.forEach((name) => {
                    found = classesInElementArray.indexOf(name) !== -1 || found;
                });
                return found;
            });
        });
    }
    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 this.element
                .getAttribute("class")
                .then((classes) => classes.split(" ").indexOf(className) !== -1);
        });
    }
    getElement() {
        return this.element;
    }
    isChildElementPresent(locator) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return 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();
        });
    }
}
exports.Atom = Atom;
Atom.CSS_CLASS = "UNDEFINED-ATOM-CLASS";
//# sourceMappingURL=atom.js.map