import { inject } from '@angular/core';
import { Result } from '@shared/kernel/fp';
import { Valid } from '@registratie/domain/change-request.machine';
import { runSubmit, SUBMIT_FAILED } from '@shared/application/submit';
import { ChangeRequestAdapter } from '@registratie/infrastructure/change-request.adapter';

/**
 * Command factory: binds the change-request adapter (which owns the `ApiClient`)
 * in an injection context and returns the submit function the form calls. Same
 * field-initializer shape as `createStore`/`createDraftSync`, so the UI holds an
 * application command — not the network client. Returns a `Result`, never a thrown
 * error, so the form's reduce can branch on the outcome.
 */
export function createSubmitChangeRequest() {
  const adapter = inject(ChangeRequestAdapter);
  return (data: Valid): Promise<Result<string, string>> =>
    runSubmit(() => adapter.changeRequest(data), SUBMIT_FAILED);
}
