import { useCallback } from "react";

export function useStringAdapters({
  stringDefaultValue,
  stringValue,
  onChangeString,
  stringMinValue,
  stringMaxValue,
}: {
  stringDefaultValue?: string;
  stringValue?: string;
  onChangeString?: (event: { target: { value: string } }) => void;
  stringMinValue?: string;
  stringMaxValue?: string;
}) {
  const onChange = useCallback(
    (newValue: number) =>
      onChangeString?.({ target: { value: newValue ? String(newValue) : "" } }),
    [onChangeString]
  );

  return {
    defaultValue:
      stringDefaultValue !== undefined
        ? parseFloat(stringDefaultValue)
        : undefined,
    value: stringValue !== undefined ? parseFloat(stringValue) : undefined,
    onChange,
    minValue:
      stringMinValue !== undefined ? parseFloat(stringMinValue) : undefined,
    maxValue:
      stringMaxValue !== undefined ? parseFloat(stringMaxValue) : undefined,
  };
}
