<script setup>
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';

const emit = defineEmits(['update:modelValue', 'update:options', 'keyup.enter']);

const $props = defineProps({
    modelValue: {
        type: [String, Number],
        default: null,
    },
    isOutlined: {
        type: Boolean,
        default: false,
    },
});

const { t } = useI18n();
const requiredFieldRule = (val) => !!val || t('globals.fieldRequired');
const vnInputRef = ref(null);
const value = computed({
    get() {
        return $props.modelValue;
    },
    set(value) {
        emit('update:modelValue', value);
    },
});

const styleAttrs = computed(() => {
    return $props.isOutlined
        ? {
              dense: true,
              outlined: true,
              rounded: true,
          }
        : {};
});

const onEnterPress = () => {
    emit('keyup.enter');
};

const focus = () => {
    vnInputRef.value.focus();
};

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

<template>
    <QInput
        ref="vnInputRef"
        v-model="value"
        v-bind="{ ...$attrs, ...styleAttrs }"
        type="text"
        :class="{ required: $attrs.required }"
        @keyup.enter="onEnterPress()"
        :rules="$attrs.required ? [requiredFieldRule] : null"
    >
        <template v-if="$slots.prepend" #prepend>
            <slot name="prepend" />
        </template>
        <template v-if="$slots.append" #append>
            <slot name="append" />
        </template>
    </QInput>
</template>
