import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props';
import _extends from 'babel-runtime/helpers/extends';
import Icon from '../icon';
import ListItemMixin from '../mixins/list-item';
import classNames from '../_utils/classnames';
import { isDef } from '../utils/commonUtil';
import './index.less';

var ListItem = {
    functional: true,
    name: 'SnListItem',
    mixins: [ListItemMixin],
    render: function render(h, context) {
        var props = context.props,
            slots = context.slots,
            data = context.data;
        var icon = props.icon,
            rightIcon = props.rightIcon,
            title = props.title,
            label = props.label,
            value = props.value,
            labelNumber = props.labelNumber,
            titleClass = props.titleClass,
            titleStyle = props.titleStyle,
            valueClass = props.valueClass,
            valueStyle = props.valueStyle,
            clickable = props.clickable,
            center = props.center,
            required = props.required,
            border = props.border,
            borderIndent = props.borderIndent,
            valuePosition = props.valuePosition;

        var $slots = slots();
        var showTitle = $slots.title || isDef(title);
        var showValue = $slots['default'] || isDef(value);
        var showLabel = $slots.label || isDef(label);

        var Label = showLabel && h(
            'div',
            {
                'class': {
                    'sn-list-item-label': true,
                    '': props.labelClass
                }
            },
            [$slots.label ? $slots.label : label]
        );

        var titleClassName = classNames('sn-list-item-title', labelNumber ? 'sn-list-item-label-number-' + labelNumber : '', titleClass);

        var Title = showTitle && h(
            'div',
            { 'class': titleClassName, style: titleStyle },
            [$slots.title ? $slots.title : h('span', [title]), Label]
        );

        var valueClassName = classNames('sn-list-item-value', valuePosition === 'left' ? 'sn-list-item-value-position-left' : '', valueClass);

        var Value = showValue && h(
            'div',
            { 'class': valueClassName, style: valueStyle },
            [$slots['default'] ? $slots['default'] : h('span', [value])]
        );

        var onLeftIcon = data.on && data.on['icon-click'] && {
            on: { click: data.on['icon-click'] }
        } || {};
        var LeftIcon = $slots.icon ? $slots.icon : icon && h(Icon, _mergeJSXProps([{
            attrs: {
                type: icon
            }
        }, _extends({}, onLeftIcon, {
            'class': 'sn-list-item-left-icon'
        })]));

        var rightIconSlot = $slots['right-icon'];
        var onRightIcon = data.on && data.on['right-icon-click'] && {
            on: { click: data.on['right-icon-click'] }
        } || {};
        var RightIcon = rightIconSlot ? rightIconSlot : rightIcon && h(Icon, _mergeJSXProps([{
            attrs: {
                type: rightIcon
            }
        }, _extends({}, onRightIcon, {
            'class': 'sn-list-item-right-icon'
        })]));

        var classes = classNames({
            'sn-list-item': true,
            'sn-list-item-clickable': clickable,
            'sn-list-item-center': center,
            'sn-list-item-required': required,
            'sn-list-item-borderless': !border,
            'sn-list-item-border-indentless': !borderIndent
        }, data['class']);

        var domProps = _extends({}, data, {
            attrs: {
                role: clickable ? 'button' : null,
                tabindex: clickable ? 0 : null
            },
            'class': classes
        });

        return h(
            'div',
            domProps,
            [LeftIcon, Title, Value, RightIcon]
        );
    }
};

ListItem.install = function (Vue) {
    Vue.component(ListItem.name, ListItem);
};

export default ListItem;