/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { h as helper } from '../../_chunks/dep-b13ef886.js';
import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import intersection from 'lodash/intersection';
import _Checkbox from './checkbox.js';
import { CheckboxGroupProps } from './props.js';
import { emitEvent } from '../../utils/event.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import 'vue';
import '../../utils/render-node.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-a0f2090c.js';

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(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 classPrefixMixins = getClassPrefixMixins("checkbox");
var _CheckboxGroup = mixins(classPrefixMixins).extend({
  name: "LCheckboxGroup",
  components: {
    LCheckbox: _Checkbox
  },
  props: _objectSpread({}, CheckboxGroupProps),
  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(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(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, index) {
        return h("l-checkbox", helper([{
          "key": index
        }, {
          "props": 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": "".concat(this.componentName, "-group"),
      "on": {
        "click": function click(e) {
          return _this2.$emit("click", e);
        }
      }
    }, [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, context) {
      emitEvent(this, "change", val, context);
    },
    handleCheckboxChange: function handleCheckboxChange(data) {
      var currentValue = data.option.value;

      if (this.value instanceof Array) {
        var val = _toConsumableArray(this.value);

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

        this.emitChange(val, {
          e: data.e,
          current: data.option.value,
          option: data.option,
          type: data.checked ? "check" : "uncheck"
        });
      } else {
        console.warn("TDesign CheckboxGroup Warn: `value` must be an array, instead of ".concat(_typeof(this.value)));
      }
    },
    getAllCheckboxValue: function getAllCheckboxValue() {
      var val = /* @__PURE__ */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(val);
    },
    onCheckAllChange: function onCheckAllChange(checked, context) {
      var value = checked ? this.getAllCheckboxValue() : [];
      this.emitChange(value, {
        e: context.e,
        type: checked ? "check" : "uncheck",
        current: void 0,
        option: void 0
      });
    }
  }
});

export { _CheckboxGroup as default };
//# sourceMappingURL=group.js.map
