import { TypeOrmPersistenceSchemaError } from './TypeOrmPersistenceSchemaError';

type InvalidResultSchemaErrorParams = {
  fieldName: string;
  reason: TypeOrmPersistenceSchemaError['reason'];
  message?: string;
};

export class InvalidResultSchemaError extends TypeOrmPersistenceSchemaError {
  constructor(params: InvalidResultSchemaErrorParams);
  constructor(message: string);
  constructor(paramsOrMessage: InvalidResultSchemaErrorParams | string) {
    const params =
      typeof paramsOrMessage === 'string'
        ? {
            entityName: 'Result',
            fieldName: 'unknown',
            reason: 'invalid_shape' as const,
            message: paramsOrMessage,
          }
        : {
            entityName: 'Result',
            fieldName: paramsOrMessage.fieldName,
            reason: paramsOrMessage.reason,
            ...(paramsOrMessage.message ? { message: paramsOrMessage.message } : {}),
          };

    super(params);
    this.name = 'InvalidResultSchemaError';
  }
}