"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var EventBus_1 = __importDefault(require("../utils/EventBus"));
var Loader_1 = require("./Loader");
var utils_1 = require("../../utils");
var GlobalLoader = /** @class */ (function (_super) {
    __extends(GlobalLoader, _super);
    function GlobalLoader(props) {
        var _a;
        var _this = _super.call(this, props) || this;
        _this.show = function (text) {
            if (text === void 0) { text = ''; }
            _this.setState(function (_a) {
                var stack = _a.stack, wasVisible = _a.visible;
                stack = stack.concat([text]);
                if (!wasVisible) {
                    EventBus_1.default.emit(utils_1.constants.onTestAutomationAwait);
                }
                return {
                    stack: stack,
                    visible: true,
                    content: _this.getContent(stack)
                };
            });
        };
        _this.hide = function (text) {
            if (text === void 0) { text = ''; }
            _this.setState(function (_a) {
                var stack = _a.stack, wasVisible = _a.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 = stack.slice();
                var visible = stack.length > 0;
                if (wasVisible && !visible) {
                    setTimeout(function () { return EventBus_1.default.emit(utils_1.constants.onTestAutomationReady); });
                }
                return {
                    stack: stack,
                    visible: visible,
                    content: _this.getContent(stack)
                };
            });
        };
        _this.state = {
            stack: [],
            visible: false,
            content: props.children
        };
        _this.eventHandlers = (_a = {},
            _a[utils_1.constants.eventShowGlobalLoader] = function (text) { return _this.show(text); },
            _a[utils_1.constants.eventHideGlobalLoader] = function (text) { return _this.hide(text); },
            _a);
        if (props.showEvents.length !== props.hideEvents.length) {
            throw new Error('GlobalLoader: showEvents and hideEvents has a different length');
        }
        return _this;
    }
    GlobalLoader.prototype.componentDidMount = function () {
        var _this = this;
        this.props.showEvents.forEach(function (event) {
            if (!_this.eventHandlers[event]) {
                _this.eventHandlers[event] = function () { return _this.show(); };
            }
            EventBus_1.default.on(event, _this.eventHandlers[event]);
        });
        this.props.hideEvents.forEach(function (event) {
            if (!_this.eventHandlers[event]) {
                _this.eventHandlers[event] = function () { return _this.hide(); };
            }
            EventBus_1.default.on(event, _this.eventHandlers[event]);
        });
    };
    GlobalLoader.prototype.shouldComponentUpdate = function (nextProps, nextState) {
        return nextState.visible !== this.state.visible || nextState.content !== this.state.content;
    };
    GlobalLoader.prototype.componentWillUnmount = function () {
        var _this = this;
        this.props.showEvents.forEach(function (event) { return EventBus_1.default.off(event, _this.eventHandlers[event]); });
        this.props.hideEvents.forEach(function (event) { return EventBus_1.default.off(event, _this.eventHandlers[event]); });
        this.eventHandlers = null;
    };
    GlobalLoader.prototype.getTextIndex = function (stack, text) {
        for (var index = stack.length - 1; index > -1; index--) {
            if (stack[index] === text) {
                return index;
            }
        }
        return -1;
    };
    GlobalLoader.prototype.getContent = function (stack) {
        for (var index = stack.length - 1; index > -1; index--) {
            if (stack[index]) {
                return stack[index];
            }
        }
        return this.props.children;
    };
    GlobalLoader.prototype.render = function () {
        var _a = this.props, showEvents = _a.showEvents, hideEvents = _a.hideEvents, children = _a.children, props = __rest(_a, ["showEvents", "hideEvents", "children"]);
        if (this.state.visible) {
            return (react_1.default.createElement("div", { className: "global-loader" },
                react_1.default.createElement(Loader_1.Loader, __assign({}, props), this.state.content)));
        }
        return null;
    };
    GlobalLoader.propTypes = __assign({}, Loader_1.Loader.propTypes, { showEvents: prop_types_1.default.arrayOf(prop_types_1.default.string).isRequired, hideEvents: prop_types_1.default.arrayOf(prop_types_1.default.string).isRequired });
    GlobalLoader.defaultProps = Loader_1.Loader.defaultProps;
    return GlobalLoader;
}(react_1.Component));
exports.GlobalLoader = GlobalLoader;
//# sourceMappingURL=GlobalLoader.js.map