"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.EyesLens = void 0;
const tslib_1 = require("tslib");
const helpers_1 = require("../helpers");
class EyesLens {
    constructor(browser, cameraSettings) {
        this.browser = browser;
        this.cameraSettings = cameraSettings;
    }
    takeSnapshot(label) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.eyes.setForceFullPageScreenshot(false);
            yield this.snapshotEachWidth(label);
        });
    }
    takeFullScreenSnapshot(label) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.eyes.setForceFullPageScreenshot(true);
            yield this.snapshotEachWidth(label);
        });
    }
    cameraON() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.eyes = yield this.configure();
            yield this.eyes.open(this.browser, this.cameraSettings.currentSuiteName, this.cameraSettings.currentTestName);
        });
    }
    cameraOFF() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.eyes.close();
            yield this.eyes.abortIfNotClosed();
        });
    }
    toolConfig() {
        return this.eyes;
    }
    configure() {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { Eyes } = require("@applitools/eyes-protractor");
            if (!this.eyes) {
                this.eyes = new Eyes();
                this.eyes.setApiKey(process.env.EYES_API_KEY);
                let branchName = process.env.CIRCLE_BRANCH ||
                    helpers_1.getCurrentBranchName() ||
                    "Unknown";
                const userName = process.env.CIRCLE_USERNAME
                    ? ` - [${process.env.CIRCLE_USERNAME}]`
                    : "";
                const batchName = ((_a = process.env.CIRCLE_PROJECT_REPONAME) === null || _a === void 0 ? void 0 : _a.toUpperCase()) +
                    " - " +
                    process.env.CIRCLE_JOB +
                    " - " +
                    branchName +
                    userName;
                const batchID = process.env.CIRCLE_JOB +
                    "_" +
                    process.env.CIRCLE_SHA1;
                branchName = branchName.substring(branchName.lastIndexOf("/") + 1);
                batchID
                    ? this.eyes.setBatch(batchName, batchID)
                    : this.eyes.setBatch(batchName);
                this.eyes.setBranchName(branchName);
                if (branchName !== "main") {
                    this.eyes.setParentBranchName("main");
                }
                this.eyes.setForceFullPageScreenshot(true);
                this.eyes.setIgnoreCaret(true);
            }
            return this.eyes;
        });
    }
    snapshotEachWidth(label) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (const width of this.cameraSettings.responsiveWidths) {
                yield this.browser.manage().window().setSize(width, 1080);
                if (this.cameraSettings.responsivityCallback) {
                    yield this.cameraSettings.responsivityCallback();
                }
                yield this.eyes.checkWindow(label);
            }
        });
    }
}
exports.EyesLens = EyesLens;
//# sourceMappingURL=eyes-lens.js.map