'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var utils = require('../utils.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var ToastContext = /*#__PURE__*/React.createContext(null);
var useToastInner = function useToastInner() {
  var context = React.useContext(ToastContext);
  if (!context) {
    throw new Error('useToast must be used within a ToastProvider');
  }
  return context;
};
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: utils.DEFAULT_POSITION,
  fade: utils.DEFAULT_FADE,
  timeout: undefined,
  contentLeft: undefined,
  role: undefined,
  pilled: undefined,
  hasClose: undefined,
  width: undefined,
  size: 'm',
  view: 'primary'
};
var getShowToastCallSignature = function getShowToastCallSignature(args) {
  if (_rollupPluginBabelHelpers.typeof(args[0]) === 'object' && 'text' in args[0]) {
    return args[0];
  }

  // TODO: issue https://github.com/salute-developers/plasma/issues/333
  var _args = _rollupPluginBabelHelpers.slicedToArray(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
  };
};
var ToastProviderHoc = function ToastProviderHoc(ToastComponent) {
  return function ToastProvider(_ref) {
    var children = _ref.children,
      defaultToastArgs = _ref.defaultToastArgs;
    var _useState = React.useState(false),
      _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
      isVisible = _useState2[0],
      setIsVisible = _useState2[1];
    var _useState3 = React.useState(true),
      _useState4 = _rollupPluginBabelHelpers.slicedToArray(_useState3, 2),
      isHidden = _useState4[0],
      setIsHidden = _useState4[1];
    var hideTimeout = React.useRef(null);
    var animationRunTimeout = React.useRef(null);
    var _useState5 = React.useState(initialToastData),
      _useState6 = _rollupPluginBabelHelpers.slicedToArray(_useState5, 2),
      toastProps = _useState6[0],
      setToastProps = _useState6[1];
    var _useState7 = React.useState({
        timeout: undefined,
        onHide: undefined
      }),
      _useState8 = _rollupPluginBabelHelpers.slicedToArray(_useState7, 2),
      toastInfo = _useState8[0],
      setToastInfo = _useState8[1];
    var showToastCallback = function showToastCallback() {
      if (isVisible) {
        return;
      }
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }
      var _getShowToastCallSign = getShowToastCallSignature(args),
        _getShowToastCallSign2 = _getShowToastCallSign.text,
        text = _getShowToastCallSign2 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.text : _getShowToastCallSign2,
        _getShowToastCallSign3 = _getShowToastCallSign.offset,
        offset = _getShowToastCallSign3 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.offset : _getShowToastCallSign3,
        _getShowToastCallSign4 = _getShowToastCallSign.position,
        position = _getShowToastCallSign4 === void 0 ? (defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.position) || utils.DEFAULT_POSITION : _getShowToastCallSign4,
        _getShowToastCallSign5 = _getShowToastCallSign.fade,
        fade = _getShowToastCallSign5 === void 0 ? (defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.fade) || utils.DEFAULT_FADE : _getShowToastCallSign5,
        _getShowToastCallSign6 = _getShowToastCallSign.timeout,
        timeout = _getShowToastCallSign6 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.timeout : _getShowToastCallSign6,
        _getShowToastCallSign7 = _getShowToastCallSign.contentLeft,
        contentLeft = _getShowToastCallSign7 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.contentLeft : _getShowToastCallSign7,
        _getShowToastCallSign8 = _getShowToastCallSign.role,
        role = _getShowToastCallSign8 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.role : _getShowToastCallSign8,
        _getShowToastCallSign9 = _getShowToastCallSign.pilled,
        pilled = _getShowToastCallSign9 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.pilled : _getShowToastCallSign9,
        _getShowToastCallSign0 = _getShowToastCallSign.hasClose,
        hasClose = _getShowToastCallSign0 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.hasClose : _getShowToastCallSign0,
        _getShowToastCallSign1 = _getShowToastCallSign.size,
        size = _getShowToastCallSign1 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.size : _getShowToastCallSign1,
        _getShowToastCallSign10 = _getShowToastCallSign.view,
        view = _getShowToastCallSign10 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.view : _getShowToastCallSign10,
        _getShowToastCallSign11 = _getShowToastCallSign.width,
        width = _getShowToastCallSign11 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.width : _getShowToastCallSign11,
        _getShowToastCallSign12 = _getShowToastCallSign.closeIconType,
        closeIconType = _getShowToastCallSign12 === void 0 ? (defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.closeIconType) || utils.DEFAULT_CLOSE_ICON_TYPE : _getShowToastCallSign12,
        _getShowToastCallSign13 = _getShowToastCallSign.textColor,
        textColor = _getShowToastCallSign13 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.textColor : _getShowToastCallSign13,
        _getShowToastCallSign14 = _getShowToastCallSign.onHide,
        onHide = _getShowToastCallSign14 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.onHide : _getShowToastCallSign14,
        _getShowToastCallSign15 = _getShowToastCallSign.onShow,
        onShow = _getShowToastCallSign15 === void 0 ? defaultToastArgs === null || defaultToastArgs === void 0 ? void 0 : defaultToastArgs.onShow : _getShowToastCallSign15;
      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 || onShow();
    };
    var showToast = React.useCallback(showToastCallback, []);
    var onHide = toastInfo.onHide,
      timeout = toastInfo.timeout;
    var hideToast = React.useCallback(function () {
      if (hideTimeout !== null && hideTimeout !== void 0 && hideTimeout.current) {
        clearTimeout(hideTimeout.current);
        hideTimeout.current = null;
      }
      if (!isVisible) {
        return;
      }
      onHide === null || onHide === void 0 || onHide();
      setIsVisible(false);
      animationRunTimeout.current = setTimeout(function () {
        setIsHidden(true);
        setToastProps(initialToastData);
      }, utils.TIMER_DELAY);
    }, [onHide, isVisible, isHidden]);
    React.useEffect(function () {
      if (timeout === null && animationRunTimeout.current !== null) {
        clearTimeout(animationRunTimeout.current);
        animationRunTimeout.current = null;
      }
      return function () {
        if (animationRunTimeout !== null && animationRunTimeout !== void 0 && animationRunTimeout.current) {
          clearTimeout(animationRunTimeout.current);
        }
      };
    }, [timeout]);

    // очистка таймаутов перенесена из ToastController, т. к. в StrictМоde react делает дополнительный unmount
    React.useEffect(function () {
      if (timeout && isVisible) {
        hideTimeout.current = setTimeout(function () {
          hideToast();
        }, timeout);
      }
      return function () {
        if (hideTimeout !== null && hideTimeout !== void 0 && hideTimeout.current) {
          clearTimeout(hideTimeout.current);
          hideTimeout.current = null;
        }
      };
    }, [isVisible, timeout]);
    return /*#__PURE__*/React__default.default.createElement(ToastContext.Provider, {
      value: {
        showToast: showToast,
        hideToast: hideToast,
        isVisible: isVisible,
        hideTimeout: hideTimeout,
        animationRunTimeout: animationRunTimeout
      }
    }, children, !isHidden && /*#__PURE__*/React__default.default.createElement(ToastComponent, toastProps));
  };
};

exports.ToastContext = ToastContext;
exports.ToastProviderHoc = ToastProviderHoc;
exports.useToast = useToast;
exports.useToastInner = useToastInner;
