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 if the given elements text is the same as the given text
 * @param  elementType button | element | field
 * @param pageConfig
 * @param  {String}   falseCase     Whether to check if the content equals the
 *                                  given text or not
 * @param  {String}   expectedText  The text to validate against
 *
 * Therefore you can use getText on DOM-elements such as a <div> or a <p> or <a>. However,
 * if it is an <input> field or <textarea>, <select> then you would need to use the getValue command.
 * @param ignoreCaseCheck
 */
export default async function (elementType: string, pageConfig: PageConfigTypes, falseCase: boolean, expectedText: string, ignoreCaseCheck = false) {
    let text;
    const elem = $(pageConfig.selector);
    await elem.waitForDisplayed();

    if (ignoreCaseCheck)
        expectedText = expectedText.toLowerCase();

    if (elementType.trim() !== 'button' && elementType.trim() !== 'element' && elementType.trim() !== 'field')
        throw new Error(`Invalid selection for "CheckEqualsText" function. Valid selection is "button | element | field"`);

    if (elementType.trim() == 'button' || elementType.trim() == 'element') {
        text = ignoreCaseCheck ? (await elem.getText()).toLowerCase() : (await elem.getText());
    } else if (elementType.trim() == 'field') {
        text = ignoreCaseCheck ? (await elem.getValue()).toLowerCase() : (await elem.getValue());
    }

    if (falseCase) {
        CustomExpect.expectNotToBe(expectedText, text, `Expected Text/Value of element "${pageConfig.pageId}.${pageConfig.elementKey}" "${expectedText}" is matching with actual Text/Value "${text}"`);
    } else {
        CustomExpect.expectToBe(expectedText, text, `Expected Text/Value of element "${pageConfig.pageId}.${pageConfig.elementKey}" "${expectedText}" is not matching with actual Text/Value "${text}"`);
    }
};
