/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var helper = require('../_chunks/dep-44e6bdf6.js');
var _typeof = require('@babel/runtime/helpers/typeof');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var intersection = require('lodash/intersection');
var config = require('../config.js');
var checkbox_checkbox = require('./checkbox.js');
var checkbox_checkboxGroupProps = require('./checkbox-group-props.js');
var utils_event = require('../utils/event.js');
require('../utils/render-tnode.js');
require('../utils/classnames.js');
require('./props.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var intersection__default = /*#__PURE__*/_interopDefaultLegacy(intersection);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var name = "".concat(config.prefix, "-checkbox-group");
var CheckboxGroup = Vue__default["default"].extend({
  name: "TCheckboxGroup",
  components: {
    Checkbox: checkbox_checkbox["default"]
  },
  props: _objectSpread({}, checkbox_checkboxGroupProps["default"]),
  data: function data() {
    return {
      checkedMap: {},
      optionList: []
    };
  },
  provide: function provide() {
    return {
      checkboxGroup: this
    };
  },
  watch: {
    values: {
      immediate: true,
      handler: function handler() {
        if (this.value instanceof Array) {
          var map = {};
          this.value.forEach(function (item) {
            map[item] = true;
          });
          this.checkedMap = map;
        }
      }
    },
    options: {
      immediate: true,
      handler: function handler() {
        var _this = this;

        if (!this.options) return [];
        this.optionList = this.options.map(function (item) {
          var r = {};

          if (_typeof__default["default"](item) !== "object") {
            r = {
              label: String(item),
              value: item
            };
          } else {
            r = _objectSpread({}, item);
            r.disabled = r.disabled === void 0 ? _this.disabled : r.disabled;
          }

          return r;
        });
      }
    }
  },
  computed: {
    values: function values() {
      if (this.value instanceof Array) {
        return this.value.join();
      }

      return "";
    },
    intersectionLen: function intersectionLen() {
      var values = this.optionList.map(function (item) {
        return item.value;
      });

      if (this.value instanceof Array) {
        var n = intersection__default["default"](this.value, values);
        return n.length;
      }

      return 0;
    },
    isCheckAll: function isCheckAll() {
      if (this.value instanceof Array && this.value.length !== this.optionList.length - 1) {
        return false;
      }

      return this.intersectionLen === this.optionList.length - 1;
    },
    indeterminate: function indeterminate() {
      return !this.isCheckAll && this.intersectionLen < this.optionList.length && this.intersectionLen !== 0;
    },
    maxExceeded: function maxExceeded() {
      return this.max !== void 0 && this.value.length === this.max;
    }
  },
  mounted: function mounted() {
    var _this$options;

    if (this.$scopedSlots["default"] && (_this$options = this.options) !== null && _this$options !== void 0 && _this$options.length) {
      console.warn("TDesign CheckboxGroup Warn: both `options` and slot exist, rendered by `options`");
    }
  },
  created: function created() {
    this.$on("checked-change", this.onCheckedChange);
  },
  render: function render() {
    var _this$options2,
        _this2 = this;

    var h = arguments[0];
    var children = null;

    if ((_this$options2 = this.options) !== null && _this$options2 !== void 0 && _this$options2.length) {
      var _this$optionList;

      children = (_this$optionList = this.optionList) === null || _this$optionList === void 0 ? void 0 : _this$optionList.map(function (option) {
        return h(checkbox_checkbox["default"], helper.helper([{
          "key": option.value
        }, {
          "props": _objectSpread({}, option)
        }, {
          "attrs": {
            "checked": _this2.checkedMap[option.value]
          }
        }]), [_this2.renderLabel(option)]);
      });
    } else {
      var nodes = this.$scopedSlots["default"] && this.$scopedSlots["default"](null);
      this.optionList = this.getOptionListBySlots(nodes);
      children = nodes;
    }

    return h("div", {
      "class": name
    }, [children]);
  },
  methods: {
    onCheckedChange: function onCheckedChange(p) {
      var checked = p.checked,
          checkAll = p.checkAll,
          e = p.e;

      if (checkAll) {
        this.onCheckAllChange(checked, {
          e: e
        });
      } else {
        this.handleCheckboxChange(p);
      }
    },
    getOptionListBySlots: function getOptionListBySlots(nodes) {
      var arr = [];
      nodes === null || nodes === void 0 ? void 0 : nodes.forEach(function (node) {
        var _node$componentOption;

        var option = node === null || node === void 0 ? void 0 : (_node$componentOption = node.componentOptions) === null || _node$componentOption === void 0 ? void 0 : _node$componentOption.propsData;
        option && arr.push(option);
      });
      return arr;
    },
    renderLabel: function renderLabel(option) {
      if (typeof option.label === "function") {
        return option.label(this.$createElement);
      }

      return option.label;
    },
    emitChange: function emitChange(val, e) {
      utils_event.emitEvent(this, "change", val, {
        e: e
      });
    },
    handleCheckboxChange: function handleCheckboxChange(data) {
      var currentValue = data.option.value;

      if (this.value instanceof Array) {
        var val = _toConsumableArray__default["default"](this.value);

        if (data.checked) {
          val.push(currentValue);
        } else {
          var i = val.indexOf(currentValue);
          val.splice(i, 1);
        }

        this.emitChange(val, data.e);
      } else {
        console.warn("TDesign CheckboxGroup Warn: `value` must be an array, instead of ".concat(_typeof__default["default"](this.value)));
      }
    },
    getAllCheckboxValue: function getAllCheckboxValue() {
      var val = new Set();

      for (var i = 0, len = this.optionList.length; i < len; i++) {
        var item = this.optionList[i];
        if (item.checkAll) continue;
        val.add(item.value);
        if (this.maxExceeded) break;
      }

      return _toConsumableArray__default["default"](val);
    },
    onCheckAllChange: function onCheckAllChange(checked, context) {
      var value = checked ? this.getAllCheckboxValue() : [];
      this.emitChange(value, context.e);
    }
  }
});

exports["default"] = CheckboxGroup;
//# sourceMappingURL=group.js.map
