'use strict';

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

var vue = require('vue');
var aria = require('../../../utils/aria.js');
var constants = require('../../../utils/constants.js');
require('../../../tokens/index.js');
require('../../../hooks/index.js');
var radioGroup = require('./radio-group2.js');
var index = require('../../../hooks/use-form-item/index.js');
var radio = require('../../../tokens/radio.js');

var script = vue.defineComponent({
  name: "ElRadioGroup",
  props: radioGroup.radioGroupProps,
  emits: radioGroup.radioGroupEmits,
  setup(props, ctx) {
    const radioGroupRef = vue.ref();
    const { size, disabled, formItem } = index.useFormItem({});
    const changeEvent = (value) => {
      ctx.emit(constants.UPDATE_MODEL_EVENT, value);
      vue.nextTick(() => ctx.emit("change", value));
    };
    const handleKeydown = (e) => {
      if (!radioGroupRef.value)
        return;
      const target = e.target;
      const className = target.nodeName === "INPUT" ? "[type=radio]" : "[role=radio]";
      const radios = radioGroupRef.value.querySelectorAll(className);
      const length = radios.length;
      const index = Array.from(radios).indexOf(target);
      const roleRadios = radioGroupRef.value.querySelectorAll("[role=radio]");
      let nextIndex = null;
      switch (e.code) {
        case aria.EVENT_CODE.left:
        case aria.EVENT_CODE.up:
          e.stopPropagation();
          e.preventDefault();
          nextIndex = index === 0 ? length - 1 : index - 1;
          break;
        case aria.EVENT_CODE.right:
        case aria.EVENT_CODE.down:
          e.stopPropagation();
          e.preventDefault();
          nextIndex = index === length - 1 ? 0 : index + 1;
          break;
        default:
          break;
      }
      if (nextIndex === null)
        return;
      roleRadios[nextIndex].click();
      roleRadios[nextIndex].focus();
    };
    vue.onMounted(() => {
      const radios = radioGroupRef.value.querySelectorAll("[type=radio]");
      const firstLabel = radios[0];
      if (!Array.from(radios).some((radio) => radio.checked) && firstLabel) {
        firstLabel.tabIndex = 0;
      }
    });
    vue.provide(radio.radioGroupKey, vue.reactive({
      ...vue.toRefs(props),
      changeEvent
    }));
    vue.watch(() => props.modelValue, () => formItem == null ? void 0 : formItem.validate("change"));
    return {
      size,
      disabled,
      radioGroupRef,
      handleKeydown
    };
  }
});

exports["default"] = script;
//# sourceMappingURL=radio-group.vue_vue_type_script_lang.js.map
