import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import EventBus from '../utils/EventBus';
import { Loader } from './Loader';
import { constants } from '../../utils';
export var GlobalLoader =
/*#__PURE__*/
function (_Component) {
  _inheritsLoose(GlobalLoader, _Component);

  function GlobalLoader(props) {
    var _this$eventHandlers;

    var _this;

    _this = _Component.call(this, props) || this;

    _defineProperty(_assertThisInitialized(_this), "show", function (text) {
      _this.setState(function (_ref) {
        var stack = _ref.stack,
            wasVisible = _ref.visible;
        stack = [].concat(stack, [text || '']);

        if (!wasVisible) {
          EventBus.emit(constants.onTestAutomationAwait);
        }

        return {
          stack: stack,
          visible: true,
          content: _this.getContent(stack)
        };
      });
    });

    _defineProperty(_assertThisInitialized(_this), "hide", function (text) {
      _this.setState(function (_ref2) {
        var stack = _ref2.stack,
            wasVisible = _ref2.visible;

        if (!stack.length) {
          // eslint-disable-next-line
          console.error('GlobalLoader: hide-handler called when loader is already hidden');
          return;
        }

        var index = -1;

        if (text) {
          index = _this.getTextIndex(stack, text);
        }

        if (index === -1) {
          index = _this.getTextIndex(stack, '');
        }

        if (index === -1) {
          index = stack.length - 1;
        }

        stack.splice(index, 1);
        stack = [].concat(stack);
        var visible = stack.length > 0;

        if (wasVisible && !visible) {
          setTimeout(function () {
            return EventBus.emit(constants.onTestAutomationReady);
          });
        }

        return {
          stack: stack,
          visible: visible,
          content: _this.getContent(stack)
        };
      });
    });

    _this.state = {
      stack: [],
      visible: false,
      content: props.children
    };
    _this.eventHandlers = (_this$eventHandlers = {}, _this$eventHandlers[constants.eventShowGlobalLoader] = function (text) {
      return _this.show(text);
    }, _this$eventHandlers[constants.eventHideGlobalLoader] = function (text) {
      return _this.hide(text);
    }, _this$eventHandlers);

    if (props.showEvents.length !== props.hideEvents.length) {
      throw new Error('GlobalLoader: showEvents and hideEvents has a different length');
    }

    return _this;
  }

  var _proto = GlobalLoader.prototype;

  _proto.componentDidMount = function componentDidMount() {
    var _this2 = this;

    this.props.showEvents.forEach(function (event) {
      if (!_this2.eventHandlers[event]) {
        _this2.eventHandlers[event] = function () {
          return _this2.show();
        };
      }

      EventBus.on(event, _this2.eventHandlers[event]);
    });
    this.props.hideEvents.forEach(function (event) {
      if (!_this2.eventHandlers[event]) {
        _this2.eventHandlers[event] = function () {
          return _this2.hide();
        };
      }

      EventBus.on(event, _this2.eventHandlers[event]);
    });
  };

  _proto.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
    return nextState.visible !== this.state.visible || nextState.content !== this.state.content;
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    var _this3 = this;

    this.props.showEvents.forEach(function (event) {
      return EventBus.off(event, _this3.eventHandlers[event]);
    });
    this.props.hideEvents.forEach(function (event) {
      return EventBus.off(event, _this3.eventHandlers[event]);
    });
    this.eventHandlers = null;
  };

  _proto.getTextIndex = function getTextIndex(stack, text) {
    for (var index = stack.length - 1; index > -1; index--) {
      if (stack[index] === text) {
        return index;
      }
    }

    return -1;
  };

  _proto.getContent = function getContent(stack) {
    for (var index = stack.length - 1; index > -1; index--) {
      if (stack[index]) {
        return stack[index];
      }
    }

    return this.props.children;
  };

  _proto.render = function render() {
    var _this$props = this.props,
        showEvents = _this$props.showEvents,
        hideEvents = _this$props.hideEvents,
        children = _this$props.children,
        props = _objectWithoutPropertiesLoose(_this$props, ["showEvents", "hideEvents", "children"]);

    if (this.state.visible) {
      return React.createElement("div", {
        className: "global-loader"
      }, React.createElement(Loader, props, this.state.content));
    }

    return null;
  };

  return GlobalLoader;
}(Component);

_defineProperty(GlobalLoader, "propTypes", Object.assign({}, Loader.propTypes, {
  showEvents: PropTypes.arrayOf(PropTypes.string).isRequired,
  hideEvents: PropTypes.arrayOf(PropTypes.string).isRequired
}));

_defineProperty(GlobalLoader, "defaultProps", Loader.defaultProps);