<template>
  <div
    class="textarea-input text-start"
    :class="{
      'input-group': !floatingLabel,
      flush: flush,
      'form-floating': floatingLabel,
      'form-filled': !!modelValue,
    }"
  >
    <label class="input-group-text" :for="id" v-if="!!label && !floatingLabel">{{ label }}</label>
    <textarea
      :id="id"
      :type="type"
      class="form-control"
      v-bind="$attrs"
      :placeholder="placeholderText"
      :value="modelValue"
      :maxlength="maxlength"
      :max="max"
      :min="min"
      :pattern="pattern"
      ref="textInput"
      @focus="onFocus($event)"
      @blur="onBlur($event)"
      @input="onInput($event)"
      @keydown="onKeyDown($event)"
    />
    <label :for="id" v-if="!!label && floatingLabel">{{ label }}</label>
  </div>
</template>

<script lang="ts">
import { useIds } from '@/composables';
const { getId } = useIds();
</script>

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

defineOptions({ inheritAttrs: false });
const props = defineProps({
  type: {
    type: String,
    default: 'text',
  },
  id: {
    type: String,
    default: () => getId('input'),
  },
  flush: {
    type: Boolean,
    default: false,
  },
  max: Number,
  min: Number,
  maxlength: Number,
  pattern: String,
  regex: RegExp,
  label: String,
  modelValue: String,
  placeholder: String,
  floatingLabel: Boolean,
});
const emit = defineEmits(['update:modelValue', 'onAction', 'keydown.enter', 'focus', 'blur']);

const placeholderText = computed(() => props.placeholder || '');
const onInput = (e: any) => {
  emit('update:modelValue', e.target.value);
};
const textInput = ref<HTMLInputElement | null>(null);

const getTextValue = () => textInput.value?.value || '';

const focus = () => textInput.value?.focus();
const blur = () => textInput.value?.blur();
const onFocus = (e: any) => emit('focus', e);
const onBlur = (e: any) => emit('blur', e);
const onKeyDown = (e: any) => {
  if (e.which === 13) {
    onEnter();
  } else if (e.which === 8 || e.which === 46) {
    return true;
  } else {
    const val = e.target.value + e.key;
    if (!!props.regex && !props.regex.test(val)) {
      e.preventDefault();
      return false;
    }
  }
  return true;
};
const onEnter = () => {
  emit('keydown.enter', getTextValue());
};

defineExpose({ focus, blur });
</script>

<style lang="scss">
@import '../../styles/mixins';

.textarea-input {
  &.input-group.text-start,
  &.form-floating.text-start {
    &.flush {
      label.input-group-text {
        border-radius: 0;
        border-left-width: 0;
      }
      .form-control {
        border-radius: 0;
        border-right-width: 0;
      }
    }
  }
  &.form-floating.text-start {
    .form-control {
      padding: 1rem 0.75rem 0.5rem;
      min-height: 68px;
      &:focus ~ label {
        opacity: 0.6;
        top: -15px;
      }
    }
    &.form-filled {
      .form-control ~ label {
        opacity: 0.6;
        top: -15px;
      }
    }
  }
  label.input-group-text {
    @include noTextSelect();
  }
}
</style>
