'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.Plugin = undefined;

var _map = require('babel-runtime/core-js/map');

var _map2 = _interopRequireDefault(_map);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

require('source-map-support/register');

var _AgentLogger = require('./AgentLogger');

var _AgentLogger2 = _interopRequireDefault(_AgentLogger);

var _events = require('events');

var _events2 = _interopRequireDefault(_events);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var log = _AgentLogger2.default.log();

var Plugin = exports.Plugin = function (_EventEmitter) {
    (0, _inherits3.default)(Plugin, _EventEmitter);

    function Plugin() {
        var _ret;

        (0, _classCallCheck3.default)(this, Plugin);

        var _this = (0, _possibleConstructorReturn3.default)(this, (Plugin.__proto__ || (0, _getPrototypeOf2.default)(Plugin)).call(this));

        return _ret = _this, (0, _possibleConstructorReturn3.default)(_this, _ret);
    }

    (0, _createClass3.default)(Plugin, null, [{
        key: 'isRegistered',
        value: function isRegistered(clazzname) {
            return this.registeredTypes.has(clazzname);
        }
    }, {
        key: 'register',
        value: function register(clazzname, clazz) {
            if (!(this.isRegistered(clazzname) && clazz.prototype instanceof this)) {
                this.registeredTypes.set(clazzname, clazz);
                log.info('Registered ' + this.name + ': ' + clazzname);
            } else {
                log.error('Cannot Register ' + clazzname + ', Invalid Type. Must be a subclass of ' + this.name);
            }
        }

        /**
         *
         * @param {string} clazzname - Class Name to Create
         * @param {*} options - Object of Options to pass into Class
         */

    }, {
        key: 'create',
        value: function create(clazzname) {
            if (!clazzname) return null;
            if (!this.registeredTypes.has(clazzname)) {
                log.error('Cannot Create Instance. Unknown Subclass: ' + clazzname);
                return null;
            }

            var clazz = this.registeredTypes.get(clazzname);

            for (var _len = arguments.length, options = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
                options[_key - 1] = arguments[_key];
            }

            var instance = new (Function.prototype.bind.apply(clazz, [null].concat(options)))();
            return instance;
        }
    }, {
        key: 'registeredTypes',
        get: function get() {
            if (this.hasOwnProperty('_registeredTypeMap')) {
                return this._registeredTypeMap;
            } else {
                this._registeredTypeMap = new _map2.default();
                return this._registeredTypeMap;
            }
        }
    }]);
    return Plugin;
}(_events2.default);