const MAX_DIGITS = 6;

export function keypadReduce(current: string, key: string): string {
  if (key === "back") return current.slice(0, -1);
  if (!/^\d$/.test(key)) return current;
  if (current === "0") return key === "0" ? "0" : key;  // replace leading zero
  if (current === "" && key === "0") return "";          // no leading zero from empty
  if (current.length >= MAX_DIGITS) return current;
  return current + key;
}

export function withCommission(base: number, rate: number): { commission: number; total: number } {
  const commission = Math.round(base * rate * 100) / 100;
  const total = Math.round((base + commission) * 100) / 100;
  return { commission, total };
}
