import 'package:flutter/material.dart';

import 'async_action_button.dart';

/// Single-line text-input dialog. The confirm action runs [onConfirm] with the
/// trimmed input via [AsyncDialogAction], so it shows a spinner and an inline
/// error and only closes on success. Throw inside [onConfirm] to keep the
/// dialog open with a message (e.g. for empty or duplicate input).
void showPromptDialog(
  BuildContext context, {
  required String title,
  required String confirmButton,
  required Future<void> Function(String value) onConfirm,
  String label = 'Name',
  String initialValue = '',
  AsyncErrorBuilder? errorBuilder,
}) {
  final controller = TextEditingController(text: initialValue);
  showDialog(
    context: context,
    builder: (dialogCtx) => AlertDialog(
      title: Text(title),
      content: TextField(
        controller: controller,
        decoration: InputDecoration(labelText: label),
        autofocus: true,
      ),
      actions: [
        AsyncDialogAction(
          confirmLabel: confirmButton,
          onConfirm: () => onConfirm(controller.text.trim()),
          errorBuilder: errorBuilder,
        ),
      ],
    ),
  ).whenComplete(controller.dispose);
}
