<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 Switches allow the user to toggle an option on or off.
 * @documentation https://synergy-design-system.github.io/?path=/docs/components-syn-switch--docs
 * @status stable
 * @since 1.0.0
 *
 * @slot - The switch's label.
 * @slot help-text - Text that describes how to use the switch. Alternatively, you can use the `help-text` attribute.
 *
 * @event syn-blur - Emitted when the control loses focus.
 * @event syn-change - Emitted when the control's checked state changes.
 * @event syn-input - Emitted when the control receives input.
 * @event syn-focus - Emitted when the control gains focus.
 * @event syn-invalid - Emitted when the form control has been checked for validity and its constraints aren't satisfied.
 *
 * @csspart base - The component's base wrapper.
 * @csspart control - The control that houses the switch's thumb.
 * @csspart thumb - The switch's thumb.
 * @csspart label - The switch's label.
 * @csspart form-control-help-text - The help text's wrapper.
 *
 * @cssproperty --width - The width of the switch.
 * @cssproperty --height - The height of the switch.
 * @cssproperty --thumb-size - The size of the thumb.
 */
import { computed, ref } from 'vue';
import '@synergy-design-system/components/components/switch/switch.js';

import type { SynBlurEvent } from '@synergy-design-system/components';
import type { SynChangeEvent } from '@synergy-design-system/components';
import type { SynInputEvent } from '@synergy-design-system/components';
import type { SynFocusEvent } from '@synergy-design-system/components';
import type { SynInvalidEvent } from '@synergy-design-system/components';
import type SynSwitch from '@synergy-design-system/components/components/switch/switch.component.js';

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

defineExpose({
  nativeElement,
});

// Map attributes
const props = defineProps<{
  title?: SynSwitch['title'];

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

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

  /**
   * The switch's size.
   */
  size?: SynSwitch['size'];

  /**
   * Disables the switch.
   */
  disabled?: SynSwitch['disabled'];

  /**
   * Sets the switch to a readonly state.
   */
  readonly?: SynSwitch['readonly'];

  /**
   * Draws the switch in a checked state.
   */
  checked?: SynSwitch['checked'];

  /**
* 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?: SynSwitch['form'];

  /**
   * Makes the switch a required field.
   */
  required?: SynSwitch['required'];

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

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

// 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 control loses focus.
   */
  'syn-blur': [e: SynBlurEvent];

  /**
   * Emitted when the control's checked state changes.
   */
  'syn-change': [e: SynChangeEvent];

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

  /**
   * Emitted when the control gains focus.
   */
  'syn-focus': [e: SynFocusEvent];

  /**
   * 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: SynSwitch['checked']];
}>();
</script>

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

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