import { toast } from './store/toasts'
import { logError } from './reportError'

/**
 * Open a downloaded file, or reveal it in its folder, surfacing any failure as a
 * toast (UX6) instead of silently doing nothing. Clicking "Open file" on a
 * download that was later moved or deleted used to no-op with no message; now the
 * main-process reason ("File not found — it may have been moved…", "Refusing to
 * open this file type.") is shown. Both IPC calls resolve to '' on success or an
 * error string; a rejected promise (a bridge failure) is logged.
 */
export function revealFile(action: 'open' | 'folder', filePath: string): void {
  const call = action === 'open' ? window.api.openPath(filePath) : window.api.showInFolder(filePath)
  call
    .then((err) => {
      if (err) toast(err, 'error')
    })
    .catch(logError(action === 'open' ? 'openPath' : 'showInFolder'))
}
