/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

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

var helper = require('../../chunks/helper.js');
var intersection = require('lodash/intersection');
var log = require('../../chunks/log.js');
var utils_event = require('../../utils/event.js');
var components_configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../../utils/mixins.js');
var components_checkbox_checkbox = require('./checkbox.js');
var components_checkbox_props = require('./props.js');
require('../../chunks/_commonjsHelpers-fe796db6.js');
require('../../utils/helper.js');
require('lodash/camelCase');
require('vue');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../../chunks/zh_CN.js');
require('../../utils/render-node.js');
require('lodash');

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

var intersection__default = /*#__PURE__*/_interopDefaultLegacy(intersection);

const classPrefixMixins = components_configProvider_configReceiver.getClassPrefixMixins("checkbox");
var _CheckboxGroup = utils_mixins["default"](classPrefixMixins).extend({
  name: "LCheckboxGroup",
  components: {
    LCheckbox: components_checkbox_checkbox["default"]
  },
  props: {
    ...components_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__default["default"](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() {
    if (this.$scopedSlots.default && this.options?.length) log.log.warn("CheckboxGroup", "both `options` and slot exist, rendered by `options`");
  },
  created() {
    this.$on("checked-change", this.onCheckedChange);
  },
  render() {
    const h = arguments[0];
    let children = null;
    if (this.options?.length) {
      children = this.optionList?.map((option, index) => h("l-checkbox", helper.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?.forEach(node => {
        const option = node?.componentOptions?.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) {
      utils_event.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.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
      });
    }
  }
});

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