import { FOCUS_KEYS } from "../focus-visible";
import { rIC } from "../helpers";
export class DropdownEventsUtil {
    constructor() {
        this.handlePointerDown = () => {
            this.component.isKeyboardMode = false;
        };
        this.handleKeydown = (ev) => {
            this.component.isKeyboardMode = FOCUS_KEYS.includes(ev.key);
        };
    }
    connectedCallback(component) {
        this.component = component;
    }
    handleFocus(ev) {
        this.component.hasFocus = true;
        this.component.balFocus.emit(ev);
    }
    handleBlur(ev) {
        if (!this.component.isExpanded) {
            this.component.hasFocus = false;
            rIC(() => this.component.balBlur.emit(ev));
        }
    }
    handleClick(ev) {
        if (!this.component.valueUtil.isDisabled()) {
            if (this.component.chips) {
                const targetEl = ev.target;
                const closeEl = targetEl.closest('bal-close');
                if (closeEl) {
                    return;
                }
            }
            if (this.component.clearable) {
                const targetEl = ev.target;
                const clearEl = targetEl.closest('.bal-dropdown__clear');
                if (clearEl) {
                    this.component.valueUtil.updateRawValueBySelection([]);
                    return;
                }
            }
            this.component.popupUtil.toggleList();
        }
    }
    handleOutsideClick(ev) {
        var _a;
        if (this.component.isExpanded) {
            if (!this.component.el.contains(ev.target)) {
                this.component.isExpanded = false;
                (_a = this.component.listEl) === null || _a === void 0 ? void 0 : _a.resetFocus();
                this.component.hasFocus = false;
                rIC(() => this.component.balBlur.emit(ev));
            }
        }
    }
}
