"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
require("../../ui/index.js");
const XEUtils = require("xe-utils");
const checkbox = require("./checkbox.js");
const config = require("../../../../@vxe-ui/core/es/src/config.js");
const useFns = require("../../../../@vxe-ui/core/es/src/useFns.js");
const event = require("../../../../@vxe-ui/core/es/src/event.js");
function createInternalData() {
  return {
    // isLoaded: false
  };
}
const VxeCheckboxGroupComponent = comp.defineVxeComponent({
  name: "VxeCheckboxGroup",
  props: {
    modelValue: Array,
    options: Array,
    optionProps: Object,
    disabled: {
      type: Boolean,
      default: null
    },
    max: {
      type: [String, Number],
      default: null
    },
    size: {
      type: String,
      default: () => config.getConfig().checkboxGroup.size || config.getConfig().size
    },
    defaultConfig: Object
  },
  emits: [
    "update:modelValue",
    "change",
    "default-change"
  ],
  setup(props, context) {
    const { slots, emit } = context;
    const $xeForm = vue.inject("$xeForm", null);
    const formItemInfo = vue.inject("xeFormItemInfo", null);
    const xID = XEUtils.uniqueId();
    const reactData = vue.reactive({});
    const internalData = createInternalData();
    const computeIsDisabled = vue.computed(() => {
      const { disabled } = props;
      if (disabled === null) {
        if ($xeForm) {
          return $xeForm.props.readonly || $xeForm.props.disabled;
        }
        return false;
      }
      return disabled;
    });
    const computeIsMaximize = vue.computed(() => {
      const { modelValue, max } = props;
      if (max) {
        return (modelValue ? modelValue.length : 0) >= XEUtils.toNumber(max);
      }
      return false;
    });
    const computeDefaultOpts = vue.computed(() => {
      return Object.assign({}, props.defaultConfig);
    });
    const computePropsOpts = vue.computed(() => {
      return Object.assign({}, props.optionProps);
    });
    const computeLabelField = vue.computed(() => {
      const propsOpts = computePropsOpts.value;
      return propsOpts.label || "label";
    });
    const computeValueField = vue.computed(() => {
      const propsOpts = computePropsOpts.value;
      return propsOpts.value || "value";
    });
    const computeDisabledField = vue.computed(() => {
      const propsOpts = computePropsOpts.value;
      return propsOpts.disabled || "disabled";
    });
    const computeMaps = {
      computeIsMaximize,
      computeIsDisabled
    };
    const $xeCheckboxGroup = {
      xID,
      props,
      context,
      reactData,
      getComputeMaps: () => computeMaps
    };
    useFns.useSize(props);
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $checkboxGroup: $xeCheckboxGroup }, params));
    };
    const emitModel = (value) => {
      emit("update:modelValue", value);
    };
    const emitDefaultValue = (value) => {
      emitModel(value);
      dispatchEvent("default-change", { value }, null);
    };
    const checkboxGroupMethods = {
      dispatchEvent
    };
    const checkboxGroupPrivateMethods = {
      handleChecked(params, evnt) {
        const { checked, label } = params;
        const checklist = props.modelValue || [];
        const checkIndex = checklist.indexOf(label);
        if (checked) {
          if (checkIndex === -1) {
            checklist.push(label);
          }
        } else {
          checklist.splice(checkIndex, 1);
        }
        emitModel(checklist);
        $xeCheckboxGroup.dispatchEvent("change", Object.assign({}, params, { checklist, value: checklist }), evnt);
        if ($xeForm && formItemInfo) {
          $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, checklist);
        }
      }
    };
    const loadData = (datas) => {
      const { isLoaded } = internalData;
      const defaultOpts = computeDefaultOpts.value;
      const valueField = computeValueField.value;
      if (!isLoaded) {
        const { selectMode } = defaultOpts;
        if (datas.length > 0 && XEUtils.eqNull(props.modelValue)) {
          if (selectMode === "all") {
            vue.nextTick(() => {
              emitDefaultValue(datas.map((item) => item[valueField]));
            });
          } else if (selectMode === "first" || selectMode === "last") {
            const selectItem = XEUtils[selectMode](datas);
            if (selectItem) {
              vue.nextTick(() => {
                if (XEUtils.eqNull(props.modelValue)) {
                  emitDefaultValue([selectItem[valueField]]);
                }
              });
            }
          }
          internalData.isLoaded = true;
        }
      }
      return vue.nextTick();
    };
    Object.assign($xeCheckboxGroup, checkboxGroupMethods, checkboxGroupPrivateMethods);
    const renderVN = () => {
      const { options } = props;
      const defaultSlot = slots.default;
      const valueField = computeValueField.value;
      const labelField = computeLabelField.value;
      const disabledField = computeDisabledField.value;
      return vue.h("div", {
        class: "vxe-checkbox-group"
      }, defaultSlot ? defaultSlot({}) : options ? options.map((item) => {
        return vue.h(checkbox.default, {
          key: item[valueField],
          label: item[valueField],
          content: item[labelField],
          disabled: item[disabledField]
        });
      }) : []);
    };
    vue.watch(() => props.options, (val) => {
      loadData(val || []);
    });
    vue.onMounted(() => {
      vue.nextTick(() => {
        const { options } = props;
        if (options) {
          loadData(options);
        }
      });
    });
    vue.onUnmounted(() => {
      XEUtils.assign(internalData, createInternalData());
    });
    vue.provide("$xeCheckboxGroup", $xeCheckboxGroup);
    $xeCheckboxGroup.renderVN = renderVN;
    return renderVN;
  }
});
exports.default = VxeCheckboxGroupComponent;
