import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import React, { useContext } from "react";
import classNames from "classnames";
import { View } from "@tarojs/components";
import { ComponentDefaults } from "../../utils/typings";
import Badge from "../badge/index";
import TabbarContext from "../tabbar/context";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    title: '',
    icon: null,
    value: '',
    dot: false,
    max: 99,
    top: '0',
    right: '0',
    direction: 'vertical'
});
export var TabbarItem = function(props) {
    var ctx = useContext(TabbarContext);
    var _ref = _object_spread({}, defaultProps, props), className = _ref.className, style = _ref.style, title = _ref.title, icon = _ref.icon, value = _ref.value, dot = _ref.dot, max = _ref.max, top = _ref.top, right = _ref.right, // @ts-ignore
    index = _ref.index, direction = _ref.direction, onActiveClick = _ref.onActiveClick, rest = _object_without_properties(_ref, [
        "className",
        "style",
        "title",
        "icon",
        "value",
        "dot",
        "max",
        "top",
        "right",
        "index",
        "direction",
        "onActiveClick"
    ]);
    var active = index === (ctx === null || ctx === void 0 ? void 0 : ctx.selectIndex);
    var classPrefix = 'nut-tabbar-item';
    var _obj;
    var tabbarItemClass = classNames(classPrefix, (_obj = {}, _define_property(_obj, "".concat(classPrefix, "-active"), active), _define_property(_obj, "".concat(classPrefix, "-large"), !icon || !title), _obj), className);
    var renderNodeWithActive = function(node) {
        return node && typeof node === 'function' ? node(active) : node;
    };
    var badgeProps = {
        value: renderNodeWithActive(value),
        dot: dot,
        max: max,
        top: top,
        right: right,
        color: ctx === null || ctx === void 0 ? void 0 : ctx.activeColor
    };
    var renderTitleText = function() {
        return title && /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-text"),
            style: {
                color: active ? ctx === null || ctx === void 0 ? void 0 : ctx.activeColor : ctx === null || ctx === void 0 ? void 0 : ctx.inactiveColor
            }
        }, renderNodeWithActive(title));
    };
    var renderTitle = function() {
        return /*#__PURE__*/ React.createElement(Badge, _object_spread({
            size: "normal"
        }, badgeProps), renderTitleText());
    };
    var renderIcon = function() {
        var distIcon = renderNodeWithActive(icon);
        // 鸿蒙差异处理，需要手动给icon一个color
        return /*#__PURE__*/ React.isValidElement(distIcon) ? /*#__PURE__*/ React.cloneElement(distIcon, _object_spread_props(_object_spread({}, distIcon.props), {
            color: active ? ctx === null || ctx === void 0 ? void 0 : ctx.activeColor : ctx === null || ctx === void 0 ? void 0 : ctx.inactiveColor
        })) : null;
    };
    var renderIconAndTitle = function() {
        return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Badge, _object_spread_props(_object_spread({
            size: "normal"
        }, badgeProps), {
            top: 3
        }), renderIcon()), renderTitleText());
    };
    var renderDualItem = function() {
        return dot ? null : /*#__PURE__*/ React.createElement(React.Fragment, null, renderIcon(), renderTitleText(), /*#__PURE__*/ React.createElement(Badge, badgeProps));
    };
    return /*#__PURE__*/ React.createElement(View, _object_spread({
        className: tabbarItemClass,
        style: _object_spread({
            color: active ? ctx === null || ctx === void 0 ? void 0 : ctx.activeColor : ctx === null || ctx === void 0 ? void 0 : ctx.inactiveColor
        }, style),
        onClick: function() {
            return active ? onActiveClick === null || onActiveClick === void 0 ? void 0 : onActiveClick() : ctx === null || ctx === void 0 ? void 0 : ctx.handleClick(index);
        }
    }, rest), direction === 'horizontal' && !dot ? renderDualItem() : /*#__PURE__*/ React.createElement(React.Fragment, null, icon && renderIconAndTitle(), !icon && renderTitle()));
};
