"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.GlobalLoader = void 0;

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _EventBus = _interopRequireDefault(require("../utils/EventBus"));

var _Loader = require("./Loader");

var _utils = require("../../utils");

var GlobalLoader =
/*#__PURE__*/
function (_Component) {
  (0, _inheritsLoose2.default)(GlobalLoader, _Component);

  function GlobalLoader(props) {
    var _this$eventHandlers;

    var _this;

    _this = _Component.call(this, props) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "show", function (text) {
      _this.setState(function (_ref) {
        var stack = _ref.stack,
            wasVisible = _ref.visible;
        stack = [].concat(stack, [text || '']);

        if (!wasVisible) {
          _EventBus.default.emit(_utils.constants.onTestAutomationAwait);
        }

        return {
          stack: stack,
          visible: true,
          content: _this.getContent(stack)
        };
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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.default.emit(_utils.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[_utils.constants.eventShowGlobalLoader] = function (text) {
      return _this.show(text);
    }, _this$eventHandlers[_utils.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.default.on(event, _this2.eventHandlers[event]);
    });
    this.props.hideEvents.forEach(function (event) {
      if (!_this2.eventHandlers[event]) {
        _this2.eventHandlers[event] = function () {
          return _this2.hide();
        };
      }

      _EventBus.default.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.default.off(event, _this3.eventHandlers[event]);
    });
    this.props.hideEvents.forEach(function (event) {
      return _EventBus.default.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 = (0, _objectWithoutPropertiesLoose2.default)(_this$props, ["showEvents", "hideEvents", "children"]);

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

    return null;
  };

  return GlobalLoader;
}(_react.Component);

exports.GlobalLoader = GlobalLoader;
(0, _defineProperty2.default)(GlobalLoader, "propTypes", Object.assign({}, _Loader.Loader.propTypes, {
  showEvents: _propTypes.default.arrayOf(_propTypes.default.string).isRequired,
  hideEvents: _propTypes.default.arrayOf(_propTypes.default.string).isRequired
}));
(0, _defineProperty2.default)(GlobalLoader, "defaultProps", _Loader.Loader.defaultProps);