<script setup lang="ts">
import axios from 'axios'
import { useVModel, watchDebounced, useTextareaAutosize, syncRef } from '@vueuse/core'
import { ref, computed, watchEffect, onMounted, nextTick, watch } from 'vue'
import { useI18n } from 'vue-i18n'

import useLogger from '~/composables/useLogger'

import Textarea from '~/components/ui/Textarea.vue'

interface Events {
  (e: 'update:modelValue', value: string): void
}

interface Props {
  modelValue: string
  placeholder?: string
  autofocus?: boolean
  permissive?: boolean
  required?: boolean
  charLimit?: number
}

const emit = defineEmits<Events>()
const props = withDefaults(defineProps<Props>(), {
  placeholder: undefined,
  autofocus: false,
  charLimit: 5000,
  permissive: false,
  required: false
})

const logger = useLogger()

const { t } = useI18n()
const { textarea, input } = useTextareaAutosize()
const value = useVModel(props, 'modelValue', emit)
syncRef(value, input)

const isPreviewing = ref(false)
const preview = ref()
const isLoadingPreview = ref(false)

const labels = computed(() => ({
  placeholder: props.placeholder ?? t('components.common.ContentForm.placeholder.input')
}))

const remainingChars = computed(() => props.charLimit - props.modelValue.length)

</script>

<template>
  <Textarea
    ref="textarea"
    v-model="value"
    :required="required || undefined"
    :placeholder="labels.placeholder"
    :autofocus = "autofocus || undefined"
  />
  <span
    v-if="charLimit"
    :class="['right', 'floated', {'ui danger text': remainingChars < 0}]"
  >
    {{ remainingChars }}
  </span>
  <p>
    {{ t('components.common.ContentForm.help.markdown') }}
  </p>
</template>
