function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _type_of(obj) {
    "@swc/helpers - typeof";
    return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { createContext, useCallback, useContext, useEffect, useRef, useState } from "react";
import { DEFAULT_CLOSE_ICON_TYPE, DEFAULT_FADE, DEFAULT_POSITION, TIMER_DELAY } from "../utils";
export var ToastContext = /*#__PURE__*/ createContext(null);
export var useToastInner = function useToastInner() {
    var context = useContext(ToastContext);
    if (!context) {
        throw new Error('useToast must be used within a ToastProvider');
    }
    return context;
};
export var useToast = function useToast() {
    var _useToastInner = useToastInner(), showToast = _useToastInner.showToast, hideToast = _useToastInner.hideToast;
    return {
        showToast: showToast,
        hideToast: hideToast
    };
};
var initialToastData = {
    text: undefined,
    offset: undefined,
    position: DEFAULT_POSITION,
    fade: DEFAULT_FADE,
    timeout: undefined,
    contentLeft: undefined,
    role: undefined,
    pilled: undefined,
    hasClose: undefined,
    width: undefined,
    size: 'm',
    view: 'primary'
};
var getShowToastCallSignature = function getShowToastCallSignature(args) {
    if (_type_of(args[0]) === 'object' && 'text' in args[0]) {
        return args[0];
    }
    // TODO: issue https://github.com/salute-developers/plasma/issues/333
    var _args = _sliced_to_array(args, 16), text = _args[0], position = _args[1], timeout = _args[2], fade = _args[3], contentLeft = _args[4], role = _args[5], offset = _args[6], pilled = _args[7], width = _args[8], hasClose = _args[9], size = _args[10], view = _args[11], closeIconType = _args[12], textColor = _args[13], onHide = _args[14], onShow = _args[15];
    return {
        text: text === undefined ? undefined : String(text),
        position: position,
        timeout: timeout,
        fade: fade,
        contentLeft: contentLeft,
        role: role,
        offset: offset,
        pilled: pilled,
        width: width,
        hasClose: hasClose,
        size: size,
        view: view,
        closeIconType: closeIconType,
        textColor: textColor,
        onHide: onHide,
        onShow: onShow
    };
};
export var ToastProviderHoc = function ToastProviderHoc(ToastComponent) {
    return function ToastProvider(param) {
        var children = param.children, defaultToastArgs = param.defaultToastArgs;
        var _useState = _sliced_to_array(useState(false), 2), isVisible = _useState[0], setIsVisible = _useState[1];
        var _useState1 = _sliced_to_array(useState(true), 2), isHidden = _useState1[0], setIsHidden = _useState1[1];
        var hideTimeout = useRef(null);
        var animationRunTimeout = useRef(null);
        var _useState2 = _sliced_to_array(useState(initialToastData), 2), toastProps = _useState2[0], setToastProps = _useState2[1];
        var _useState3 = _sliced_to_array(useState({
            timeout: undefined,
            onHide: undefined
        }), 2), toastInfo = _useState3[0], setToastInfo = _useState3[1];
        var showToastCallback = function showToastCallback() {
            for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
                args[_key] = arguments[_key];
            }
            if (isVisible) {
                return;
            }
            var _getShowToastCallSignature = getShowToastCallSignature(args), _getShowToastCallSignature_text = _getShowToastCallSignature.text, text = _getShowToastCallSignature_text === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.text : _getShowToastCallSignature_text, _getShowToastCallSignature_offset = _getShowToastCallSignature.offset, offset = _getShowToastCallSignature_offset === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.offset : _getShowToastCallSignature_offset, _getShowToastCallSignature_position = _getShowToastCallSignature.position, position = _getShowToastCallSignature_position === void 0 ? (defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.position) || DEFAULT_POSITION : _getShowToastCallSignature_position, _getShowToastCallSignature_fade = _getShowToastCallSignature.fade, fade = _getShowToastCallSignature_fade === void 0 ? (defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.fade) || DEFAULT_FADE : _getShowToastCallSignature_fade, _getShowToastCallSignature_timeout = _getShowToastCallSignature.timeout, timeout = _getShowToastCallSignature_timeout === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.timeout : _getShowToastCallSignature_timeout, _getShowToastCallSignature_contentLeft = _getShowToastCallSignature.contentLeft, contentLeft = _getShowToastCallSignature_contentLeft === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.contentLeft : _getShowToastCallSignature_contentLeft, _getShowToastCallSignature_role = _getShowToastCallSignature.role, role = _getShowToastCallSignature_role === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.role : _getShowToastCallSignature_role, _getShowToastCallSignature_pilled = _getShowToastCallSignature.pilled, pilled = _getShowToastCallSignature_pilled === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.pilled : _getShowToastCallSignature_pilled, _getShowToastCallSignature_hasClose = _getShowToastCallSignature.hasClose, hasClose = _getShowToastCallSignature_hasClose === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.hasClose : _getShowToastCallSignature_hasClose, _getShowToastCallSignature_size = _getShowToastCallSignature.size, size = _getShowToastCallSignature_size === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.size : _getShowToastCallSignature_size, _getShowToastCallSignature_view = _getShowToastCallSignature.view, view = _getShowToastCallSignature_view === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.view : _getShowToastCallSignature_view, _getShowToastCallSignature_width = _getShowToastCallSignature.width, width = _getShowToastCallSignature_width === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.width : _getShowToastCallSignature_width, _getShowToastCallSignature_closeIconType = _getShowToastCallSignature.closeIconType, closeIconType = _getShowToastCallSignature_closeIconType === void 0 ? (defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.closeIconType) || DEFAULT_CLOSE_ICON_TYPE : _getShowToastCallSignature_closeIconType, _getShowToastCallSignature_textColor = _getShowToastCallSignature.textColor, textColor = _getShowToastCallSignature_textColor === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.textColor : _getShowToastCallSignature_textColor, _getShowToastCallSignature_onHide = _getShowToastCallSignature.onHide, onHide = _getShowToastCallSignature_onHide === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.onHide : _getShowToastCallSignature_onHide, _getShowToastCallSignature_onShow = _getShowToastCallSignature.onShow, onShow = _getShowToastCallSignature_onShow === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.onShow : _getShowToastCallSignature_onShow;
            setToastProps({
                text: text,
                width: width,
                position: position,
                offset: offset,
                fade: fade,
                contentLeft: contentLeft,
                role: role,
                pilled: pilled,
                hasClose: hasClose,
                size: size,
                view: view,
                closeIconType: closeIconType,
                textColor: textColor
            });
            setToastInfo({
                timeout: timeout,
                onHide: onHide
            });
            setIsHidden(false);
            setIsVisible(true);
            onShow === null || onShow === void 0 ? void 0 : onShow();
        };
        var showToast = useCallback(showToastCallback, []);
        var onHide = toastInfo.onHide, timeout = toastInfo.timeout;
        var hideToast = useCallback(function() {
            if (hideTimeout === null || hideTimeout === void 0 ? void 0 : hideTimeout.current) {
                clearTimeout(hideTimeout.current);
                hideTimeout.current = null;
            }
            if (!isVisible) {
                return;
            }
            onHide === null || onHide === void 0 ? void 0 : onHide();
            setIsVisible(false);
            animationRunTimeout.current = setTimeout(function() {
                setIsHidden(true);
                setToastProps(initialToastData);
            }, TIMER_DELAY);
        }, [
            onHide,
            isVisible,
            isHidden
        ]);
        useEffect(function() {
            if (timeout === null && animationRunTimeout.current !== null) {
                clearTimeout(animationRunTimeout.current);
                animationRunTimeout.current = null;
            }
            return function() {
                if (animationRunTimeout === null || animationRunTimeout === void 0 ? void 0 : animationRunTimeout.current) {
                    clearTimeout(animationRunTimeout.current);
                }
            };
        }, [
            timeout
        ]);
        // очистка таймаутов перенесена из ToastController, т. к. в StrictМоde react делает дополнительный unmount
        useEffect(function() {
            if (timeout && isVisible) {
                hideTimeout.current = setTimeout(function() {
                    hideToast();
                }, timeout);
            }
            return function() {
                if (hideTimeout === null || hideTimeout === void 0 ? void 0 : hideTimeout.current) {
                    clearTimeout(hideTimeout.current);
                    hideTimeout.current = null;
                }
            };
        }, [
            isVisible,
            timeout
        ]);
        return /*#__PURE__*/ React.createElement(ToastContext.Provider, {
            value: {
                showToast: showToast,
                hideToast: hideToast,
                isVisible: isVisible,
                hideTimeout: hideTimeout,
                animationRunTimeout: animationRunTimeout
            }
        }, children, !isHidden && /*#__PURE__*/ React.createElement(ToastComponent, toastProps));
    };
};
