"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildCustomFindEvent = buildCustomFindEvent;
exports.extractCustomFindParams = extractCustomFindParams;
function listenerForStepType(stepType) {
    switch (stepType) {
        case 'enter_text':
            return 'input';
        case 'hover':
            return 'hover';
        case 'click':
        case 'assert':
        case 'create_variable':
        default:
            return 'mouse-select';
    }
}
function buildSimpleElementSelector(customFind) {
    return {
        uuid: '',
        ...(customFind.selectorType === 'xpath'
            ? { xpath: customFind.selector }
            : { css_query: customFind.selector }),
    };
}
function buildCustomFindEvent(customFind, mode, stepType) {
    const primarySelectors = buildSimpleElementSelector(customFind);
    const foundElementSelectors = { ...primarySelectors };
    const findDescriptor = {
        type: mode,
        primarySelectors,
        foundElementSelectors,
    };
    return {
        action: 'record',
        listener: listenerForStepType(stepType),
        xPath: customFind.selectorType === 'xpath' ? customFind.selector : '',
        sourceEventTimeStamp: Date.now(),
        type: 'find_one',
        synthetic: true,
        findDescriptor,
    };
}
function extractCustomFindParams(toolParams) {
    if (!toolParams || typeof toolParams !== 'object') {
        return undefined;
    }
    const params = toolParams;
    const customFind = params.customFind;
    if (!customFind || typeof customFind !== 'object') {
        return undefined;
    }
    const inner = customFind;
    if (inner.selectorType !== 'css' && inner.selectorType !== 'xpath') {
        return undefined;
    }
    if (typeof inner.selector !== 'string' || inner.selector.length === 0) {
        return undefined;
    }
    return {
        selectorType: inner.selectorType,
        selector: inner.selector,
    };
}
