import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
var _excluded = ["expectedResponseTime", "delayBeforeHide", "status", "disableAnimations"];
import React, { useContext, useRef } from 'react';
import { cx } from "../../../lib/theming/Emotion";
import { ZIndex } from "../../../internal/ZIndex";
import { ThemeContext } from "../../../lib/theming/ThemeContext";
import { CommonWrapper } from "../../../internal/CommonWrapper";
import { animations, styles } from "../GlobalLoaderView.styles";
import { useGlobalLoaderPosition, useGlobalLoaderWidth } from "../useParams";
export var GlobalLoaderView = function GlobalLoaderView(_ref) {
  var expectedResponseTime = _ref.expectedResponseTime,
    delayBeforeHide = _ref.delayBeforeHide,
    status = _ref.status,
    disableAnimations = _ref.disableAnimations,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var ref = useRef(null);
  var theme = useContext(ThemeContext);
  var _useGlobalLoaderWidth = useGlobalLoaderWidth(status, ref),
    width = _useGlobalLoaderWidth.width,
    startWidth = _useGlobalLoaderWidth.startWidth,
    fullWidth = _useGlobalLoaderWidth.fullWidth;
  var _useGlobalLoaderPosit = useGlobalLoaderPosition(ref),
    left = _useGlobalLoaderPosit.left;
  var getAnimationClass = function getAnimationClass(status) {
    if (!disableAnimations) {
      switch (status) {
        case 'success':
          return animations.successAnimation(delayBeforeHide, width, left);
        case 'accept':
          if (startWidth < fullWidth * 0.8) {
            return animations.acceptAnimation(theme, startWidth, expectedResponseTime, width, left);
          }
          return animations.slowAcceptAnimation(theme, startWidth, width, left);
        case 'error':
          return animations.errorAnimation(theme);
        case 'standard':
          return animations.standardAnimation(theme, expectedResponseTime);
      }
    }
    if (disableAnimations) {
      switch (status) {
        case 'success':
          return styles.successWithoutAnimation();
        case 'accept':
          return animations.acceptWithoutAnimation(startWidth);
        case 'error':
          return styles.errorWithoutAnimation();
        case 'standard':
          return styles.standardWithoutAnimation();
      }
    }
  };
  return /*#__PURE__*/(
    React.createElement(CommonWrapper, _extends({}, rest, {
      "data-status": status
    }), /*#__PURE__*/
    React.createElement(ZIndex, {
      priority: "GlobalLoader",
      className: styles.outer(theme)
    }, /*#__PURE__*/
    React.createElement("div", {
      ref: ref,
      className: cx(styles.inner(theme), getAnimationClass(status))
    })))
  );
};