"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const index = require("../icon/index.js");
const props = require("../utils/props.js");
const basic = require("../utils/basic.js");
const format = require("../utils/format.js");
const checkerProps = {
  name: props.unknownProp,
  disabled: Boolean,
  iconSize: props.numericProp,
  modelValue: props.unknownProp,
  checkedColor: String,
  labelPosition: String,
  labelDisabled: Boolean
};
var stdin_default = Vue.defineComponent({
  props: basic.extend({}, checkerProps, {
    bem: props.makeRequiredProp(Function),
    role: String,
    shape: String,
    parent: Object,
    checked: Boolean,
    bindGroup: props.truthProp,
    indeterminate: {
      type: Boolean,
      default: null
    }
  }),
  emits: ["click", "toggle"],
  setup(props2, {
    emit,
    slots
  }) {
    const iconRef = Vue.ref();
    const getParentProp = (name) => {
      if (props2.parent && props2.bindGroup) {
        return props2.parent.props[name];
      }
    };
    const disabled = Vue.computed(() => {
      if (props2.parent && props2.bindGroup) {
        const disabled2 = getParentProp("disabled") || props2.disabled;
        if (props2.role === "checkbox") {
          const checkedCount = getParentProp("modelValue").length;
          const max = getParentProp("max");
          const overlimit = max && checkedCount >= +max;
          return disabled2 || overlimit && !props2.checked;
        }
        return disabled2;
      }
      return props2.disabled;
    });
    const direction = Vue.computed(() => getParentProp("direction"));
    const iconStyle = Vue.computed(() => {
      const checkedColor = props2.checkedColor || getParentProp("checkedColor");
      if (checkedColor && (props2.checked || props2.indeterminate) && !disabled.value) {
        return {
          borderColor: checkedColor,
          backgroundColor: checkedColor
        };
      }
    });
    const shape = Vue.computed(() => {
      return props2.shape || getParentProp("shape") || "round";
    });
    const onClick = (event) => {
      const {
        target
      } = event;
      const icon = iconRef.value;
      const iconClicked = icon === target || (icon == null ? void 0 : icon.contains(target));
      if (!disabled.value && (iconClicked || !props2.labelDisabled)) {
        emit("toggle");
      }
      emit("click", event);
    };
    const renderIcon = () => {
      var _a, _b;
      const {
        bem,
        checked,
        indeterminate
      } = props2;
      const iconSize = props2.iconSize || getParentProp("iconSize");
      return Vue.createVNode("div", {
        "ref": iconRef,
        "class": bem("icon", [shape.value, {
          disabled: disabled.value,
          checked,
          indeterminate
        }]),
        "style": shape.value !== "dot" ? {
          fontSize: format.addUnit(iconSize)
        } : {
          width: format.addUnit(iconSize),
          height: format.addUnit(iconSize),
          borderColor: (_a = iconStyle.value) == null ? void 0 : _a.borderColor
        }
      }, [slots.icon ? slots.icon({
        checked,
        disabled: disabled.value
      }) : shape.value !== "dot" ? Vue.createVNode(index.Icon, {
        "name": indeterminate ? "minus" : "success",
        "style": iconStyle.value
      }, null) : Vue.createVNode("div", {
        "class": bem("icon--dot__icon"),
        "style": {
          backgroundColor: (_b = iconStyle.value) == null ? void 0 : _b.backgroundColor
        }
      }, null)]);
    };
    const renderLabel = () => {
      const {
        checked
      } = props2;
      if (slots.default) {
        return Vue.createVNode("span", {
          "class": props2.bem("label", [props2.labelPosition, {
            disabled: disabled.value
          }])
        }, [slots.default({
          checked,
          disabled: disabled.value
        })]);
      }
    };
    return () => {
      const nodes = props2.labelPosition === "left" ? [renderLabel(), renderIcon()] : [renderIcon(), renderLabel()];
      return Vue.createVNode("div", {
        "role": props2.role,
        "class": props2.bem([{
          disabled: disabled.value,
          "label-disabled": props2.labelDisabled
        }, direction.value]),
        "tabindex": disabled.value ? void 0 : 0,
        "aria-checked": props2.checked,
        "onClick": onClick
      }, [nodes]);
    };
  }
});
exports.checkerProps = checkerProps;
exports.default = stdin_default;
