/**
 * Shared numpad input handler for EUR amount entry.
 * Used by every modal with a custom numpad (transactions, debts, fixed costs, etc.).
 */

/** Processes a numpad key press and returns the updated amount string. */
export function handleNumpadKey(current: string, key: string): string {
  if (key === "\u232B") {
    return current.length > 1 ? current.slice(0, -1) : "0";
  }
  if (key === ",") {
    return current.includes(",") ? current : current + ",";
  }
  if (current === "0") {
    return key;
  }
  const parts = current.split(",");
  if (parts[1] !== undefined && parts[1].length >= 2) {
    return current;
  }
  return current + key;
}

/** Parses a German-format amount string ("1.234,56") to a number. */
export function parseAmountStr(amountStr: string): number {
  return parseFloat(amountStr.replace(",", ".")) || 0;
}

/** The numpad key layout used across all modals. */
export const NUMPAD_KEYS: string[][] = [
  ["1", "2", "3"],
  ["4", "5", "6"],
  ["7", "8", "9"],
  [",", "0", "\u232B"],
];
