import {ConfigHelper} from "../../common-helpers/config-helper";
import {PageConfigTypes} from "../../../custom-types/pageConfig-types";
import {CustomExpect} from "../../common-helpers/custom-expect";
import {browser, $, $$, expect} from '@wdio/globals'

/**
 * Check the offset of the given element
 * @param  {String}   selector              Element selector
 * @param  {String}   falseCase         Whether to check if the offset matches
 *                                      or not
 * @param  {String}   expectedPosition  The position to check against
 * @param  {String}   axis              The axis to check on (x or y)
 */
export default async function (pageConfig: PageConfigTypes, falseCase: boolean, expectedPosition: string, axis: 'x' | 'y') {
    /**
     * Get the location of the element on the given axis
     * @type {[type]}
     */

    // @ts-ignore
    if (axis.toLowerCase().trim() !== ('x' || 'y'))
        throw new Error(`Invalid selection for "CheckOffSet" function. Valid selection is "x" OR "y"`);

    const location = await $(pageConfig.selector).getLocation(axis);

    if (falseCase)
        CustomExpect.expectNotToEqual(location, parseFloat(expectedPosition), `Expected location of element "${pageConfig.pageId}.${pageConfig.elementKey}" is matching with actual location`)
    else
        CustomExpect.expectToEqual(location, parseFloat(expectedPosition), `Expected location of element "${pageConfig.pageId}.${pageConfig.elementKey}" is not matching with actual location`)
};
