"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dom_util_1 = require("@mablhq-internal/mablscript-find/dom-util");
const domUtils_1 = require("../../sharedUtils/domUtils");
const actions_1 = require("../actions");
const BaseContentEventHandler_1 = require("./BaseContentEventHandler");
const domTools_1 = require("../domTools");
const shadowDomUtils_1 = require("../../sharedUtils/shadowDomUtils");
const util_1 = require("./util");
const walkthrough_1 = require("../../sharedUtils/walkthrough");
class InputEventHandler extends BaseContentEventHandler_1.BaseContentEventHandler {
    constructor(mouseHoverEventHandler, store, selectorCache) {
        super('input', 'Text Input Event Handler', store, selectorCache);
        this.mouseHoverEventHandler = mouseHoverEventHandler;
        this.handle = this.handle.bind(this);
        this.clearSelectionTarget = this.clearSelectionTarget.bind(this);
    }
    setSelectionTarget(selectionTarget) {
        this.selectionTarget = selectionTarget;
    }
    clearSelectionTarget() {
        this.selectionTarget = undefined;
    }
    async handle(event) {
        if (!(0, util_1.shouldListenForTrainedStepInRecording)(this.store.getState()) ||
            !(0, util_1.isTrustedEvent)(event)) {
            return;
        }
        const { path, target } = (0, domUtils_1.adjustTarget)(event.target, (0, util_1.getPathOnEvent)(event));
        const isInWalkthrough = this.store.getState().walkthrough.isInProgress;
        if (isInWalkthrough) {
            const isInTrainerContext = this.store.getState().walkthrough.activeContext ===
                walkthrough_1.ActiveWalkthroughContext.TRAINER;
            if (isInTrainerContext || !(0, util_1.isWalkthroughTarget)(target)) {
                return;
            }
        }
        if ((0, domUtils_1.isFileInput)(target)) {
            this.handleFileInput(event, path, target);
        }
        else if (InputEventHandler.isHidden(target)) {
        }
        else if ((0, dom_util_1.isTextField)(target) ||
            (0, domUtils_1.isSelect)(target)) {
            await this.handleTextFieldOrSelect(event, path, target);
        }
    }
    handleFileInput(event, path, target) {
        this.mouseHoverEventHandler.recordHovers();
        const xPath = (0, dom_util_1.createXpathFromPathArray)(path);
        const eventMessage = (0, domTools_1.createBaseEventMessageWithShadowParents)(event, 'record', 'set-files', xPath, event.timeStamp, undefined);
        eventMessage.inputData = event.data;
        eventMessage.inputType = event.inputType;
        eventMessage.inputValue = (0, domUtils_1.getElementValueFromElement)(target);
        eventMessage.multiple = target.multiple;
        if (target?.files?.length) {
            eventMessage.files = Array.from(target.files).map((file) => ({
                url: URL.createObjectURL(file),
                name: file.name,
                size: file.size,
                type: file.type,
            }));
            void (0, actions_1.sendEventMessage)(eventMessage);
        }
    }
    async handleTextFieldOrSelect(event, path, target) {
        const inputValue = (0, domUtils_1.getElementValueFromElement)(target);
        const selectedIndex = (0, domUtils_1.getSelectedIndexFromElement)(target);
        const selectedText = (0, domUtils_1.getSelectedTextFromElement)(target);
        this.mouseHoverEventHandler.recordHovers();
        const xPath = (0, dom_util_1.createXpathFromPathArray)(path);
        let eventMessage;
        if (this.selectionTarget?.findDescriptor) {
            eventMessage = await (0, domTools_1.createEventMessageWithShadowParents)(target, 'record', 'input', xPath, event.timeStamp);
        }
        else {
            eventMessage = await (0, domTools_1.createEventMessageWithShadowParents)(target, 'record', 'input', xPath, event.timeStamp);
        }
        eventMessage.inputData = event.data;
        eventMessage.inputType = event.inputType;
        eventMessage.inputValue = inputValue;
        eventMessage.selectedIndex = selectedIndex;
        eventMessage.selectedText = selectedText;
        eventMessage.variable = this.selectionTarget?.variable;
        eventMessage.findDescriptor = this.selectionTarget?.findDescriptor;
        void (0, actions_1.sendEventMessage)(eventMessage);
    }
    async createPrototypeEventMessageForInsert(element) {
        const xPath = (0, dom_util_1.createXPathFromElement)(element);
        let eventMessage;
        const timeStamp = new Date().getTime();
        if (this.selectionTarget?.findDescriptor) {
            eventMessage = (0, shadowDomUtils_1.createBaseEventMessageWithShadowParentsFromElement)(element, 'record', 'input', xPath, timeStamp, (0, util_1.getCustomTestIdAttributes)());
        }
        else {
            eventMessage = await (0, domTools_1.createEventMessageWithShadowParents)(element, 'record', 'input', xPath, timeStamp);
        }
        return eventMessage;
    }
    async recordInsertVariableValueAsText(element, inputData) {
        const eventMessage = await this.createPrototypeEventMessageForInsert(element);
        eventMessage.inputData = inputData;
        eventMessage.variable = this.selectionTarget?.variable;
        eventMessage.findDescriptor = this.selectionTarget?.findDescriptor;
        eventMessage.synthetic = true;
        void (0, actions_1.sendEventMessage)(eventMessage);
    }
    async recordInsertText(element, inputData) {
        const eventMessage = await this.createPrototypeEventMessageForInsert(element);
        eventMessage.inputData = inputData;
        eventMessage.inputValue = inputData;
        eventMessage.findDescriptor = this.selectionTarget?.findDescriptor;
        eventMessage.synthetic = true;
        void (0, actions_1.sendEventMessage)(eventMessage);
    }
    static isHidden(element) {
        const style = window.getComputedStyle(element);
        return style.display === 'none';
    }
}
exports.default = InputEventHandler;
