text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.RouterResponseController = void 0;
  const common_1 = require("@nestjs/common");
  const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
  const operators_1 = require("rxjs/operators");
  const sse_stream_1 = require("./sse-stream");
  class RouterResponseController {
      constructor(applicationRef) {
          this.applicationRef = applicationRef;
      }
      async apply(result, response, httpStatusCode) {
          return this.applicationRef.reply(response, result, httpStatusCode);
      }
      async redirect(resultOrDeferred, response, redirectResponse) {
          const result = await this.transformToResult(resultOrDeferred);
          const statusCode = result && result.statusCode
              ? result.statusCode
              : redirectResponse.statusCode
                  ? redirectResponse.statusCode
                  : common_1.HttpStatus.FOUND;
          const url = result && result.url ? result.url : redirectResponse.url;
          this.applicationRef.redirect(response, statusCode, url);
      }
      async render(resultOrDeferred, response, template) {
          const result = await this.transformToResult(resultOrDeferred);
          return this.applicationRef.render(response, template, result);
      }
      async transformToResult(resultOrDeferred) {
          if (resultOrDeferred && shared_utils_1.isFunction(resultOrDeferred.subscribe)) {
              return resultOrDeferred.toPromise();
          }
          return resultOrDeferred;
      }
      getStatusByMethod(requestMethod) {
          switch (requestMethod) {
              case common_1.RequestMethod.POST:
                  return common_1.HttpStatus.CREATED;
              default:
                  return common_1.HttpStatus.OK;
          }
      }
      setHeaders(response, headers) {
          headers.forEach(({ name, value }) => this.applicationRef.setHeader(response, name, value));
      }
      setStatus(response, statusCode) {
          this.applicationRef.status(response, statusCode);
      }
      async sse(result, response, request) {
          this.assertObservable(result);
          const stream = new sse_stream_1.SseStream(request);
          stream.pipe(response);
          const subscription = result
              .pipe(operators_1.debounce((message) => new Promise(resolve => {
              if (!shared_utils_1.isObject(message)) {
                  message = { data: message };
              }
              stream.writeMessage(message, resolve);
          })))
              .subscribe();
          request.on('close', () => {
              response.end();
              subscription.unsubscribe();
          });
      }
      assertObservable(result) {
          if (!shared_utils_1.isFunction(result.subscribe)) {
              throw new ReferenceError('You must return an Observable stream to use Server-Sent Events (SSE).');
          }
      }
  }
  exports.RouterResponseController = RouterResponseController;
  