"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const CheckboxGroup = require("../checkbox-group/CheckboxGroup.js");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const useExpose = require("../composables/use-expose.js");
const Checker = require("./Checker.js");
const basic = require("../utils/basic.js");
const create = require("../utils/create.js");
const props = require("../utils/props.js");
const [name, bem] = create.createNamespace("checkbox");
const checkboxProps = basic.extend({}, Checker.checkerProps, {
  shape: String,
  bindGroup: props.truthProp,
  indeterminate: {
    type: Boolean,
    default: null
  }
});
var stdin_default = Vue.defineComponent({
  name,
  props: checkboxProps,
  emits: ["change", "update:modelValue"],
  setup(props2, {
    emit,
    slots
  }) {
    const {
      parent
    } = index_esm.useParent(CheckboxGroup.CHECKBOX_GROUP_KEY);
    const setParentValue = (checked2) => {
      const {
        name: name2
      } = props2;
      const {
        max,
        modelValue
      } = parent.props;
      const value = modelValue.slice();
      if (checked2) {
        const overlimit = max && value.length >= +max;
        if (!overlimit && !value.includes(name2)) {
          value.push(name2);
          if (props2.bindGroup) {
            parent.updateValue(value);
          }
        }
      } else {
        const index = value.indexOf(name2);
        if (index !== -1) {
          value.splice(index, 1);
          if (props2.bindGroup) {
            parent.updateValue(value);
          }
        }
      }
    };
    const checked = Vue.computed(() => {
      if (parent && props2.bindGroup) {
        return parent.props.modelValue.indexOf(props2.name) !== -1;
      }
      return !!props2.modelValue;
    });
    const toggle = (newValue = !checked.value) => {
      if (parent && props2.bindGroup) {
        setParentValue(newValue);
      } else {
        emit("update:modelValue", newValue);
      }
      if (props2.indeterminate !== null) emit("change", newValue);
    };
    Vue.watch(() => props2.modelValue, (value) => {
      if (props2.indeterminate === null) emit("change", value);
    });
    useExpose.useExpose({
      toggle,
      props: props2,
      checked
    });
    index_esm.useCustomFieldValue(() => props2.modelValue);
    return () => Vue.createVNode(Checker.default, Vue.mergeProps({
      "bem": bem,
      "role": "checkbox",
      "parent": parent,
      "checked": checked.value,
      "onToggle": toggle
    }, props2), basic.pick(slots, ["default", "icon"]));
  }
});
exports.checkboxProps = checkboxProps;
exports.default = stdin_default;
