import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props';

import warning from 'warning';
import PropTypes from '../../_util/vue-types';
import BaseMixin from '../../_util/BaseMixin';
import { getOptionProps, getComponentFromProp } from '../../_util/props-util';

function noop() {}
export default {
  name: 'TabBarTabsNode',
  mixins: [BaseMixin],
  props: {
    activeKey: PropTypes.string,
    panels: PropTypes.any.def([]),
    prefixCls: PropTypes.string.def(''),
    tabBarGutter: PropTypes.any.def(null),
    onTabClick: PropTypes.func,
    saveRef: PropTypes.func.def(noop),
    getRef: PropTypes.func.def(noop)
  },
  render: function render() {
    var _this = this;

    var h = arguments[0];
    var _$props = this.$props,
        children = _$props.panels,
        activeKey = _$props.activeKey,
        prefixCls = _$props.prefixCls,
        tabBarGutter = _$props.tabBarGutter;

    var rst = [];

    children.forEach(function (child, index) {
      if (!child) {
        return;
      }
      var props = getOptionProps(child);
      var key = child.key;
      var cls = activeKey === key ? prefixCls + '-tab-active' : '';
      cls += ' ' + prefixCls + '-tab';
      var events = { on: {} };
      var disabled = props.disabled || props.disabled === '';
      if (disabled) {
        cls += ' ' + prefixCls + '-tab-disabled';
      } else {
        events.on.click = function () {
          _this.__emit('tabClick', key);
        };
      }
      var directives = [];
      if (activeKey === key) {
        directives.push({
          name: 'ref',
          value: _this.saveRef('activeTab')
        });
      }
      var tab = getComponentFromProp(child, 'tab');
      warning(tab !== undefined, 'There must be `tab` property or slot on children of Tabs.');
      rst.push(h(
        'div',
        _mergeJSXProps([{
          attrs: {
            role: 'tab',
            'aria-disabled': disabled ? 'true' : 'false',
            'aria-selected': activeKey === key ? 'true' : 'false'
          }
        }, events, {
          'class': cls,
          key: key,
          style: { marginRight: tabBarGutter && index === children.length - 1 ? 0 : tabBarGutter }
        }, { directives: directives }]),
        [tab]
      ));
    });

    return h('div', [rst]);
  }
};