"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScrollStep = void 0;
exports.humanizeScrollInformation = humanizeScrollInformation;
exports.formatPercentageVal = formatPercentageVal;
exports.getScrollDirectionAndPercentageFromCoordinates = getScrollDirectionAndPercentageFromCoordinates;
const MablStepV2_1 = require("../../MablStepV2");
const ScrollStepDescriptor_1 = require("../../types/mobile/ScrollStepDescriptor");
const stepUtil_1 = require("./stepUtil");
const STEP_NAME = 'Scroll';
class ScrollStep extends MablStepV2_1.MablStepV2 {
    static stepName = STEP_NAME;
    static yamlMablScriptNames = [ScrollStep.stepName];
    static stepVersion = 2;
    constructor(descriptor) {
        super(ScrollStep.stepName, descriptor, 'scroll');
    }
    stepDescription() {
        return humanizeScrollInformation(this.descriptor);
    }
    static fromYaml(_stepName, stepDescriptor) {
        if (!stepDescriptor.scrollDirection) {
            const { scrollDirection } = getScrollDirectionAndPercentageFromCoordinates(stepDescriptor.coordinates);
            stepDescriptor.scrollDirection = scrollDirection;
        }
        if (!stepDescriptor.scrollType) {
            stepDescriptor.scrollType = ScrollStepDescriptor_1.ScrollType.ScrollDeviceScreen;
        }
        return new ScrollStep(stepDescriptor);
    }
    static create(descriptor) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new ScrollStep(descriptor);
    }
}
exports.ScrollStep = ScrollStep;
function humanizeScrollInformation(stepDescriptor) {
    switch (stepDescriptor.scrollType) {
        case ScrollStepDescriptor_1.ScrollType.ScrollDeviceScreen: {
            const { coordinates } = stepDescriptor;
            return humanizeCoordinateScroll(coordinates, 'of the screen');
        }
        case ScrollStepDescriptor_1.ScrollType.ScrollToElement: {
            const { coordinates, find, maxScrollAttempts, scrollDirection, targetElementDescriptor, } = stepDescriptor;
            let scrollText = `Scroll ${scrollDirection} a maximum of ${maxScrollAttempts} times`;
            if (coordinates) {
                scrollText = humanizeCoordinateScroll(coordinates, `a maximum of ${maxScrollAttempts} times`);
            }
            const containerElementDescription = find
                ? (0, stepUtil_1.getElementDescriptionFromMobileFindDescriptor)(find)
                : 'screen';
            const targetElementDescription = (0, stepUtil_1.getElementDescriptionFromMobileFindDescriptor)(targetElementDescriptor);
            return `${scrollText} within the ${containerElementDescription} until the ${targetElementDescription} is visible`;
        }
        case ScrollStepDescriptor_1.ScrollType.ScrollWithinElement: {
            const { coordinates, find } = stepDescriptor;
            const containerElementDescription = (0, stepUtil_1.getElementDescriptionFromMobileFindDescriptor)(find);
            return humanizeCoordinateScroll(coordinates, `within the ${containerElementDescription}`);
        }
        default:
            return 'Unknown scroll step type';
    }
}
function humanizeCoordinateScroll(coordinates, scrollTargetActionString) {
    const { scrollDirection, scrollPercent } = getScrollDirectionAndPercentageFromCoordinates(coordinates);
    return `Scroll ${scrollDirection} ${scrollPercent} ${scrollTargetActionString}`;
}
function formatPercentageVal(percent) {
    return `${Math.round(percent * 10000) / 100}%`;
}
function getScrollDirectionAndPercentageFromCoordinates(coordinates) {
    if (!coordinates) {
        return { scrollDirection: ScrollStepDescriptor_1.ScrollDirection.Down, scrollPercent: '80%' };
    }
    const yScrollDistance = Math.abs(coordinates.to.yPercent - coordinates.from.yPercent);
    const xScrollDistance = Math.abs(coordinates.to.xPercent - coordinates.from.xPercent);
    if (yScrollDistance > xScrollDistance) {
        const scrollDistanceFormatted = formatPercentageVal(yScrollDistance);
        const isScrollUp = coordinates.from.yPercent < coordinates.to.yPercent;
        const scrollDirection = isScrollUp
            ? ScrollStepDescriptor_1.ScrollDirection.Up
            : ScrollStepDescriptor_1.ScrollDirection.Down;
        return { scrollDirection, scrollPercent: scrollDistanceFormatted };
    }
    const scrollDistanceFormatted = formatPercentageVal(xScrollDistance);
    const isScrollLeft = coordinates.from.xPercent < coordinates.to.xPercent;
    const scrollDirection = isScrollLeft
        ? ScrollStepDescriptor_1.ScrollDirection.Left
        : ScrollStepDescriptor_1.ScrollDirection.Right;
    return { scrollDirection, scrollPercent: scrollDistanceFormatted };
}
