"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "TabbarItem", {
    enumerable: true,
    get: function() {
        return TabbarItem;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _components = require("@tarojs/components");
var _typings = require("../../utils/typings");
var _index = /*#__PURE__*/ _interop_require_default._(require("../badge/index"));
var _context = /*#__PURE__*/ _interop_require_default._(require("../tabbar/context"));
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    title: '',
    icon: null,
    value: '',
    dot: false,
    max: 99,
    top: '0',
    right: '0',
    direction: 'vertical'
});
var TabbarItem = function TabbarItem(props) {
    var ctx = (0, _react.useContext)(_context.default);
    var _ref = (0, _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, index = _ref.index, direction = _ref.direction, onActiveClick = _ref.onActiveClick, rest = (0, _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 = (0, _classnames.default)(classPrefix, (_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-active"), active), (0, _define_property._)(_obj, "".concat(classPrefix, "-large"), !icon || !title), _obj), className);
    var renderNodeWithActive = function renderNodeWithActive(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 renderTitleText() {
        return title && /*#__PURE__*/ _react.default.createElement(_components.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 renderTitle() {
        return /*#__PURE__*/ _react.default.createElement(_index.default, (0, _object_spread._)({
            size: "normal"
        }, badgeProps), renderTitleText());
    };
    var renderIcon = function renderIcon() {
        var distIcon = renderNodeWithActive(icon);
        // 鸿蒙差异处理，需要手动给icon一个color
        return /*#__PURE__*/ _react.default.isValidElement(distIcon) ? /*#__PURE__*/ _react.default.cloneElement(distIcon, (0, _object_spread_props._)((0, _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 renderIconAndTitle() {
        return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_index.default, (0, _object_spread_props._)((0, _object_spread._)({
            size: "normal"
        }, badgeProps), {
            top: 3
        }), renderIcon()), renderTitleText());
    };
    var renderDualItem = function renderDualItem() {
        return dot ? null : /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, renderIcon(), renderTitleText(), /*#__PURE__*/ _react.default.createElement(_index.default, badgeProps));
    };
    return /*#__PURE__*/ _react.default.createElement(_components.View, (0, _object_spread._)({
        className: tabbarItemClass,
        style: (0, _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 onClick() {
            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.default.createElement(_react.default.Fragment, null, icon && renderIconAndTitle(), !icon && renderTitle()));
};
