// Copyright (c) Mito
// Distributed under the terms of the Modified BSD License.

/*
    Contains all the selectors used across Mito frontend tests. 
*/

import { Selector} from 'testcafe';
import { testUser } from './roles';
import { 
    getCellSelector, 
    selectKernelButton,
    fileTab,
    fileTabNew,
    fileTabNewNotebook,
    getActiveElement,
    ascendingSortSelector,
    descendingSortSelector,
} from './selectors';

import { getTabNameSelector } from '../utils/tabHelpers';

import {
    getColumnHeaderFilterSelector
} from './columnHelpers'
import { exception } from 'console';
import { LOCAL_TEST } from '../config';

// A string you can pass to t.pressKeys that will delete all values from the currently selecte input
export const DELETE_PRESS_KEYS_STRING = 'ctrl+a delete'

/*
    Sorts a column, yo.
*/
export async function sortColumn(t: TestController, columnHeader: string, direction: 'ascending' | 'descending'): Promise<void> {
    
    // Open the filter/sort modal
    await t.click(getColumnHeaderFilterSelector(columnHeader));

    if (direction === 'ascending') {
        await t.click(ascendingSortSelector);
    } else {
        await t.click(descendingSortSelector);
    }
}


/*
    Helper function for checking mulitple tabs in one go!
*/
export async function checkSheets(t: TestController, sheets: Record<string, Record<string, string[]>>): Promise<void> {
    for (const tabName in sheets) {
        await checkSheet(t, sheets[tabName], tabName);
    }
}


/*
    Helper function for checking that a given sheet has the passed data
*/
export async function checkSheet(t: TestController, dataFrame: Record<string, string[]>, tabName?: string): Promise<void> {
    if (tabName !== undefined) {
        await t.click(getTabNameSelector(tabName));
    }

    for (const key in dataFrame) {
        await checkColumn(t, key, dataFrame[key])
    }
}


/*
    Helper function for checking that a given column has exactly columnValues inside
    of it, and nothing else. 
*/
export async function checkColumn(t: TestController, columnHeader: string, columnValues: string[]): Promise<void> {

    for (let i = 0; i < columnValues.length; i++) {
        // There is a warning generated by this line that is a testcafe bug fixed here: https://github.com/DevExpress/testcafe/issues/5389
        await t.expect(getCellSelector(columnHeader, i.toString()).innerText).eql(columnValues[i])
    }

    // And we check there are no other values
    await t.expect(getCellSelector(columnHeader, columnValues.length.toString()).exists).notOk();
}
