import { Result, ok, err } from '@shared/kernel/fp';
import { problemDetail } from '@shared/infrastructure/api-error';

/**
 * Run a mutating API call and fold it into a `Result` — the one place the
 * try/catch + ProblemDetails-mapping lives, so every `submit-*` command is just
 * its own payload mapping. The backend re-validates and returns a 422
 * ProblemDetails on rejection, surfaced here as the error string.
 */
export async function runSubmit<T>(fn: () => Promise<T>, fallback: string): Promise<Result<string, T>> {
  try {
    return ok(await fn());
  } catch (e) {
    return err(problemDetail(e, fallback));
  }
}

// Single shared default for a failed submit; the @@id dedupes it at the
// translation layer.
export const SUBMIT_FAILED = $localize`:@@submit.failed:Het indienen is niet gelukt. Probeer het later opnieuw.`;
