function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
import React, { useEffect, useRef, useContext } from "react";
import { classes } from "../../Dropdown.tokens";
import { cx } from "../../../../utils";
import { Context } from "../../Dropdown.context";
import { getItemId } from "../../utils";
import { Wrapper, DisclosureIconWrapper, Divider, CellWrapper, StyledCell, RenderItemWrapper, StyledIconDisclosureRight } from "./DropdownItem.styles";
export var DropdownItem = function DropdownItem(param) {
    var item = param.item, path = param.path, currentLevel = param.currentLevel, index = param.index, ariaControls = param.ariaControls, ariaExpanded = param.ariaExpanded, ariaLevel = param.ariaLevel, ariaLabel = param.ariaLabel;
    var value = item.value, label = item.label, disabled = item.disabled, isDisabled = item.isDisabled, contentLeft = item.contentLeft, contentRight = item.contentRight, dividerBefore = item.dividerBefore, dividerAfter = item.dividerAfter, className = item.className, rest = _object_without_properties(item, [
        "value",
        "label",
        "disabled",
        "isDisabled",
        "contentLeft",
        "contentRight",
        "dividerBefore",
        "dividerAfter",
        "className"
    ]);
    var ref = useRef(null);
    var _useContext = useContext(Context), focusedPath = _useContext.focusedPath, size = _useContext.size, variant = _useContext.variant, itemRole = _useContext.itemRole, handleGlobalToggle = _useContext.handleGlobalToggle, closeOnSelect = _useContext.closeOnSelect, onHover = _useContext.onHover, onItemSelect = _useContext.onItemSelect, onItemClick = _useContext.onItemClick, hasArrow = _useContext.hasArrow, treeId = _useContext.treeId, renderItem = _useContext.renderItem;
    var hasDescendants = Boolean(item.items);
    var disclosureIconSize = size === 'xs' ? 'xs' : 's';
    var itemDisabled = Boolean(disabled || isDisabled);
    var isDisabledClassName = itemDisabled ? classes.dropdownItemIsDisabled : undefined;
    var focusedClass = currentLevel === focusedPath.length - 1 && index === (focusedPath === null || focusedPath === void 0 ? void 0 : focusedPath[currentLevel]) ? classes.dropdownItemIsFocused : undefined;
    var activeClass = value === (path === null || path === void 0 ? void 0 : path[currentLevel + 1]) ? classes.dropdownItemIsActive : undefined;
    useEffect(function() {
        if (focusedClass && (ref === null || ref === void 0 ? void 0 : ref.current)) {
            ref.current.scrollIntoView({
                behavior: 'smooth',
                block: 'center',
                inline: 'center'
            });
        }
    }, [
        focusedClass
    ]);
    var handleClick = function handleClick(event) {
        if (itemDisabled) {
            event.stopPropagation();
            return;
        }
        if (onItemSelect) {
            onItemSelect(item, event);
        }
        if (onItemClick) {
            onItemClick(item, event);
        }
        // Закрываем весь дропдаун целиком при клике на айтем без потомков. Только при closeOnSelect === true.
        if (closeOnSelect && !hasDescendants) {
            handleGlobalToggle(false, event);
        }
    };
    var handleHover = function handleHover() {
        if (onHover) {
            onHover(index, item);
        }
    };
    return /*#__PURE__*/ React.createElement(React.Fragment, null, dividerBefore && /*#__PURE__*/ React.createElement(Divider, {
        variant: variant
    }), /*#__PURE__*/ React.createElement(Wrapper, _object_spread_props(_object_spread({}, rest), {
        ref: ref,
        className: cx(isDisabledClassName, focusedClass, activeClass, className),
        id: getItemId(treeId, value.toString()),
        role: itemRole,
        onClick: handleClick,
        onMouseEnter: handleHover,
        variant: variant,
        "aria-disabled": disabled || isDisabled,
        "aria-controls": ariaControls,
        "aria-expanded": ariaExpanded,
        "aria-level": ariaLevel,
        "aria-label": ariaLabel
    }), renderItem ? /*#__PURE__*/ React.createElement(RenderItemWrapper, null, renderItem(item)) : /*#__PURE__*/ React.createElement(CellWrapper, null, /*#__PURE__*/ React.createElement(StyledCell, {
        contentLeft: contentLeft,
        contentRight: contentRight,
        alignContentLeft: "center",
        alignContentRight: "center",
        title: label
    })), item.items && hasArrow && /*#__PURE__*/ React.createElement(DisclosureIconWrapper, null, /*#__PURE__*/ React.createElement(StyledIconDisclosureRight, {
        size: disclosureIconSize,
        color: "inherit"
    }))), dividerAfter && /*#__PURE__*/ React.createElement(Divider, {
        variant: variant
    }));
};
