/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { h as helper } from '../../chunks/helper.js';
import intersection from 'lodash/intersection';
import '../../chunks/index.js';
import { emitEvent } from '../../utils/event.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import _Checkbox from './checkbox.js';
import { CheckboxGroupProps } from './props.js';
import { l as log } from '../../chunks/log.js';
import '../../chunks/_commonjsHelpers.js';
import '../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/render-node.js';

const classPrefixMixins = getClassPrefixMixins("checkbox");
var _CheckboxGroup = mixins(classPrefixMixins).extend({
  name: "LCheckboxGroup",
  components: {
    LCheckbox: _Checkbox
  },
  props: {
    ...CheckboxGroupProps
  },
  data() {
    return {
      checkedMap: {},
      optionList: []
    };
  },
  provide() {
    return {
      checkboxGroup: this
    };
  },
  watch: {
    values: {
      immediate: true,
      handler() {
        if (Array.isArray(this.value)) {
          const map = {};
          this.value.forEach(item => {
            map[item] = true;
          });
          this.checkedMap = map;
        }
      }
    },
    options: {
      immediate: true,
      deep: true,
      handler() {
        if (!this.options) return [];
        this.optionList = this.options.map(item => {
          let r = {};
          if (typeof item !== "object") {
            r = {
              label: String(item),
              value: item
            };
          } else {
            r = {
              ...item
            };
            r.disabled = r.disabled === void 0 ? this.disabled : r.disabled;
          }
          return r;
        });
      }
    }
  },
  computed: {
    values() {
      if (Array.isArray(this.value)) return this.value.join();
      return "";
    },
    intersectionLen() {
      const values = this.optionList.map(item => item.value);
      if (Array.isArray(this.value)) {
        const n = intersection(this.value, values);
        return n.length;
      }
      return 0;
    },
    isCheckAll() {
      if (Array.isArray(this.value) && this.value.length !== this.optionList.length - 1) return false;
      return this.intersectionLen === this.optionList.length - 1;
    },
    indeterminate() {
      return !this.isCheckAll && this.intersectionLen < this.optionList.length && this.intersectionLen !== 0;
    },
    maxExceeded() {
      return this.max !== void 0 && this.value.length === this.max;
    }
  },
  mounted() {
    var _this$options;
    if (this.$scopedSlots.default && (_this$options = this.options) !== null && _this$options !== void 0 && _this$options.length) log.warn("CheckboxGroup", "both `options` and slot exist, rendered by `options`");
  },
  created() {
    this.$on("checked-change", this.onCheckedChange);
  },
  render() {
    var _this$options2;
    const h = arguments[0];
    let 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((option, index) => h("l-checkbox", helper([{
        "key": index
      }, {
        "props": option
      }, {
        "attrs": {
          "checked": this.checkedMap[option.value]
        }
      }]), [this.renderLabel(option)]));
    } else {
      const nodes = this.$scopedSlots.default && this.$scopedSlots.default(null);
      this.optionList = this.getOptionListBySlots(nodes);
      children = nodes;
    }
    return h("div", {
      "class": `${this.componentName}-group`,
      "on": {
        "click": e => this.$emit("click", e)
      }
    }, [children]);
  },
  methods: {
    onCheckedChange(p) {
      const {
        checked,
        checkAll,
        e
      } = p;
      if (checkAll) this.onCheckAllChange(checked, {
        e
      });else this.handleCheckboxChange(p);
    },
    getOptionListBySlots(nodes) {
      const arr = [];
      nodes === null || nodes === void 0 ? void 0 : nodes.forEach(node => {
        var _node$componentOption;
        const 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(option) {
      if (typeof option.label === "function") return option.label(this.$createElement);
      return option.label;
    },
    emitChange(val, context) {
      emitEvent(this, "change", val, context);
    },
    handleCheckboxChange(data) {
      const currentValue = data.option.value;
      if (Array.isArray(this.value)) {
        const val = [...this.value];
        if (data.checked) {
          val.push(currentValue);
        } else {
          const 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 {
        log.warn("CheckboxGroup", `\`value\` must be an array, instead of ${typeof this.value}`);
      }
    },
    getAllCheckboxValue() {
      const val = /* @__PURE__ */new Set();
      for (let i = 0, len = this.optionList.length; i < len; i++) {
        const item = this.optionList[i];
        if (item.checkAll) continue;
        val.add(item.value);
        if (this.maxExceeded) break;
      }
      return [...val];
    },
    onCheckAllChange(checked, context) {
      const 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
