export function parseRupiah(value: string | number): string {
    const str = String(value);
    const isNegative = str.startsWith('-') || (typeof value === 'number' && value < 0);

    return isNegative ? '-' + str.replace(/[^0-9]/g, '') : str.replace(/[^0-9]/g, '');
}

export function formatRupiah(value: string | number): string {
    const digits = parseRupiah(value);

    if (!digits) {
        return '';
    }

    return Number(digits).toLocaleString('id-ID');
}

export function formatRupiahShort(value: number): string {
    const abs = Math.abs(value);
    const sign = value < 0 ? '-' : '';

    if (abs >= 1_000_000_000) {
        const v = abs / 1_000_000_000;

        return sign + (v % 1 === 0 ? v.toFixed(0) : v.toFixed(1).replace('.', ',')) + 'M';
    }

    if (abs >= 1_000_000) {
        const v = abs / 1_000_000;

        return sign + (v % 1 === 0 ? v.toFixed(0) : v.toFixed(1).replace('.', ',')) + 'jt';
    }

    if (abs >= 1_000) {
        const v = abs / 1_000;

        return sign + (v % 1 === 0 ? v.toFixed(0) : v.toFixed(1).replace('.', ',')) + 'rb';
    }

    return sign + abs.toString();
}
