"use client";

import { forwardRef } from "react";
import { Input } from "@/components/ui/input";
import { formatNumber, parseRupiah } from "@/lib/format";
import { cn } from "@/lib/utils";

interface CurrencyInputProps extends Omit<React.ComponentProps<"input">, "value" | "onChange" | "type"> {
  value: number | null | undefined;
  onChange: (value: number | null) => void;
}

/** Input Rupiah: tampil "150.000", nilai yang dikirim berupa angka 150000. */
export const CurrencyInput = forwardRef<HTMLInputElement, CurrencyInputProps>(({ value, onChange, className, ...props }, ref) => (
  <div className="relative">
    <span className="pointer-events-none absolute left-3 top-1/2 -translate-y-1/2 text-sm text-muted-foreground">Rp</span>
    <Input
      ref={ref}
      inputMode="numeric"
      autoComplete="off"
      className={cn("pl-10 tabular-nums", className)}
      value={value === null || value === undefined ? "" : formatNumber(value)}
      onChange={(e) => onChange(parseRupiah(e.target.value))}
      {...props}
    />
  </div>
));
CurrencyInput.displayName = "CurrencyInput";
