'use strict';

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

var _from = require('babel-runtime/core-js/array/from');

var _from2 = _interopRequireDefault(_from);

var _getIterator2 = require('babel-runtime/core-js/get-iterator');

var _getIterator3 = _interopRequireDefault(_getIterator2);

var _assign = require('babel-runtime/core-js/object/assign');

var _assign2 = _interopRequireDefault(_assign);

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

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

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);

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

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

exports.default = function () {
    var Base = function Base() {
        (0, _classCallCheck3.default)(this, Base);
    };

    var Component = function (_Base) {
        (0, _inherits3.default)(Component, _Base);

        function Component(config) {
            (0, _classCallCheck3.default)(this, Component);

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

            _this.config = config;
            _this.items = [];
            _this.events = {};
            _this.init();

            if (config.renderTo) {
                _this.render(config.renderTo);
            }
            return _this;
        }

        (0, _createClass3.default)(Component, [{
            key: 'init',
            value: function init() {
                (0, _assign2.default)(this, {
                    render: (0, _sinon.spy)(function (el) {
                        var div = document.createElement('div');
                        div.className = this.getClsName();
                        div._extCmp = this;
                        if (el) el.appendChild(div);
                        this.el = this.renderElement = { dom: div };

                        if (this.config.html) {
                            div.innerHTML = this.config.html;
                        }

                        if (this.config.items) {
                            var _iteratorNormalCompletion = true;
                            var _didIteratorError = false;
                            var _iteratorError = undefined;

                            try {
                                for (var _iterator = (0, _getIterator3.default)(this.config.items), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                                    var _item = _step.value;

                                    div.appendChild(_item.render());
                                }
                            } catch (err) {
                                _didIteratorError = true;
                                _iteratorError = err;
                            } finally {
                                try {
                                    if (!_iteratorNormalCompletion && _iterator.return) {
                                        _iterator.return();
                                    }
                                } finally {
                                    if (_didIteratorError) {
                                        throw _iteratorError;
                                    }
                                }
                            }
                        }var events = this.events['afterrender'] || this.events['painted'];

                        if (events) {
                            var _iteratorNormalCompletion2 = true;
                            var _didIteratorError2 = false;
                            var _iteratorError2 = undefined;

                            try {
                                for (var _iterator2 = (0, _getIterator3.default)(events), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
                                    var _handler = _step2.value;

                                    _handler(this.el || this.renderElement);
                                }
                            } catch (err) {
                                _didIteratorError2 = true;
                                _iteratorError2 = err;
                            } finally {
                                try {
                                    if (!_iteratorNormalCompletion2 && _iterator2.return) {
                                        _iterator2.return();
                                    }
                                } finally {
                                    if (_didIteratorError2) {
                                        throw _iteratorError2;
                                    }
                                }
                            }
                        }return div;
                    }),

                    destroy: (0, _sinon.spy)(function () {
                        var el = this.el || this.renderElement;

                        if (el.dom.parentNode && el.dom.parentNode._extCmp) {
                            el.dom.parentNode && el.dom.parentNode.removeChild(el.dom);
                        }
                    }),

                    insert: (0, _sinon.spy)(function (index, child) {
                        var el = this.el.dom;
                        var target = el.childNodes[index + 1];
                        var childEl = child.render();
                        if (target) {
                            el.insertBefore(childEl, target);
                        } else {
                            el.appendChild(childEl);
                        }
                        child.ownerCt = this;
                        this.items.splice(index, 0, child);
                    }),

                    add: (0, _sinon.spy)(function (child) {
                        child.render(this.el.dom);
                        child.ownerCt = this;
                        this.items.push(child);
                    }),

                    setConfig: (0, _sinon.spy)(function (config) {
                        this.config = config;
                    })
                });
            }
        }, {
            key: 'on',
            value: function on(event, handler, scope) {
                var events = this.events[event] || (this.events[event] = []);
                events.push(handler.bind(scope));
            }
        }, {
            key: 'getClsName',
            value: function getClsName() {
                return 'x-component';
            }
        }]);
        return Component;
    }(Base);

    global.Ext = {
        ClassManager: {
            getByAlias: function getByAlias(alias) {
                return function (_Component) {
                    (0, _inherits3.default)(_class, _Component);

                    function _class() {
                        (0, _classCallCheck3.default)(this, _class);
                        return (0, _possibleConstructorReturn3.default)(this, (_class.__proto__ || (0, _getPrototypeOf2.default)(_class)).apply(this, arguments));
                    }

                    (0, _createClass3.default)(_class, [{
                        key: 'getClsName',
                        value: function getClsName() {
                            return 'x-' + alias.replace(/^widget\./, '');
                        }
                    }]);
                    return _class;
                }(Component);
            }
        },
        Array: {
            union: function union(a, b) {
                var result = (0, _from2.default)(a);
                var _iteratorNormalCompletion3 = true;
                var _didIteratorError3 = false;
                var _iteratorError3 = undefined;

                try {
                    for (var _iterator3 = (0, _getIterator3.default)(b), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
                        var item = _step3.value;
                        if (result.indexOf(item) !== -1) result.push(b);
                    }
                } catch (err) {
                    _didIteratorError3 = true;
                    _iteratorError3 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion3 && _iterator3.return) {
                            _iterator3.return();
                        }
                    } finally {
                        if (_didIteratorError3) {
                            throw _iteratorError3;
                        }
                    }
                }

                return result;
            }
        },
        String: {
            capitalize: function capitalize(str) {
                return str[0].toUpperCase() + str.slice(1);
            }
        },
        Base: Base,
        Component: Component
    };
};

var _sinon = require('sinon');

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

;
//# sourceMappingURL=mockExt.js.map