import {error} from "winston";
import waitForElementState from "./waitForElementState";
import {PageConfigTypes} from "../../../custom-types/pageConfig-types";
import logger from "../../reporting-helpers/logger";
import {browser, $, $$, expect} from '@wdio/globals'

/**
 * @param  method - only accepts 'add' or 'set'
 * @param  add -> to append the text to the existing text
 * @param  set -> cleans the existing text and writes new text
 */

export default async function (method: string, value: string, pageConfig: PageConfigTypes) {
    if (method.trim() !== 'add' && method.trim() !== 'set')
        throw new Error(`Invalid selection for "SetInputField" function. Valid selection is "add" OR "set"`);
    const command = (method.toLowerCase().trim() === 'add') ? 'addValue' : 'setValue';
    if (!value) throw error(`Value for the input box "${pageConfig.elementKey}" is missing`)
    await waitForElementState(pageConfig, false, "Displayed", 0, 0);
    await $(pageConfig.selector)[command](value);
    logger.info(`"Action "${method}" is performed on element "${pageConfig.elementKey}"`)
};


