<script setup lang="ts">
import type { HTMLAttributes } from 'vue';
import { computed } from 'vue';
import { Input } from '@/components/ui/input';
import { FIELD_LIMITS } from '@/lib/field-limits';
import { formatPhoneNumber, parsePhoneNumber } from '@/lib/phone-number';

const props = defineProps<{
    id?: string;
    modelValue?: string | number;
    class?: HTMLAttributes['class'];
    placeholder?: string;
    disabled?: boolean;
}>();

const emit = defineEmits<{
    (event: 'update:modelValue', value: string): void;
}>();

const displayValue = computed({
    get: () => formatPhoneNumber(props.modelValue ?? ''),
    set: (value: string) => {
        emit('update:modelValue', parsePhoneNumber(value));
    },
});
</script>

<template>
    <Input :id="id" v-model="displayValue" type="tel" inputmode="tel" autocomplete="off"
        :placeholder="placeholder ?? '08xx-xxxx-xxxx'" :maxlength="FIELD_LIMITS.phoneNumber" :disabled="disabled"
        :class="props.class" />
</template>
