import { ResponseArgs } from '../models';
import { DataAccessError } from './data-access.error';

function invalidPropertyErrorMessage(
  invalidProperties: Record<string, string> | undefined,
): string | undefined {
  if (!invalidProperties || Object.keys(invalidProperties).length === 0) {
    return undefined;
  }

  return Object.entries(invalidProperties)
    .map(([key, value]) => `${key}: ${value}`)
    .join('; ');
}

export class ResponseArgsError<
  T,
> extends DataAccessError<'RESPONSE_ARGS_ERROR'> {
  constructor(public readonly responseArgs: Omit<ResponseArgs<T>, 'result'>) {
    super(
      'RESPONSE_ARGS_ERROR',
      responseArgs.message ||
        invalidPropertyErrorMessage(responseArgs.invalidProperties) ||
        'An error occurred while processing response arguments',
    );
  }
}
