"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get Notify () {
        return Notify;
    },
    get close () {
        return close;
    },
    get open () {
        return open;
    }
});
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 _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _reacttransitiongroup = require("react-transition-group");
var _components = require("@tarojs/components");
var _iconsreacttaro = require("@nutui/icons-react-taro");
var _platform = require("../../utils/taro/platform");
var _pxtransform = require("../../utils/taro/px-transform");
var _typings = require("../../utils/typings");
var _usecustomevent = require("../../hooks/taro/use-custom-event");
var _mergeprops = require("../../utils/merge-props");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    id: '',
    distance: 8,
    navHeight: 57,
    position: 'top',
    visible: false,
    closeable: false,
    leftIcon: null,
    rightIcon: null,
    duration: 3000,
    onClose: function onClose() {},
    onClick: function onClick() {}
});
var classPrefix = 'nut-notify';
var Notify = function Notify(props) {
    var _mergeProps = (0, _mergeprops.mergeProps)(defaultProps, props), id = _mergeProps.id, style = _mergeProps.style, children = _mergeProps.children, distance = _mergeProps.distance, navHeight = _mergeProps.navHeight, closeable = _mergeProps.closeable, leftIcon = _mergeProps.leftIcon, rightIcon = _mergeProps.rightIcon, position = _mergeProps.position, visible = _mergeProps.visible, duration = _mergeProps.duration, className = _mergeProps.className, onClose = _mergeProps.onClose, onClick = _mergeProps.onClick;
    (0, _usecustomevent.useCustomEvent)(id, function(status) {
        status ? show() : hide();
    });
    var timer;
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), showNotify = _useState[0], setShowNotify = _useState[1];
    (0, _react.useEffect)(function() {
        if (visible) {
            show();
        } else {
            hide();
        }
    }, [
        visible
    ]);
    var show = function show() {
        setShowNotify(true);
        clearTimer();
        if (duration) {
            timer = window.setTimeout(function() {
                hide();
            }, duration);
        }
    };
    var clearTimer = function clearTimer() {
        if (timer) {
            clearTimeout(timer);
            timer = null;
        }
    };
    var hide = function hide() {
        setShowNotify(false);
        onClose();
    };
    var getDistance = (0, _react.useMemo)(function() {
        if (position === 'top') {
            return {
                top: (0, _pxtransform.pxTransform)((0, _platform.web)() ? Number(distance) + navHeight : Number(distance))
            };
        }
        return {
            bottom: (0, _pxtransform.pxTransform)(Number(distance))
        };
    }, [
        distance,
        position
    ]);
    var _obj;
    var classes = (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix), true), (0, _define_property._)(_obj, "".concat(className), true), _obj));
    var handleClick = function handleClick() {
        onClick === null || onClick === void 0 ? void 0 : onClick();
    };
    var handleClickIcon = function handleClickIcon() {
        hide();
    };
    var _obj1;
    return /*#__PURE__*/ _react.default.createElement(_reacttransitiongroup.CSSTransition, {
        in: showNotify,
        timeout: 300,
        classNames: "fade",
        unmountOnExit: true,
        appear: true,
        position: position,
        id: id
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: classes,
        style: (0, _object_spread._)({}, style, getDistance),
        onClick: handleClick
    }, leftIcon ? /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, "-left-icon")
    }, leftIcon) : null, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)((_obj1 = {}, (0, _define_property._)(_obj1, "".concat(classPrefix, "-content"), true), (0, _define_property._)(_obj1, "".concat(classPrefix, "-ellipsis"), closeable || rightIcon), (0, _define_property._)(_obj1, "".concat(classPrefix, "-layout-left"), leftIcon || rightIcon), _obj1))
    }, children), rightIcon || closeable ? /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, "-right-icon"),
        onClick: handleClickIcon
    }, rightIcon || (closeable ? /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.Close, {
        className: "".concat(classPrefix, "-close-icon")
    }) : null)) : null));
};
function open(selector) {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    var path = (0, _usecustomevent.useCustomEventsPath)(selector);
    _usecustomevent.customEvents.trigger(path, true);
}
function close(selector) {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    var path = (0, _usecustomevent.useCustomEventsPath)(selector);
    _usecustomevent.customEvents.trigger(path, false);
}
Notify.displayName = 'NutNotify';
Notify.open = open;
Notify.close = close;
