<script setup lang="ts">
// ---------------------------------------------------------------------
// 🔒 AUTOGENERATED @synergy-design-system/vue wrappers for @synergy-design-system/components
// Please do not edit this file directly!
// It will get recreated when running pnpm build.
// ---------------------------------------------------------------------

/**
 * @summary Radio groups are used to group multiple [radios](/components/radio) or [radio buttons](/components/radio-button) so they function as a single form control.
 * @documentation https://synergy-design-system.github.io/?path=/docs/components-syn-radio-group--docs
 * @status stable
 * @since 1.0.0
 *
 * @dependency syn-button-group
 *
 * @slot - The default slot where `<syn-radio>` or `<syn-radio-button>` elements are placed.
 * @slot label - The radio group's label. Required for proper accessibility. Alternatively, you can use the `label`
 *  attribute.
 * @slot help-text - Text that describes how to use the radio group. Alternatively, you can use the `help-text` attribute.
 *
 * @event syn-change - Emitted when the radio group's selected value changes.
 * @event syn-input - Emitted when the radio group receives user input.
 * @event syn-invalid - Emitted when the form control has been checked for validity and its constraints aren't satisfied.
 *
 * @csspart form-control - The form control that wraps the label, input, and help text.
 * @csspart form-control-label - The label's wrapper.
 * @csspart form-control-input - The input's wrapper.
 * @csspart form-control-help-text - The help text's wrapper.
 * @csspart button-group - The button group that wraps radio buttons.
 * @csspart button-group__base - The button group's `base` part.
 */
import { computed, ref } from 'vue';
import '@synergy-design-system/components/components/radio-group/radio-group.js';

import type { SynChangeEvent } from '@synergy-design-system/components';
import type { SynInputEvent } from '@synergy-design-system/components';
import type { SynInvalidEvent } from '@synergy-design-system/components';
import type SynRadioGroup from '@synergy-design-system/components/components/radio-group/radio-group.component.js';

// DOM Reference to the element
const nativeElement = ref<SynRadioGroup>();

defineExpose({
  nativeElement,
});

// Map attributes
const props = defineProps<{
  /**
* The radio group's label.
* Required for proper accessibility.
* If you need to display HTML, use the `label` slot
instead.
 */
  label?: SynRadioGroup['label'];

  /**
   * The radio groups's help text.
   * If you need to display HTML, use the `help-text` slot instead.
   */
  helpText?: SynRadioGroup['helpText'];

  /**
   * The name of the radio group, submitted as a name/value pair with form data.
   */
  name?: SynRadioGroup['name'];

  /**
   * The current value of the radio group, submitted as a name/value pair with form data.
   */
  value?: SynRadioGroup['value'];

  /**
   * The radio group's size.
   * This size will be applied to all child radios and radio buttons.
   */
  size?: SynRadioGroup['size'];

  /**
* By default, form controls are associated with the nearest containing `<form>` element.
* This attribute allows you
to place the form control outside of a form and associate it with the form that has this `id`.
* The form must be in
the same document or shadow root for this to work.
 */
  form?: SynRadioGroup['form'];

  /**
   * Ensures a child radio is checked before allowing the containing form to submit.
   */
  required?: SynRadioGroup['required'];

  /**
   * Support for two way data binding
   */
  modelValue?: SynRadioGroup['value'];
}>();

// Make sure prop binding only forwards the props that are actually there.
// This is needed because :param="param" also adds an empty attribute
// when using web-components, which breaks optional arguments like size in SynInput
// @see https://github.com/vuejs/core/issues/5190#issuecomment-1003112498
const visibleProps = computed(() =>
  Object.fromEntries(
    Object.entries(props).filter(([, value]) => typeof value !== 'undefined'),
  ),
);

// Map events
defineEmits<{
  /**
   * Emitted when the radio group's selected value changes.
   */
  'syn-change': [e: SynChangeEvent];

  /**
   * Emitted when the radio group receives user input.
   */
  'syn-input': [e: SynInputEvent];

  /**
   * Emitted when the form control has been checked for validity and its constraints aren't satisfied.
   */
  'syn-invalid': [e: SynInvalidEvent];

  /**
   * Support for two way data binding
   */
  'update:modelValue': [newValue: SynRadioGroup['value']];
}>();
</script>

<script lang="ts">
export type { SynChangeEvent } from '@synergy-design-system/components';
export type { SynInputEvent } from '@synergy-design-system/components';
export type { SynInvalidEvent } from '@synergy-design-system/components';
</script>

<template>
  <syn-radio-group
    @syn-change="$emit('syn-change', $event)"
    @syn-input="
      $emit('update:modelValue', ($event.target as SynRadioGroup).value);
      $emit('syn-input', $event);
    "
    @syn-invalid="$emit('syn-invalid', $event)"
    :value="
      typeof props.modelValue !== 'undefined'
        ? props.modelValue
        : typeof props.value !== 'undefined'
          ? props.value
          : undefined
    "
    v-bind="visibleProps"
    ref="nativeElement">
    <slot></slot>
  </syn-radio-group>
</template>
