<script setup lang="ts">
import type { HTMLAttributes } from 'vue';
import { computed } from 'vue';
import { Input } from '@/components/ui/input';
import { formatRupiah, parseRupiah } from '@/lib/rupiah';
import { cn } from '@/lib/utils';

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: () => formatRupiah(props.modelValue ?? ''),
    set: (value: string) => {
        emit('update:modelValue', parseRupiah(value));
    },
});
</script>

<template>
    <div class="relative">
        <span class="text-muted-foreground pointer-events-none absolute top-1/2 left-3 -translate-y-1/2 text-sm">
            Rp
        </span>
        <Input :id="id" v-model="displayValue" inputmode="numeric" autocomplete="off" :placeholder="placeholder ?? '0'"
            :disabled="disabled" :class="cn('pl-9', props.class)" />
    </div>
</template>
