<script>
    export let value=null

    $: value = formatRut(value)

    function formatRut(str) {
        if (!str) return str;
        if (str.length < 3) return str;

        const arr = str.replace(/[.|-]/g,'').split('')
        const dv = arr.pop().toUpperCase()
        const num = Number(arr.join(''))
        if (isNaN(num)) return str;
        return num.toLocaleString('en').replace(/,/g,'.') + '-' + dv;
    }
</script>

<input type="text" bind:value={value} class="form-control" {...$$props}>