"use strict";
/* !
 * (c) Copyright 2026 Palantir Technologies Inc. All rights reserved.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.PopoverTarget = void 0;
const tslib_1 = require("tslib");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_1 = require("react");
const common_1 = require("../../common");
const popoverProps_1 = require("../popover/popoverProps");
exports.PopoverTarget = (0, react_1.forwardRef)((props, targetRef) => {
    const { children, className, floatingData, disabled = false, fill = false, handleMouseEnter, handleMouseLeave, handleTargetBlur, handleTargetContextMenu, handleTargetFocus, interactionKind, isContentEmpty, isControlled, isHoverInteractionKind, openOnTargetFocus = true, popupKind, renderTarget = undefined, targetProps, targetTagName = "span", } = props;
    const tagName = fill ? "div" : targetTagName;
    const { isOpen } = floatingData;
    const ref = (0, common_1.mergeRefs)(floatingData.refs.setReference, targetRef);
    // Custom keyboard click handler for the reference element. Floating UI's useClick hook
    // has its keyboardHandlers disabled because it calls `preventDefault()` on Space keydown,
    // which prevents space characters from being typed in <input>/<textarea> target children.
    // This handler replicates keyboard-to-click behavior while preserving typeable element input.
    const handleReferenceKeyDown = (0, react_1.useCallback)((event) => {
        if (!common_1.Utils.isKeyboardClick(event) || event.repeat) {
            return;
        }
        const eventTarget = event.target;
        // Don't intercept keyboard events on typeable elements — let them handle
        // Space (character input) and Enter (form submission) normally
        if (isTypeableElement(eventTarget)) {
            return;
        }
        if (event.key === " ") {
            // Buttons handle Space natively (fire click on keyup), skip to avoid double-toggle
            if (eventTarget.closest("button") != null) {
                return;
            }
            // Prevent page scroll for non-typeable, non-button targets
            event.preventDefault();
        }
        else if (event.key === "Enter") {
            // Buttons and anchors fire native click on Enter, skip to avoid double-toggle
            if (eventTarget.closest("button, a") != null) {
                return;
            }
        }
        // For non-typeable, non-natively-clickable targets (e.g. <span>, <div>),
        // simulate a click so that useClick's onClick handler toggles the popover
        event.currentTarget.click();
    }, []);
    const targetEventHandlers = isHoverInteractionKind
        ? {
            onBlur: handleTargetBlur,
            onContextMenu: handleTargetContextMenu,
            onFocus: handleTargetFocus,
            onMouseEnter: handleMouseEnter,
            onMouseLeave: handleMouseLeave,
        }
        : {};
    // Ensure target is focusable if relevant prop enabled
    const targetTabIndex = !isContentEmpty && !disabled && openOnTargetFocus && isHoverInteractionKind ? 0 : undefined;
    const ownTargetProps = {
        // N.B. this.props.className is passed along to renderTarget even though the user would have access to it.
        // If, instead, renderTarget is undefined and the target is provided as a child, props.className is
        // applied to the generated target wrapper element.
        className: (0, classnames_1.default)(className, common_1.Classes.POPOVER_TARGET, {
            [common_1.Classes.POPOVER_OPEN]: isOpen,
            // this class is mainly useful for button targets
            [common_1.Classes.ACTIVE]: isOpen && !isControlled && !isHoverInteractionKind,
            [common_1.Classes.FILL]: fill,
        }),
        ref,
        ...targetEventHandlers,
    };
    const childTargetProps = {
        "aria-expanded": isHoverInteractionKind ? undefined : isOpen,
        "aria-haspopup": interactionKind === popoverProps_1.PopoverInteractionKind.HOVER_TARGET_ONLY ? undefined : (popupKind ?? "menu"),
    };
    const targetModifierClasses = {
        // this class is mainly useful for Blueprint <Button> targets; we should only apply it for
        // uncontrolled popovers when they are opened by a user interaction
        [common_1.Classes.ACTIVE]: isOpen && !isControlled && !isHoverInteractionKind,
        // similarly, this class is mainly useful for targets like <Button>, <InputGroup>, etc.
        [common_1.Classes.FILL]: fill,
    };
    let target;
    if (renderTarget !== undefined) {
        const floatingProps = floatingData.getReferenceProps({
            onKeyDown: handleReferenceKeyDown,
        });
        // When using renderTarget, if the consumer renders a tooltip target, it's their responsibility
        // to disable that tooltip when this popover is open
        target = renderTarget({
            ...ownTargetProps,
            ...childTargetProps,
            // Apply Floating UI's interaction props for renderTarget case
            ...floatingProps,
            className: (0, classnames_1.default)(ownTargetProps.className, targetModifierClasses),
            isOpen,
            tabIndex: targetTabIndex,
        });
    }
    else {
        const childTarget = common_1.Utils.ensureElement(react_1.Children.toArray(children)[0]);
        if (childTarget === undefined) {
            return null;
        }
        const clonedTarget = (0, react_1.cloneElement)(childTarget, {
            ...childTargetProps,
            className: (0, classnames_1.default)(childTarget.props.className, targetModifierClasses),
            disabled: (isOpen && isTooltipElement(childTarget)) || childTarget.props.disabled,
            tabIndex: childTarget.props.tabIndex ?? targetTabIndex,
        });
        const wrappedTarget = (0, react_1.createElement)(tagName, {
            ...ownTargetProps,
            ...targetProps,
            // Apply Floating UI's interaction props to the wrapper element (same element that has the ref)
            ...floatingData.getReferenceProps({
                onKeyDown: handleReferenceKeyDown,
            }),
        }, clonedTarget);
        target = wrappedTarget;
    }
    return target;
});
exports.PopoverTarget.displayName = `${common_1.DISPLAYNAME_PREFIX}.PopoverTarget`;
/**
 * Check if a React element is a Tooltip component by comparing its displayName.
 * This avoids importing the Tooltip component directly, which would create a dependency cycle.
 */
function isTooltipElement(element) {
    return (element?.type != null &&
        typeof element.type !== "string" &&
        typeof element.type === "function" &&
        "displayName" in element.type &&
        element.type.displayName === `${common_1.DISPLAYNAME_PREFIX}.Tooltip`);
}
/**
 * Check if an element accepts keyboard text input (i.e., Space should insert a character,
 * not be interpreted as a click).
 */
function isTypeableElement(element) {
    const tagName = element.tagName;
    return tagName === "INPUT" || tagName === "TEXTAREA" || tagName === "SELECT" || element.isContentEditable;
}
//# sourceMappingURL=popoverTarget.js.map