"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
const XEUtils = require("xe-utils");
const utils = require("../../ui/src/utils.js");
require("../../ui/index.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");
const VxeRadioButtonComponent = comp.defineVxeComponent({
  name: "VxeRadioButton",
  props: {
    modelValue: [String, Number, Boolean],
    checkedValue: {
      type: [String, Number, Boolean],
      default: void 0
    },
    title: [String, Number],
    content: [String, Number],
    disabled: {
      type: Boolean,
      default: null
    },
    strict: {
      type: Boolean,
      default: () => config.getConfig().radioButton.strict
    },
    size: {
      type: String,
      default: () => config.getConfig().radioButton.size || config.getConfig().size
    },
    /**
     * 已废弃，被 checkedValue 替换
     */
    label: {
      type: [String, Number, Boolean],
      default: null
    }
  },
  emits: [
    "update:modelValue",
    "change"
  ],
  setup(props, context) {
    const { slots, emit } = context;
    const $xeForm = vue.inject("$xeForm", null);
    const formItemInfo = vue.inject("xeFormItemInfo", null);
    const $xeRadioGroup = vue.inject("$xeRadioGroup", null);
    const xID = XEUtils.uniqueId();
    const reactData = vue.reactive({});
    const { computeSize } = useFns.useSize(props);
    const $xeRadioButton = {
      xID,
      props,
      context,
      reactData
    };
    const computeIsDisabled = vue.computed(() => {
      const { disabled } = props;
      if (disabled === null) {
        if ($xeRadioGroup) {
          const { computeIsDisabled: computeIsDisabled2 } = $xeRadioGroup.getComputeMaps();
          return computeIsDisabled2.value;
        }
      }
      return disabled;
    });
    const computeName = vue.computed(() => {
      return $xeRadioGroup ? $xeRadioGroup.name : null;
    });
    const computeStrict = vue.computed(() => {
      return $xeRadioGroup ? $xeRadioGroup.props.strict : props.strict;
    });
    const computeChecked = vue.computed(() => {
      const { label, checkedValue } = props;
      const radioValue = XEUtils.isUndefined(checkedValue) ? label : checkedValue;
      return $xeRadioGroup ? $xeRadioGroup.props.modelValue === radioValue : props.modelValue === radioValue;
    });
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $radioGroup: $xeRadioGroup }, params));
    };
    const radioButtonMethods = {
      dispatchEvent
    };
    const radioButtonPrivateMethods = {};
    Object.assign($xeRadioButton, radioButtonMethods, radioButtonPrivateMethods);
    const handleValue = (checkedValue, evnt) => {
      const { content } = props;
      if ($xeRadioGroup) {
        $xeRadioGroup.handleChecked({ label: checkedValue, checkedValue, checkedLabel: content }, evnt);
      } else {
        emit("update:modelValue", checkedValue);
        dispatchEvent("change", { value: checkedValue, label: checkedValue, checkedValue, checkedLabel: content }, evnt);
        if ($xeForm && formItemInfo) {
          $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, checkedValue);
        }
      }
    };
    const changeEvent = (evnt) => {
      const isDisabled = computeIsDisabled.value;
      if (!isDisabled) {
        const { label, checkedValue } = props;
        const radioValue = XEUtils.isUndefined(checkedValue) ? label : checkedValue;
        handleValue(radioValue, evnt);
      }
    };
    const clickEvent = (evnt) => {
      const isDisabled = computeIsDisabled.value;
      const isStrict = computeStrict.value;
      if (!isDisabled && !isStrict) {
        const { label, checkedValue } = props;
        const radioValue = XEUtils.isUndefined(checkedValue) ? label : checkedValue;
        if (radioValue === ($xeRadioGroup ? $xeRadioGroup.props.modelValue : props.modelValue)) {
          handleValue(null, evnt);
        }
      }
    };
    const renderVN = () => {
      const { label, checkedValue } = props;
      const radioValue = XEUtils.isUndefined(checkedValue) ? label : checkedValue;
      const vSize = computeSize.value;
      const isDisabled = computeIsDisabled.value;
      const name = computeName.value;
      const isChecked = computeChecked.value;
      return vue.h("label", {
        key: radioValue,
        class: ["vxe-radio vxe-radio--button", {
          [`size--${vSize}`]: vSize,
          "is--disabled": isDisabled
        }],
        title: props.title
      }, [
        vue.h("input", {
          class: "vxe-radio--input",
          type: "radio",
          name,
          checked: isChecked,
          disabled: isDisabled,
          onChange: changeEvent,
          onClick: clickEvent
        }),
        vue.h("span", {
          class: "vxe-radio--label"
        }, slots.default ? slots.default({}) : utils.getFuncText(props.content))
      ]);
    };
    $xeRadioButton.renderVN = renderVN;
    return renderVN;
  }
});
exports.default = VxeRadioButtonComponent;
