"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var React = require('react');
var Async_1 = require('../utilities/Async/Async');
var EventGroup_1 = require('../utilities/eventGroup/EventGroup');
var __errorCallbacks = [];
var BaseComponent = (function (_super) {
    __extends(BaseComponent, _super);
    function BaseComponent(props) {
        _super.call(this, props);
        _makeAllSafe(this, BaseComponent.prototype, [
            'componentWillMount',
            'componentDidMount',
            'componentWillUpdate',
            'componentDidUpdate',
            'componentWillUnmount',
            'render'
        ]);
    }
    BaseComponent.onError = function (cb) {
        __errorCallbacks.push(cb);
    };
    BaseComponent.prototype.componentWillUnmount = function () {
        if (this.__disposables) {
            for (var i = 0, len = this._disposables.length; i < len; i++) {
                this.__disposables[i].dispose();
            }
            this.__disposables = null;
        }
    };
    Object.defineProperty(BaseComponent.prototype, "className", {
        get: function () {
            var funcNameRegex = /function (.{1,})\(/;
            var results = (funcNameRegex).exec((this).constructor.toString());
            return (results && results.length > 1) ? results[1] : '';
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseComponent.prototype, "_disposables", {
        get: function () {
            if (!this.__disposables) {
                this.__disposables = [];
            }
            return this.__disposables;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseComponent.prototype, "_async", {
        get: function () {
            if (!this.__async) {
                this.__async = new Async_1.Async(this);
                this._disposables.push(this.__async);
            }
            return this.__async;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseComponent.prototype, "_events", {
        get: function () {
            if (!this.__events) {
                this.__events = new EventGroup_1.EventGroup(this);
                this._disposables.push(this.__events);
            }
            return this.__events;
        },
        enumerable: true,
        configurable: true
    });
    return BaseComponent;
}(React.Component));
exports.BaseComponent = BaseComponent;
function _makeAllSafe(obj, prototype, methodNames) {
    for (var i = 0, len = methodNames.length; i < len; i++) {
        _makeSafe(obj, prototype, methodNames[i]);
    }
}
function _makeSafe(obj, prototype, methodName) {
    var classMethod = obj[methodName];
    var prototypeMethod = prototype[methodName];
    obj[methodName] = function () {
        var retVal;
        try {
            if (prototypeMethod) {
                retVal = prototypeMethod.apply(this, arguments);
            }
            if (classMethod) {
                retVal = classMethod.apply(this, arguments);
            }
        }
        catch (e) {
            var errorMessage = "Exception in " + obj.className + "." + methodName + "(): " + (typeof e === 'string' ? e : e.stack);
            for (var i = 0, len = __errorCallbacks.length; i < len; i++) {
                __errorCallbacks[i](errorMessage, e);
            }
        }
        return retVal;
    };
}
BaseComponent.onError(function (errorMessage, ex) { return console.error(errorMessage); });

//# sourceMappingURL=BaseComponent.js.map
