import { defineComponent, inject, ref, onMounted, computed, watch, provide, createElementBlock, openBlock, normalizeClass, createElementVNode, renderSlot, nextTick } from "vue";
import { radioInjectKey } from "./provide.mjs";
import { radioProps } from "./types.mjs";
import { radioGroupInjectKey } from "../radio-group/provide.mjs";
import { isUndefined } from "../_utils/is.mjs";
import { uniqueId } from "../_utils/helper.mjs";
const _hoisted_1 = ["for"];
const _hoisted_2 = { class: "o-radio-wrap" };
const _hoisted_3 = ["id", "value", "disabled", "checked"];
const _hoisted_4 = { class: "o-radio-label" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "ORadio",
  props: radioProps,
  emits: ["update:modelValue", "change"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const radioGroupInjection = inject(radioGroupInjectKey, null);
    const inputId = ref(props.inputId);
    onMounted(() => {
      if (!inputId.value) {
        inputId.value = uniqueId();
      }
    });
    const _checked = ref(props.defaultChecked);
    const isChecked = computed(() => {
      if (radioGroupInjection) {
        return radioGroupInjection.realValue.value === props.value;
      }
      if (!isUndefined(props.modelValue)) {
        return props.modelValue === props.value;
      }
      return _checked.value;
    });
    watch(
      isChecked,
      (val) => {
        _checked.value = val;
      },
      { immediate: true }
    );
    __expose({
      checked: isChecked
    });
    const isDisabled = computed(() => (radioGroupInjection == null ? void 0 : radioGroupInjection.disabled.value) || props.disabled);
    const onClick = (ev) => {
      ev.stopPropagation();
    };
    const onChange = (ev) => {
      if (isDisabled.value) {
        return;
      }
      _checked.value = true;
      const val = props.value ?? true;
      emits("update:modelValue", val);
      radioGroupInjection == null ? void 0 : radioGroupInjection.updateModelValue(val);
      nextTick(() => {
        emits("change", val, ev);
        radioGroupInjection == null ? void 0 : radioGroupInjection.onChange(val, ev);
      });
    };
    provide(radioInjectKey, {
      checked: isChecked,
      disabled: isDisabled
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("label", {
        class: normalizeClass(["o-radio", {
          "o-radio-checked": isChecked.value,
          "o-radio-disabled": isDisabled.value
        }]),
        for: inputId.value
      }, [
        createElementVNode("div", _hoisted_2, [
          createElementVNode("input", {
            id: inputId.value,
            type: "radio",
            value: props.value,
            disabled: isDisabled.value,
            checked: isChecked.value,
            onClick,
            onChange
          }, null, 40, _hoisted_3),
          renderSlot(_ctx.$slots, "radio", {
            checked: isChecked.value,
            disabled: isDisabled.value
          }, () => [
            _cache[0] || (_cache[0] = createElementVNode(
              "div",
              { class: "o-radio-input-wrap" },
              [
                createElementVNode("span", { class: "o-radio-input" })
              ],
              -1
              /* HOISTED */
            )),
            createElementVNode("span", _hoisted_4, [
              renderSlot(_ctx.$slots, "default")
            ])
          ])
        ])
      ], 10, _hoisted_1);
    };
  }
});
export {
  _sfc_main as default
};
