import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
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 { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _type_of } from "@swc/helpers/_/_type_of";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useEffect, useRef, useState } from "react";
import classNames from "classnames";
import { View } from "@tarojs/components";
import { ComponentDefaults } from "../../utils/typings";
import { useRtl } from "../configprovider/index";
import { pxTransform } from "../../utils/taro/px-transform";
import { harmony } from "../../utils/taro/platform";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    value: '',
    dot: false,
    max: 99,
    top: 0,
    right: 0,
    fill: 'solid',
    size: 'large',
    disabled: false
});
export var Badge = function(props) {
    var getContent = function getContent() {
        if (dot || value === 0) return null;
        if (typeof value === 'number') {
            return max < value ? "".concat(max, "+") : "".concat(value);
        }
        return value;
    };
    var isIcon = function isIcon() {
        if ((typeof value === "undefined" ? "undefined" : _type_of(value)) === 'object' && value) return value;
    };
    var isNumber = function isNumber() {
        if (typeof value === 'number' && value) return value;
    };
    var isString = function isString() {
        if (typeof value === 'string' && value) return value;
    };
    var _this;
    var rtl = useRtl();
    var _ref = _object_spread({}, defaultProps, props), className = _ref.className, style = _ref.style, value = _ref.value, max = _ref.max, children = _ref.children, dot = _ref.dot, top = _ref.top, right = _ref.right, fill = _ref.fill, size = _ref.size, disabled = _ref.disabled, rest = _object_without_properties(_ref, [
        "className",
        "style",
        "value",
        "max",
        "children",
        "dot",
        "top",
        "right",
        "fill",
        "size",
        "disabled"
    ]);
    var classPrefix = 'nut-badge';
    var isHarmony = harmony();
    var classes = classNames(classPrefix, className);
    var badgeRef = useRef(null);
    var _useState = _sliced_to_array(useState({}), 2), contentStyle = _useState[0], setContentStyle = _useState[1];
    var _obj;
    var contentClasses = classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix, "-sup"), isNumber() || isString() || dot), _define_property(_obj, "".concat(classPrefix, "-number"), isNumber()), _define_property(_obj, "".concat(classPrefix, "-one"), typeof getContent() === 'string' && ((_this = "".concat(getContent())) === null || _this === void 0 ? void 0 : _this.length) === 1), _define_property(_obj, "".concat(classPrefix, "-dot"), dot), _define_property(_obj, "".concat(classPrefix, "-dot-").concat(size), dot), _define_property(_obj, "".concat(classPrefix, "-").concat(fill), fill === 'outline'), _define_property(_obj, "".concat(classPrefix, "-content"), children), _define_property(_obj, "".concat(classPrefix, "-disabled"), disabled), _obj));
    useEffect(function() {
        if (badgeRef.current) {
            getPositionStyle();
        }
    }, []);
    var getPositionStyle = function() {
        return _async_to_generator(function() {
            var style;
            return _ts_generator(this, function(_state) {
                style = _define_property({
                    top: pxTransform(Number(top) || 0)
                }, rtl ? 'left' : 'right', isHarmony ? pxTransform(Number(right)) : "".concat(Number(right) || 0, "px"));
                setContentStyle(style);
                return [
                    2
                ];
            });
        })();
    };
    var _obj1;
    return /*#__PURE__*/ React.createElement(View, _object_spread({
        className: classes,
        style: style,
        ref: badgeRef
    }, rest), isIcon() && /*#__PURE__*/ React.createElement(View, {
        className: classNames("".concat(classPrefix, "-content"), (_obj1 = {}, _define_property(_obj1, "".concat(classPrefix, "-icon"), true), _define_property(_obj1, "".concat(classPrefix, "-icon-rtl"), rtl), _obj1)),
        style: contentStyle
    }, value), children, !isIcon() && (getContent() || dot) && /*#__PURE__*/ React.createElement(View, {
        className: contentClasses,
        style: contentStyle
    }, getContent()));
};
Badge.displayName = 'NutBadge';
