'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

exports.inherit = inherit;
exports.emit = emit;
exports.mount = mount;

var _vue = require('vue');

var _vue2 = _interopRequireDefault(_vue);

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

var inheritKey = ['ref', 'style', 'class', 'attrs', 'nativeOn', 'directives', 'staticClass', 'staticStyle'];

var mapInheritKey = {
    nativeOn: 'on'
};

// inherit partial context, map nativeOn to on
function inherit(context, inheritListeners) {
    var result = inheritKey.reduce(function (obj, key) {
        if (context.data[key]) {
            obj[mapInheritKey[key] || key] = context.data[key];
        }
        return obj;
    }, {});

    if (inheritListeners) {
        result.on = result.on || {};
        (0, _extends3['default'])(result.on, context.data.on);
    }

    return result;
}

// emit event
function emit(context, eventName) {
    for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
        args[_key - 2] = arguments[_key];
    }

    var listeners = context.listeners[eventName];
    if (listeners) {
        if (Array.isArray(listeners)) {
            listeners.forEach(function (listener) {
                listener.apply(undefined, args);
            });
        } else {
            listeners.apply(undefined, args);
        }
    }
}

// mount functional component
function mount(Component, data) {
    var instance = new _vue2['default']({
        el: document.createElement('div'),
        props: Component.props,
        render: function render(h) {
            return h(Component, (0, _extends3['default'])({
                props: this.$props
            }, data));
        }
    });

    document.body.appendChild(instance.$el);

    return instance;
}