"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.spaceBarFindInfo = exports.backToDefaultKeyboardKeyFindInfo = exports.shiftKeyFindInfo = exports.symbolsKeyFindInfo = exports.numbersKeyFindInfo = exports.charFindInfo = exports.AndroidKeyboardState = void 0;
exports.getFindInfoForKey = getFindInfoForKey;
exports.isInNumbersKeyboard = isInNumbersKeyboard;
exports.isInSymbolsKeyboard = isInSymbolsKeyboard;
exports.createFindsForAndroidTextEntryByKeyboard = createFindsForAndroidTextEntryByKeyboard;
exports.findAndroidKeyElement = findAndroidKeyElement;
exports.sendKeysThroughNativeAndroidKeyboard = sendKeysThroughNativeAndroidKeyboard;
var AndroidKeyboardState;
(function (AndroidKeyboardState) {
    AndroidKeyboardState["DEFAULT"] = "default";
    AndroidKeyboardState["NUMBERS"] = "numbers";
    AndroidKeyboardState["SYMBOLS"] = "symbols";
})(AndroidKeyboardState || (exports.AndroidKeyboardState = AndroidKeyboardState = {}));
function getFindInfoForKey(key) {
    const keyContentDesc = numberSymbolsToContentDesc[key] ?? symbolsToContentDesc[key] ?? key;
    return {
        class: 'android.widget.FrameLayout',
        contentDesc: keyContentDesc,
    };
}
const charFindInfo = (char) => getFindInfoForKey(char);
exports.charFindInfo = charFindInfo;
const numbersKeyFindInfo = () => getFindInfoForKey('Symbol keyboard');
exports.numbersKeyFindInfo = numbersKeyFindInfo;
const symbolsKeyFindInfo = () => getFindInfoForKey('More symbols');
exports.symbolsKeyFindInfo = symbolsKeyFindInfo;
const shiftKeyFindInfo = () => getFindInfoForKey('Shift');
exports.shiftKeyFindInfo = shiftKeyFindInfo;
const backToDefaultKeyboardKeyFindInfo = () => getFindInfoForKey('Letter keyboard');
exports.backToDefaultKeyboardKeyFindInfo = backToDefaultKeyboardKeyFindInfo;
const spaceBarFindInfo = () => getFindInfoForKey('Space');
exports.spaceBarFindInfo = spaceBarFindInfo;
const numberSymbolsToContentDesc = {
    1: '1',
    2: '2',
    3: '3',
    4: '4',
    5: '5',
    6: '6',
    7: '7',
    8: '8',
    9: '9',
    0: '0',
    '@': '@',
    '#': '#',
    $: '$',
    _: 'underline',
    '&': '&',
    '-': 'Dash',
    '+': 'Plus',
    '(': 'Left parenthesis',
    ')': 'Right parenthesis',
    '/': 'slash',
    '*': '*',
    '"': '"',
    "'": 'Apostrophe',
    ':': 'Colon',
    ';': 'Semicolon',
    '!': 'Exclamation',
    '?': 'Question mark',
    '.': '.',
    ',': ',',
};
function isInNumbersKeyboard(char) {
    return Object.keys(numberSymbolsToContentDesc).includes(char);
}
const symbolsToContentDesc = {
    '~': 'Tilde',
    '`': 'grave accent',
    '|': 'vertical line',
    '•': 'bullet',
    '√': 'square root',
    π: 'π',
    '÷': '÷',
    '×': '×',
    '¶': 'pilcrow sign',
    '△': 'Increment',
    '£': '£',
    '¢': 'cent sign',
    '€': '€',
    '¥': 'yen sign',
    '^': 'circumflex accent',
    '°': '°',
    '=': 'Equals',
    '{': 'left curly bracket',
    '}': 'right curly bracket',
    '\\': '\\',
    '%': '%',
    '©': '©',
    '®': '®',
    '™': '™',
    '✓': 'Check mark',
    '[': 'Left square bracket',
    ']': 'Right square bracket',
    '<': 'less-than sign',
    '>': 'greater-than sign',
};
function isInSymbolsKeyboard(char) {
    return Object.keys(symbolsToContentDesc).includes(char);
}
function getTransitionFindsForKeyboardState(currentState, desiredState) {
    const findChain = [];
    switch (desiredState) {
        case AndroidKeyboardState.DEFAULT: {
            if (currentState === AndroidKeyboardState.NUMBERS ||
                currentState === AndroidKeyboardState.SYMBOLS) {
                findChain.push((0, exports.backToDefaultKeyboardKeyFindInfo)());
            }
            break;
        }
        case AndroidKeyboardState.NUMBERS: {
            if (currentState === AndroidKeyboardState.DEFAULT) {
                findChain.push((0, exports.numbersKeyFindInfo)());
            }
            else if (currentState === AndroidKeyboardState.SYMBOLS) {
                findChain.push((0, exports.backToDefaultKeyboardKeyFindInfo)());
                findChain.push((0, exports.numbersKeyFindInfo)());
            }
            break;
        }
        case AndroidKeyboardState.SYMBOLS: {
            if (currentState === AndroidKeyboardState.DEFAULT) {
                findChain.push((0, exports.numbersKeyFindInfo)());
                findChain.push((0, exports.symbolsKeyFindInfo)());
            }
            else if (currentState === AndroidKeyboardState.NUMBERS) {
                findChain.push((0, exports.symbolsKeyFindInfo)());
            }
        }
    }
    return findChain;
}
function createFindsForAndroidTextEntryByKeyboard(text, initialKeyboardState = AndroidKeyboardState.DEFAULT) {
    const characters = text.split('');
    const findChain = [];
    let keyboardState = initialKeyboardState;
    for (const char of characters) {
        if (char === ' ') {
            findChain.push((0, exports.spaceBarFindInfo)());
            keyboardState = AndroidKeyboardState.DEFAULT;
        }
        else if (isInNumbersKeyboard(char)) {
            findChain.push(...getTransitionFindsForKeyboardState(keyboardState, AndroidKeyboardState.NUMBERS));
            keyboardState = AndroidKeyboardState.NUMBERS;
            findChain.push((0, exports.charFindInfo)(char));
        }
        else if (isInSymbolsKeyboard(char)) {
            findChain.push(...getTransitionFindsForKeyboardState(keyboardState, AndroidKeyboardState.SYMBOLS));
            keyboardState = AndroidKeyboardState.SYMBOLS;
            findChain.push((0, exports.charFindInfo)(char));
        }
        else {
            findChain.push(...getTransitionFindsForKeyboardState(keyboardState, AndroidKeyboardState.DEFAULT));
            keyboardState = AndroidKeyboardState.DEFAULT;
            const isLowercase = char === char.toLowerCase();
            if (!isLowercase) {
                findChain.push((0, exports.shiftKeyFindInfo)());
            }
            findChain.push((0, exports.charFindInfo)(char));
        }
    }
    return findChain;
}
function findAndroidKeyElement(driver, findInfo) {
    return driver.$(`~${findInfo.contentDesc}`).getElement();
}
async function getKeyboardState(driver) {
    const foundNumber = await findAndroidKeyElement(driver, (0, exports.charFindInfo)('9'));
    if (foundNumber.error) {
        return AndroidKeyboardState.DEFAULT;
    }
    return AndroidKeyboardState.NUMBERS;
}
async function sendKeysThroughNativeAndroidKeyboard(driver, text) {
    const initialKeyboardState = await getKeyboardState(driver);
    const findChain = createFindsForAndroidTextEntryByKeyboard(text, initialKeyboardState);
    let isShiftPressed = false;
    const foundCapital = await findAndroidKeyElement(driver, (0, exports.charFindInfo)('M'));
    isShiftPressed = !foundCapital.error;
    if (isShiftPressed &&
        findChain[0].contentDesc === (0, exports.shiftKeyFindInfo)().contentDesc) {
        findChain.shift();
    }
    if (text.length >= 0 &&
        text[0] !== ' ' &&
        text[0].toLowerCase() === text[0]) {
        if (isShiftPressed) {
            const shiftKey = await findAndroidKeyElement(driver, (0, exports.shiftKeyFindInfo)());
            await shiftKey.click();
        }
    }
    for (const find of findChain) {
        const key = await findAndroidKeyElement(driver, find);
        await key.click();
    }
}
