"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const dom_util_1 = require("@mablhq-internal/mablscript-find/dom-util");
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const actions_1 = require("../../sharedUtils/actions");
const domUtils_1 = require("../../sharedUtils/domUtils");
const OverlayHandler_1 = __importDefault(require("./OverlayHandler"));
class QueryOverlayHandler extends OverlayHandler_1.default {
    constructor(store, selectorCache) {
        super(undefined, store, selectorCache, 'Query Overlay Handler');
        this.selectorTarget = {};
    }
    update(selectorTarget) {
        this.selectorTarget = selectorTarget;
        switch (selectorTarget.selectionType) {
            case 'element_group':
                (0, domUtils_1.highlightSimilarElements)(selectorTarget.elementHighlights || [], (selectorTarget.elementHighlights?.length ?? 0) > 0);
                break;
            case 'element':
            default:
                this.highlightTheElements(this.selectorTarget);
        }
    }
    highlightTheElements(selectorTarget) {
        const { dispatch } = this.store;
        let elements = (0, domUtils_1.getAllElementsBySelectors)(selectorTarget.findDescriptor?.primarySelectors);
        elements = elements?.filter((element) => !(0, domUtils_1.isIgnoredElement)(element));
        let resolvedTarget = {};
        if (!elements) {
            elements = [];
        }
        else if (!elements.length) {
            resolvedTarget = selectorTarget;
        }
        else {
            elements.sort(domUtils_1.compareElements);
            if (selectorTarget.findDescriptor?.type === 'find_all') {
                resolvedTarget = { ...selectorTarget, elements };
            }
            else {
                const element = this.getElementByFindType(elements, selectorTarget.findDescriptor?.type);
                if (!element) {
                    return;
                }
                resolvedTarget = {
                    ...selectorTarget,
                    tagName: (0, domUtils_1.getTagNameFromElement)(element),
                    inputElementType: (0, domUtils_1.getInputTypeFromElement)(element),
                };
                if (resolvedTarget.findDescriptor) {
                    resolvedTarget.findDescriptor.foundElementSelectors =
                        (0, mablscriptFind_1.elementToSelector)(element);
                }
                resolvedTarget.availableAttrProps = (0, dom_util_1.getAttributePropertyMap)(element);
                resolvedTarget.availableCssProps = (0, dom_util_1.getCssPropertyMap)(element);
                if (resolvedTarget.findDescriptor) {
                    Object.assign(resolvedTarget.availableAttrProps, resolvedTarget.findDescriptor.foundElementSelectors);
                }
            }
        }
        dispatch((0, actions_1.setSelectionTargetBackground)(resolvedTarget));
        if (!elements?.length) {
            (0, domUtils_1.highlightSimilarElements)([], false);
            return;
        }
        const elementHighlights = elements.map((element, index) => ({
            element,
            index,
            role: 'other',
        }));
        switch (selectorTarget.findDescriptor?.type) {
            case 'find_first':
                elementHighlights[0].role = 'chosen';
                break;
            case 'find_last':
                elementHighlights[elements.length - 1].role = 'chosen';
                break;
            case 'find_all':
            default:
                break;
        }
        (0, domUtils_1.highlightSimilarElements)(elementHighlights, false);
    }
    handleScroll() {
        this.update(this.selectorTarget);
    }
    getElementByFindType(elements, type) {
        switch (type) {
            case 'find_first':
                return elements?.[0];
            case 'find_last':
                return elements?.[elements.length - 1];
            default:
                return;
        }
    }
}
exports.default = QueryOverlayHandler;
