export async function copyTextWithClipboardFallback(
  text: unknown,
  fallbackTitle: string,
  onClipboardSuccess: () => void,
  onFallbackSuccess: (clipboardAvailable: boolean) => void,
) {
  const normalizedText = String(text || "");
  try {
    if (navigator?.clipboard?.writeText) {
      await navigator.clipboard.writeText(normalizedText);
      onClipboardSuccess();
      return true;
    }
    window.prompt(fallbackTitle, normalizedText);
    onFallbackSuccess(false);
    return true;
  } catch {
    window.prompt(fallbackTitle, normalizedText);
    onFallbackSuccess(true);
    return true;
  }
}

export function promptForImportText(promptLabel: string, defaultValue = "") {
  return window.prompt(promptLabel, defaultValue);
}

export function confirmDiscardChanges(message: string, isDirty: boolean) {
  if (!isDirty) {
    return true;
  }
  return window.confirm(message);
}
