import Failure from "../failure";

interface ResponseFailureHandler {
  nextHandler?: ResponseFailureHandler;
  handle(reason: Failure): void;
  setNext(next: ResponseFailureHandler): void;
}

type FailureCallbackFunction = (reason?: Failure) => void;

class BaseFailureHandler implements ResponseFailureHandler {
  nextHandler?: ResponseFailureHandler | undefined;

  constructor(nextHandler?: ResponseFailureHandler) {
    this.nextHandler = nextHandler;
  }

  handle(reason: Failure) {
    if (this.nextHandler) {
      return this.nextHandler.handle(reason);
    }
  }

  setNext(next: ResponseFailureHandler): void {
    this.nextHandler = next;
  }
}

export { BaseFailureHandler };
export type { ResponseFailureHandler, FailureCallbackFunction };
