'use strict';

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

var vue = require('vue');
var classNames = require('../_util/classNames.js');
var index = require('../_util/vue-types/index.js');
require('../hooks/index.js');
var type = require('../_util/type.js');
var FormItemContext = require('../form/FormItemContext.js');
var index$1 = require('./style/index.js');
var context = require('./context.js');
var Radio = require('./Radio.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');

function _isSlot(s) {
  return typeof s === "function" || Object.prototype.toString.call(s) === "[object Object]" && !vue.isVNode(s);
}
const RadioGroupSizeTypes = ["large", "default", "small"];
const radioGroupProps = () => ({
  "prefixCls": String,
  "value": index.default.any,
  "size": type.stringType(),
  "options": type.arrayType(),
  "disabled": type.booleanType(),
  "name": String,
  "buttonStyle": type.stringType("outline"),
  "id": String,
  "optionType": type.stringType("default"),
  "onChange": type.functionType(),
  "onUpdate:value": type.functionType()
});
var Group = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ARadioGroup",
  inheritAttrs: false,
  props: radioGroupProps(),
  // emits: ['update:value', 'change'],
  setup(props, {
    slots,
    emit,
    attrs
  }) {
    const formItemContext = FormItemContext.useInjectFormItemContext();
    const {
      prefixCls,
      direction,
      size
    } = useConfigInject.default("radio", props);
    const [wrapSSR, hashId] = index$1.default(prefixCls);
    const stateValue = vue.ref(props.value);
    const updatingValue = vue.ref(false);
    vue.watch(() => props.value, (val) => {
      stateValue.value = val;
      updatingValue.value = false;
    });
    const onRadioChange = (ev) => {
      const lastValue = stateValue.value;
      const {
        value
      } = ev.target;
      if (!("value" in props))
        stateValue.value = value;
      if (!updatingValue.value && value !== lastValue) {
        updatingValue.value = true;
        emit("update:value", value);
        emit("change", ev);
        formItemContext.onFieldChange();
      }
      vue.nextTick(() => {
        updatingValue.value = false;
      });
    };
    context.useProvideRadioGroupContext({
      onChange: onRadioChange,
      value: stateValue,
      disabled: vue.computed(() => props.disabled),
      name: vue.computed(() => props.name),
      optionType: vue.computed(() => props.optionType)
    });
    return () => {
      var _a;
      const {
        options,
        buttonStyle,
        id = formItemContext.id.value
      } = props;
      const groupPrefixCls = `${prefixCls.value}-group`;
      const classString = classNames.default(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, {
        [`${groupPrefixCls}-${size.value}`]: size.value,
        [`${groupPrefixCls}-rtl`]: direction.value === "rtl"
      }, attrs.class, hashId.value);
      let children = null;
      if (options && options.length > 0) {
        children = options.map((option) => {
          if (typeof option === "string" || typeof option === "number") {
            return vue.createVNode(Radio.default, {
              "key": option,
              "prefixCls": prefixCls.value,
              "disabled": props.disabled,
              "value": option,
              "checked": stateValue.value === option
            }, _isSlot(option) ? option : {
              default: () => [option]
            });
          }
          const {
            value,
            disabled,
            label
          } = option;
          return vue.createVNode(Radio.default, {
            "key": `radio-group-value-options-${value}`,
            "prefixCls": prefixCls.value,
            "disabled": disabled || props.disabled,
            "value": value,
            "checked": stateValue.value === value
          }, _isSlot(label) ? label : {
            default: () => [label]
          });
        });
      } else {
        children = (_a = slots.default) == null ? void 0 : _a.call(slots);
      }
      return wrapSSR(vue.createVNode("div", vue.mergeProps(attrs, {
        "class": classString,
        "id": id
      }), [children]));
    };
  }
});

exports.default = Group;
exports.radioGroupProps = radioGroupProps;
//# sourceMappingURL=Group.js.map
