<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 Textareas collect data from the user and allow multiple lines of text.
 * @documentation https://synergy-design-system.github.io/?path=/docs/components-syn-textarea--docs
 * @status stable
 * @since 1.0.0
 *
 * @slot label - The textarea's label. Alternatively, you can use the `label` attribute.
 * @slot help-text - Text that describes how to use the input. Alternatively, you can use the `help-text` attribute.
 *
 * @event syn-blur - Emitted when the control loses focus.
 * @event syn-change - Emitted when an alteration to the control's value is committed by the user.
 * @event syn-focus - Emitted when the control gains focus.
 * @event syn-input - Emitted when the control receives 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 base - The component's base wrapper.
 * @csspart textarea - The internal `<textarea>` control.
 */
import { computed, ref } from 'vue';
import '@synergy-design-system/components/components/textarea/textarea.js';

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

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

defineExpose({
  nativeElement,
});

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

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

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

  /**
   * The textarea's size.
   */
  size?: SynTextarea['size'];

  /**
   * The textarea's label.
   * If you need to display HTML, use the `label` slot instead.
   */
  label?: SynTextarea['label'];

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

  /**
   * Placeholder text to show as a hint when the input is empty.
   */
  placeholder?: SynTextarea['placeholder'];

  /**
   * The number of rows to display by default.
   */
  rows?: SynTextarea['rows'];

  /**
   * Controls how the textarea can be resized.
   */
  resize?: SynTextarea['resize'];

  /**
   * Disables the textarea.
   */
  disabled?: SynTextarea['disabled'];

  /**
   * Makes the textarea readonly.
   */
  readonly?: SynTextarea['readonly'];

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

  /**
   * Makes the textarea a required field.
   */
  required?: SynTextarea['required'];

  /**
   * The minimum length of input that will be considered valid.
   */
  minlength?: SynTextarea['minlength'];

  /**
   * The maximum length of input that will be considered valid.
   */
  maxlength?: SynTextarea['maxlength'];

  /**
   * Controls whether and how text input is automatically capitalized as it is entered by the user.
   */
  autocapitalize?: SynTextarea['autocapitalize'];

  /**
   * Indicates whether the browser's autocorrect feature is on or off.
   */
  autocorrect?: SynTextarea['autocorrect'];

  /**
* Specifies what permission the browser has to provide assistance in filling out form field values.
* Refer to
[this page on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete) for available values.
 */
  autocomplete?: SynTextarea['autocomplete'];

  /**
   * Indicates that the input should receive focus on page load.
   */
  autofocus?: SynTextarea['autofocus'];

  /**
   * Used to customize the label or icon of the Enter key on virtual keyboards.
   */
  enterkeyhint?: SynTextarea['enterkeyhint'];

  /**
   * Enables spell checking on the textarea.
   */
  spellcheck?: SynTextarea['spellcheck'];

  /**
* Tells the browser what type of data will be entered by the user, allowing it to display the appropriate virtual
keyboard on supportive devices.
 */
  inputmode?: SynTextarea['inputmode'];

  /**
   * Support for two way data binding
   */
  modelValue?: SynTextarea['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 control loses focus.
   */
  'syn-blur': [e: SynBlurEvent];

  /**
   * Emitted when an alteration to the control's value is committed by the user.
   */
  'syn-change': [e: SynChangeEvent];

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

  /**
   * Emitted when the control receives 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: SynTextarea['value']];
}>();
</script>

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

<template>
  <syn-textarea
    @syn-blur="$emit('syn-blur', $event)"
    @syn-change="$emit('syn-change', $event)"
    @syn-focus="$emit('syn-focus', $event)"
    @syn-input="
      $emit('update:modelValue', ($event.target as SynTextarea).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-textarea>
</template>
