<script setup>
import { computed, ref } from "vue";
const props = defineProps({
  modelValue: {
    type: [Number, String],
    required: false,
  },
  error: {
    type: String,
    default: "",
  },
  required: {
    type: Boolean,
    default: false,
  },
  id: {
    type: String,
    default: () => Math.random().toString(20).substr(2, 10),
  },
  name: {
    type: String,
    default: "",
  },
  ariaDescribedby: {
    type: String,
    default: "",
  },
  placeholder: {
    type: String,
    default: "",
  },
  autofocus: {
    type: Boolean,
    default: false,
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  label: {
    type: String,
    default: "",
  },
  help: {
    type: String,
    default: "",
  },
  readonly: {
    type: Boolean,
    default: false,
  },
  dense: {
    type: Boolean,
    default: false,
  },
  rows: {
    type: [Number, String],
    default: 4,
  },
  resizable: {
    type: Boolean,
    default: false,
  },
  maxlength: {
    type: [Number, String],
    required: false,
  },
});
const emit = defineEmits(["update:modelValue", "keyup"]);
const componentValue = computed({
  get() {
    return props.modelValue;
  },
  set(value) {
    emit("update:modelValue", value);
  },
});
const focus = ref(false);
</script>

<template>
  <div class="flex flex-col gap-2">
    <label
      :for="id"
      :class="`${
        disabled ? 'text-gray-400' : 'text-gray-900'
      } text-sm font-medium dark:text-white ${label ? 'block' : 'hidden'}`"
      ><slot v-if="!label" name="label" />
      <template v-else
        >{{ label }}
        <span v-if="required" class="text-red-600"> *</span></template
      >
    </label>
    <div
      :class="`flex border rounded-lg transition-colors duration-200 ${
        focus
          ? 'border-primary-500 bg-white dark:border-primary-400 dark:bg-gray-700'
          : 'border-gray-300 bg-gray-100 dark:border-gray-600 dark:bg-gray-800'
      }`"
    >
      <textarea
        :id="id"
        v-model="componentValue"
        :class="`${dense ? 'text-sm' : 'text-base'} block w-full !min-h-0 !ring-0 !border-0 !rounded-lg transition-colors duration-200 text-gray-900 bg-gray-50 placeholder-gray-500 dark:bg-transparent dark:text-white dark:placeholder-gray-400 ${resizable ? '' : 'resize-none'} ${dense ? '!py-1 !px-2' : '!p-2.5'}`"
        :required="required"
        :placeholder="placeholder"
        :autofocus="autofocus"
        :aria-describedby="ariaDescribedby"
        :name="name ? name : id"
        :disabled="disabled"
        :readonly="readonly"
        :rows="rows"
        :maxlength="maxlength"
        @keyup="emit('keyup', $event)"
        @focusin="focus = true"
        @focusout="focus = false"
      ></textarea>
    </div>
    <div v-if="help" class="text-xs text-gray-500 dark:text-gray-400">
      {{ help }}
    </div>
    <div v-show="error" class="text-sm text-red-600 flex flex-col gap-1">
      <template v-if="error instanceof Array">
        <span v-for="e in error" :key="e"> {{ e }}</span>
      </template>
      <template v-else>
        {{ error }}
      </template>
    </div>
  </div>
</template>
