'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

exports.install = install;
exports.reactify = reactify;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _ExtJSComponent2 = require('./ExtJSComponent');

var _ExtJSComponent3 = _interopRequireDefault(_ExtJSComponent2);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var settings = {};

/**
 * Configures React to resolve jsx tags.
 * @param {String} [viewport=true] Adds a stylesheet that mimics an Ext JS Viewport
 *    by setting the html, body, and react root element to height: 100%. Set this to true when using an
 *    Ext JS component at the root of your app.
 */
function install() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var _ref$viewport = _ref.viewport;
    var viewport = _ref$viewport === undefined ? false : _ref$viewport;

    settings.viewport = viewport;

    if (viewport) {
        var style = document.createElement('style');
        style.innerHTML = 'html, body, div[data-reactroot] { height: 100%; }';
        document.head.appendChild(style);
    }
};

/**
 * Creates a react component for a given Ext JS component.
 *
 *  Single class example: const Grid = reactify('grid');
 *
 *  Multiple class example: const [ Grid, Panel ] = reactify('Grid', 'Panel');
 *
 * @param {String[]/Ext.Class[]} ...targets xtypes or instances of Ext.Class.
 * @returns {React.Component/React.Component[]} If a single argument is passed a single React.Component class is returned. If multiple arguments are passed, an array of React.Component classes is returned.
 */
function reactify() {
    var result = [];

    for (var _len = arguments.length, targets = Array(_len), _key = 0; _key < _len; _key++) {
        targets[_key] = arguments[_key];
    }

    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
        var _loop = function _loop() {
            var target = _step.value;

            if (typeof target === 'string') {
                var _name = target;
                target = Ext.ClassManager.getByAlias('widget.' + target);
                if (!target) throw new Error('No xtype "' + _name + '" found.  Perhaps you need to require it with Ext.require("' + _name + '")?');
            }

            var name = target.getName && target.getName();

            result.push(function (_ExtJSComponent) {
                _inherits(_class, _ExtJSComponent);

                function _class() {
                    _classCallCheck(this, _class);

                    return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
                }

                _createClass(_class, [{
                    key: 'createExtJSComponent',
                    value: function createExtJSComponent(config) {
                        return new target(config);
                    }
                }, {
                    key: 'reactorSettings',
                    get: function get() {
                        return settings;
                    }
                }], [{
                    key: 'name',
                    get: function get() {
                        return name;
                    }
                }]);

                return _class;
            }(_ExtJSComponent3.default));
        };

        for (var _iterator = targets[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            _loop();
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally {
        try {
            if (!_iteratorNormalCompletion && _iterator.return) {
                _iterator.return();
            }
        } finally {
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }

    if (targets.length === 1) {
        return result[0];
    } else {
        return result;
    }
}
//# sourceMappingURL=data:application/json;base64, 