<script setup lang="ts">
import { computed } from 'vue';

const props = defineProps<{
  modelValue: string;
  label?: string;
  placeholder?: string;
  type?: string;
  error?: string | string[];
  hint?: string;
  disabled?: boolean;
  required?: boolean;
  name?: string;
  autocomplete?: string;
}>();

const emit = defineEmits<{
  'update:modelValue': [value: string];
  blur: [event: FocusEvent];
}>();

const errorMessage = computed(() =>
  Array.isArray(props.error) ? props.error[0] : props.error,
);

function onInput(e: Event) {
  emit('update:modelValue', (e.target as HTMLInputElement).value);
}
</script>

<template>
  <div class="field" :class="{ 'field--error': !!errorMessage, 'field--disabled': disabled }">
    <label v-if="label" class="field__label">
      {{ label }}
      <span v-if="required" class="field__required" aria-hidden="true">*</span>
    </label>
    <div class="field__wrap">
      <input
        class="field__input"
        :type="type ?? 'text'"
        :placeholder="placeholder"
        :value="modelValue"
        :disabled="disabled"
        :required="required"
        :name="name"
        :autocomplete="autocomplete"
        :aria-describedby="errorMessage ? `${name}-error` : hint ? `${name}-hint` : undefined"
        :aria-invalid="!!errorMessage"
        @input="onInput"
        @blur="emit('blur', $event)"
      />
      <slot name="suffix" />
    </div>
    <p v-if="errorMessage" :id="`${name}-error`" class="field__error" role="alert">
      {{ errorMessage }}
    </p>
    <p v-else-if="hint" :id="`${name}-hint`" class="field__hint">{{ hint }}</p>
  </div>
</template>

<style scoped lang="scss">
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;

  &__label {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--color-muted);
  }

  &__required {
    color: var(--color-signal);
    margin-left: 3px;
  }

  &__wrap {
    position: relative;
    display: flex;
    align-items: center;
  }

  &__input {
    width: 100%;
    height: 44px;
    padding: 0 14px;
    background: var(--color-surface-2);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-cream);
    font-family: var(--font-mono);
    font-size: 0.875rem;
    transition:
      border-color var(--transition-fast),
      background var(--transition-fast);
    outline: none;

    &::placeholder {
      color: var(--color-muted);
    }

    &:hover:not(:disabled) {
      border-color: var(--color-border-strong);
    }

    &:focus {
      border-color: var(--color-signal);
      background: var(--color-surface-3);
    }

    &:disabled {
      opacity: 0.5;
      cursor: not-allowed;
    }
  }

  &--error .field__input {
    border-color: var(--color-signal);
  }

  &__error {
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    color: var(--color-signal);
    margin: 0;
  }

  &__hint {
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    color: var(--color-muted);
    margin: 0;
  }
}
</style>
