import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useEffect, useRef } from "react";
import classNames from "classnames";
import { Text, View } from "@tarojs/components";
import { Failure, Loading, Success, Tips } from "@nutui/icons-react-taro";
import Overlay from "../overlay/index";
import { defaultOverlayProps } from "../overlay/overlay";
import { customEvents, useCustomEvent, useCustomEventsPath, useParams } from "../../hooks/taro/use-custom-event";
import { usePropsValue } from "../../hooks/use-props-value";
import { useRtl } from "../configprovider/index";
import { harmony, td } from "../../utils/taro/platform";
import { mergeProps } from "../../utils/merge-props";
var defaultProps = _object_spread_props(_object_spread({}, defaultOverlayProps), {
    id: '',
    duration: 3,
    position: 'middle',
    title: '',
    size: 'base',
    icon: null,
    content: '',
    contentClassName: '',
    contentStyle: {},
    closeOnOverlayClick: false,
    lockScroll: false,
    visible: false,
    wordBreak: 'break-all',
    zIndex: 1300,
    onClose: function() {}
});
export var Toast = function(props) {
    var _useParams = useParams(mergeProps(defaultProps, props)), _useParams_params = _useParams.params, id = _useParams_params.id, position = _useParams_params.position, contentStyle = _useParams_params.contentStyle, icon = _useParams_params.icon, content = _useParams_params.content, duration = _useParams_params.duration, title = _useParams_params.title, closeOnOverlayClick = _useParams_params.closeOnOverlayClick, lockScroll = _useParams_params.lockScroll, contentClassName = _useParams_params.contentClassName, visible = _useParams_params.visible, size = _useParams_params.size, className = _useParams_params.className, style = _useParams_params.style, wordBreak = _useParams_params.wordBreak, zIndex = _useParams_params.zIndex, onClose = _useParams_params.onClose, setParams = _useParams.setParams;
    var classPrefix = 'nut-toast';
    var timer = useRef(-1);
    var rtl = useRtl();
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: visible,
        defaultValue: undefined,
        finalValue: false,
        onChange: function(v) {
            !v && (onClose === null || onClose === void 0 ? void 0 : onClose());
        }
    }), 2), innerVisible = _usePropsValue[0], setInnerVisible = _usePropsValue[1];
    useEffect(function() {
        if (innerVisible) {
            autoClose();
        }
    }, [
        innerVisible,
        duration
    ]);
    useCustomEvent(id, function(param) {
        var status = param.status, options = param.options;
        if (status) {
            options.visible = true;
            setParams(options);
            _$show();
        } else {
            setParams({
                visible: false
            });
            _$hide();
        }
    });
    var clearTimer = function() {
        if (timer.current) {
            clearTimeout(timer.current);
            timer.current = -1;
        }
    };
    var _$show = function() {
        setInnerVisible(true);
    };
    var _$hide = function() {
        clearTimer();
        setInnerVisible(false);
    };
    var autoClose = function() {
        clearTimer();
        if (duration) {
            timer.current = window.setTimeout(function() {
                setParams({
                    visible: false
                });
                _$hide();
            }, duration * 1000);
        }
    };
    var clickCover = function() {
        if (closeOnOverlayClick) {
            _$hide();
        }
    };
    var renderIcon = function() {
        var iconNode = icon;
        if (typeof icon === 'string') {
            iconNode = ({
                success: /*#__PURE__*/ React.createElement(Success, null),
                fail: /*#__PURE__*/ React.createElement(Failure, null),
                warn: /*#__PURE__*/ React.createElement(Tips, null),
                loading: /*#__PURE__*/ React.createElement(Loading, {
                    className: "nut-icon-loading"
                })
            })[icon];
        }
        return iconNode && /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-icon-wrapper")
        }, iconNode);
    };
    var classes = classNames({
        'nut-toast-has-icon': icon,
        'nut-toast-rtl': rtl
    });
    var styles = harmony() || td() ? {
        left: '50%',
        transform: 'translate(-50%, -50%)'
    } : null;
    return /*#__PURE__*/ React.createElement(React.Fragment, null, innerVisible && /*#__PURE__*/ React.createElement(Overlay, {
        visible: innerVisible,
        style: style,
        zIndex: zIndex,
        className: "".concat(classPrefix, "-overlay-default-taro ").concat(className),
        closeOnOverlayClick: closeOnOverlayClick,
        lockScroll: lockScroll,
        onClick: function() {
            clickCover();
        }
    }, /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, " ").concat(classes),
        id: id
    }, /*#__PURE__*/ React.createElement(View, {
        className: classNames("".concat(classPrefix, "-inner"), "".concat(classPrefix, "-").concat(position), contentClassName, "".concat(classPrefix, "-inner-").concat(size), "".concat(classPrefix, "-inner-").concat(wordBreak)),
        style: _object_spread({}, styles, contentStyle),
        ariaRole: "alert",
        ariaLabel: (title !== null && title !== void 0 ? title : '') + (content !== null && content !== void 0 ? content : '')
    }, renderIcon(), title && /*#__PURE__*/ React.createElement(Text, {
        className: "".concat(classPrefix, "-title")
    }, title), content && (td() ? /*#__PURE__*/ React.createElement(Text, {
        className: "".concat(classPrefix, "-text")
    }, content) : /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-text")
    }, content))))));
};
export function show(selector, options) {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    var path = useCustomEventsPath(selector);
    customEvents.trigger(path, {
        status: true,
        options: options
    });
}
export function hide(selector) {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    var path = useCustomEventsPath(selector);
    customEvents.trigger(path, {
        status: false
    });
}
Toast.displayName = 'NutToast';
Toast.show = show;
Toast.hide = hide;
