var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { jsx as _jsx } from "react/jsx-runtime";
import * as React from "react";
import { useCallback, useEffect, useRef } from "react";
import { cn } from "@utils";
import { dropdownItemVariants } from "./dropdownVariants";
export const DropdownItem = React.forwardRef((_a, ref) => {
    var { children, dropdownShow, index, className, disabled = false, setShowDropdown, onClickHandler, size = "md", position = "middle" } = _a, props = __rest(_a, ["children", "dropdownShow", "index", "className", "disabled", "setShowDropdown", "onClickHandler", "size", "position"]);
    const divRef = useRef(null);
    const combinedRef = (node) => {
        divRef.current = node;
        if (typeof ref === "function") {
            ref(node);
        }
        else if (ref) {
            ref.current = node;
        }
    };
    const handleOnClick = useCallback((e) => {
        if (disabled) {
            return;
        }
        setShowDropdown === null || setShowDropdown === void 0 ? void 0 : setShowDropdown(false);
        onClickHandler === null || onClickHandler === void 0 ? void 0 : onClickHandler(e);
    }, [disabled, onClickHandler, setShowDropdown]);
    const handleOnEnter = useCallback((e) => {
        if (disabled) {
            return;
        }
        if ((e === null || e === void 0 ? void 0 : e.key) === "Enter") {
            setShowDropdown === null || setShowDropdown === void 0 ? void 0 : setShowDropdown(false);
            onClickHandler === null || onClickHandler === void 0 ? void 0 : onClickHandler(e);
        }
    }, [disabled, onClickHandler, setShowDropdown]);
    useEffect(() => {
        var _a;
        if (dropdownShow && index === 0) {
            (_a = divRef.current) === null || _a === void 0 ? void 0 : _a.focus();
        }
    }, [dropdownShow, index]);
    return children ? (_jsx("div", Object.assign({ tabIndex: 0, "data-testid": "dropdown-item", ref: combinedRef, className: cn(dropdownItemVariants({ size, disabled, position }), className), onClick: handleOnClick, onKeyDown: handleOnEnter }, props, { children: children }))) : null;
});
DropdownItem.displayName = "DropdownItem";
//# sourceMappingURL=dropdownItem.js.map