'use strict';

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

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _icon = require('../icon');

var _icon2 = _interopRequireDefault(_icon);

var _relation = require('./relation');

var _classnames4 = require('../_utils/classnames');

var _classnames5 = _interopRequireDefault(_classnames4);

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

var CheckboxMixin = exports.CheckboxMixin = function CheckboxMixin(_ref) {
    var parent = _ref.parent,
        role = _ref.role;
    return {
        mixins: [(0, _relation.ChildrenMixin)(parent)],

        props: {
            name: null,
            value: null,
            disabled: Boolean,
            iconSize: [Number, String],
            checkedColor: String,
            labelPosition: String,
            labelDisabled: Boolean,
            shape: {
                type: String,
                'default': 'dot',
                validator: function validator(value) {
                    return ['dot', 'check'].indexOf(value) > -1;
                }
            },
            bindGroup: {
                type: Boolean,
                'default': true
            }
        },

        computed: {
            disableBindRelation: function disableBindRelation() {
                return !this.bindGroup;
            },
            isDisabled: function isDisabled() {
                return this.parent && this.parent.disabled || this.disabled;
            },
            iconStyle: function iconStyle() {
                var checkedColor = this.checkedColor || this.parent && this.parent.checkedColor;

                if (checkedColor && this.checked && !this.isDisabled) {
                    return {
                        borderColor: checkedColor,
                        backgroundColor: checkedColor
                    };
                }
            },
            tabindex: function tabindex() {
                if (this.isDisabled || role === 'radio' && !this.checked) {
                    return -1;
                }

                return 0;
            }
        },

        methods: {
            onClick: function onClick(event) {
                var target = event.target;
                var icon = this.$refs.icon;

                var iconClicked = icon === target || icon.contains(target);

                if (!this.isDisabled && (iconClicked || !this.labelDisabled)) {
                    this.toggle();
                }

                this.$emit('click', event);
            },
            genIcon: function genIcon() {
                var _classnames;

                var h = this.$createElement;
                var shape = this.shape,
                    checked = this.checked;

                var iconSize = this.iconSize || this.parent && this.parent.iconSize;
                var iconType = '';
                if (role === 'radio') {
                    iconType = shape !== 'dot' ? 'radio-' + shape : 'radio';
                } else if (role === 'checkbox') {
                    iconType = 'checkbox';
                }

                return h(
                    'div',
                    {
                        ref: 'icon',
                        'class': (0, _classnames5['default'])((_classnames = {}, (0, _defineProperty3['default'])(_classnames, 'sn-' + role + '-icon', true), (0, _defineProperty3['default'])(_classnames, 'sn-' + role + '-icon-' + this.shape, true), (0, _defineProperty3['default'])(_classnames, 'sn-' + role + '-icon-disabled', this.isDisabled), (0, _defineProperty3['default'])(_classnames, 'sn-' + role + '-icon-checked', checked), _classnames)),
                        style: { fontSize: iconSize }
                    },
                    [this.$slots['icon'] || h(_icon2['default'], {
                        attrs: {
                            type: checked ? iconType : role + '-empty'
                        },
                        style: this.iconStyle
                    })]
                );
            },
            genLabel: function genLabel() {
                var h = this.$createElement;
                var labelPosition = this.labelPosition,
                    isDisabled = this.isDisabled,
                    $slots = this.$slots;


                if ($slots['default']) {
                    var _classnames2;

                    return h(
                        'span',
                        {
                            'class': (0, _classnames5['default'])((_classnames2 = {}, (0, _defineProperty3['default'])(_classnames2, 'sn-' + role + '-label', true), (0, _defineProperty3['default'])(_classnames2, 'sn-' + role + '-label-' + this.labelPosition, labelPosition), (0, _defineProperty3['default'])(_classnames2, 'sn-' + role + '-label-disabled', isDisabled), _classnames2))
                        },
                        [$slots['default']]
                    );
                }
            }
        },

        render: function render() {
            var _classnames3;

            var h = arguments[0];

            var Children = [this.genIcon()];

            if (this.labelPosition === 'left') {
                Children.unshift(this.genLabel());
            } else {
                Children.push(this.genLabel());
            }

            return h(
                'div',
                {
                    attrs: {
                        role: role,

                        tabindex: this.tabindex,
                        'aria-checked': String(this.checked)
                    },
                    'class': (0, _classnames5['default'])((_classnames3 = {}, (0, _defineProperty3['default'])(_classnames3, 'sn-' + role, true), (0, _defineProperty3['default'])(_classnames3, 'sn-' + role + '-disabled', this.isDisabled), (0, _defineProperty3['default'])(_classnames3, 'sn-' + role + '-label-disabled', this.labelDisabled), _classnames3)), on: {
                        'click': this.onClick
                    }
                },
                [Children]
            );
        }
    };
};