"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "ToastController", {
    enumerable: true,
    get: function() {
        return ToastController;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _styledcomponents = /*#__PURE__*/ _interop_require_wildcard(require("styled-components"));
var _Fade = require("../Fade");
var _utils = require("../../utils");
var _Toast = require("./Toast");
var _useToast = require("./useToast");
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 _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
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 _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);
}
var showAnimation = function showAnimation(position) {
    return (0, _styledcomponents.keyframes)([
        "0%{transform:translate(-50%,",
        "5rem);opacity:0;}80%{transform:translate(-50%,0);opacity:0.7;}100%{opacity:1;}"
    ], position === 'top' && '-');
};
var hideAnimation = function hideAnimation(position) {
    return (0, _styledcomponents.keyframes)([
        "0%{opacity:1;}20%{transform:translate(-50%,0);opacity:0.7;}100%{transform:translate(-50%,",
        "5rem);opacity:0;}"
    ], position === 'top' && '-');
};
var fadeIn = (0, _styledcomponents.keyframes)([
    "from{opacity:0;}to{opacity:1;}"
]);
var fadeOut = (0, _styledcomponents.keyframes)([
    "from{opacity:1;}to{opacity:0;}"
]);
var StyledFade = (0, _styledcomponents.default)(_Fade.Fade).withConfig({
    displayName: "ToastController__StyledFade",
    componentId: "sc-339da759-0"
})([
    "",
    ";"
], function(param) {
    var isVisible = param.isVisible;
    return (0, _styledcomponents.css)([
        "animation:300ms ",
        ";"
    ], isVisible ? fadeIn : fadeOut);
});
var StyledRoot = _styledcomponents.default.div.withConfig({
    displayName: "ToastController__StyledRoot",
    componentId: "sc-339da759-1"
})([
    "position:fixed;left:50%;z-index:1000;transform:translateX(-50%);",
    ";display:flex;width:0;",
    "{transform:translateX(-50%);max-width:calc(100vw - 5rem);flex-shrink:0;}"
], function(param) {
    var $position = param.$position, isVisible = param.isVisible, _param_$offset = param.$offset, $offset = _param_$offset === void 0 ? 0 : _param_$offset;
    return (0, _styledcomponents.css)([
        "",
        ":",
        "rem;animation:300ms ",
        ";"
    ], $position, $offset + 5, isVisible ? showAnimation($position) : hideAnimation($position));
}, _Toast.StyledRoot);
var ToastController = function ToastController(param) {
    var role = param.role, text = param.text, contentLeft = param.contentLeft, position = param.position, timeout = param.timeout, fade = param.fade, offset = param.offset;
    var hideToast = (0, _useToast.useToast)().hideToast;
    var _useState = _sliced_to_array((0, _react.useState)(true), 2), isVisible = _useState[0], setIsVisible = _useState[1];
    var hideTimeout = (0, _react.useRef)(null);
    var toastKey = "".concat(text).concat(position);
    var animationEndHandler = (0, _react.useCallback)(function() {
        if (!isVisible) {
            hideToast();
            (0, _utils.safeFlushSync)(function() {
                setIsVisible(true); // Необходимо вернуть булево к следующему вызову тоста
            });
        }
        if (isVisible) {
            hideTimeout.current = setTimeout(function() {
                setIsVisible(false);
            }, timeout);
        }
    // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [
        timeout,
        isVisible
    ]);
    (0, _react.useEffect)(function() {
        if (timeout === null && hideTimeout.current !== null) {
            clearTimeout(hideTimeout.current);
        }
    }, [
        timeout
    ]);
    (0, _react.useEffect)(function() {
        return function() {
            if (hideTimeout.current !== null) {
                clearTimeout(hideTimeout.current);
            }
        };
    }, []);
    if (!text || !position) {
        return null;
    }
    return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, fade && /*#__PURE__*/ _react.default.createElement(StyledFade, {
        isVisible: isVisible,
        placement: position
    }), /*#__PURE__*/ _react.default.createElement(StyledRoot, {
        key: toastKey,
        $position: position,
        $offset: offset,
        isVisible: isVisible,
        onAnimationEnd: animationEndHandler
    }, /*#__PURE__*/ _react.default.createElement(_Toast.Toast, {
        role: role,
        text: text,
        contentLeft: contentLeft
    })));
};
