'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var FormItemContext = require('../../form/src/FormItemContext.cjs');
var index = require('../style/index.cjs');
var context = require('./context.cjs');
var Radio = require('./Radio.cjs');
var props = require('./props.cjs');
var _class = require('../../../utils/dom/class.cjs');

var AGroup = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ARadioGroup",
  inheritAttrs: false,
  props: 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.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 = _class.classNames(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
            }, {
              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
          }, {
            default: () => [label]
          });
        });
      } else {
        children = (_a = slots.default) == null ? void 0 : _a.call(slots);
      }
      return wrapSSR(vue.createVNode("div", {
        ...attrs,
        "class": classString,
        "id": id
      }, [children]));
    };
  }
});

exports.default = AGroup;
//# sourceMappingURL=Group.cjs.map
