/*! bxs-vue-ui v3.3.0 */
import '../utils/entry-virtual.js';
import { C as create, _ as __vue_normalize__ } from '../utils/normalize-component.js';
import '../utils/sloppy-array-method.js';
import { _ as _indexOfInstanceProperty } from '../utils/index-of.js';
import { _ as _sliceInstanceProperty } from '../utils/slice.js';
import Icon from '../icon/index.js';
import { _ as _spliceInstanceProperty } from '../utils/splice.js';

var _components;

var onlyValues = function onlyValues(values) {
  return function (str) {
    return _indexOfInstanceProperty(values).call(values, str) !== -1;
  };
};

var script = create({
  name: 'checkbox',
  components: (_components = {}, _components[Icon.name] = Icon, _components),
  props: {
    name: null,
    shape: {
      type: String,
      default: 'round',
      validator: onlyValues(['round', 'square'])
    },
    value: {
      type: Boolean,
      default: false
    },
    checkedColor: String
  },
  data: function data() {
    return {
      parent: null
    };
  },
  computed: {
    checked: {
      get: function get() {
        var _context;

        return this.parent ? _indexOfInstanceProperty(_context = this.parent.value).call(_context, this.name) !== -1 : this.value;
      },
      set: function set(val) {
        if (this.parent) {
          this.setParentValue(val);
        } else {
          this.$emit('input', val);
        }
      }
    },
    iconStyle: function iconStyle() {
      var checkedColor = this.checkedColor,
          checked = this.checked;

      if (checkedColor && checked) {
        return {
          borderColor: checkedColor,
          backgroundColor: checkedColor
        };
      }

      return {};
    }
  },
  watch: {
    value: function value(val) {
      this.$emit('change', val);
    }
  },
  created: function created() {
    this.findParent('bxs-checkbox-group');
  },
  methods: {
    toggle: function toggle() {
      this.checked = !this.checked;
    },
    setParentValue: function setParentValue(val) {
      var _context2;

      var parent = this.parent;

      var value = _sliceInstanceProperty(_context2 = parent.value).call(_context2);

      if (val) {
        if (parent.max && value.length >= parent.max) {
          return;
        }

        if (_indexOfInstanceProperty(value).call(value, this.name) === -1) {
          value.push(this.name);
          parent.$emit('input', value);
        }
      } else {
        var index = _indexOfInstanceProperty(value).call(value, this.name);

        if (index !== -1) {
          _spliceInstanceProperty(value).call(value, index, 1);

          parent.$emit('input', value);
        }
      }
    },
    findParent: function findParent(name) {
      var parent = this.$parent;

      while (parent) {
        if (parent.$options.name === name) {
          this.parent = parent;
          break;
        }

        parent = parent.$parent;
      }
    }
  }
});

/* script */
var __vue_script__ = script;
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    staticClass: "bx-checkbox"
  }, [_c('div', {
    class: ['bx-checkbox-icon', "bx-checkbox-" + _vm.shape, {
      'bx-checkbox-checked': _vm.checked
    }],
    on: {
      "click": _vm.toggle
    }
  }, [_vm._t("icon", [_c('bxs-icon', {
    style: _vm.iconStyle,
    attrs: {
      "name": "check",
      "size": "12px"
    }
  })], {
    checked: _vm.checked
  })], 2), _vm.$slots.default ? _c('span', {
    staticClass: "bx-checkbox-label",
    on: {
      "click": _vm.toggle
    }
  }, [_vm._t("default")], 2) : _vm._e()]);
};

var __vue_staticRenderFns__ = [];
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* style inject */

/* style inject SSR */

var index = __vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

export default index;
