"use strict";
/**
 * @adonisjs/http-server
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("@poppinss/utils");
class HttpServerProvider {
    constructor(application) {
        this.application = application;
    }
    /**
     * Validate server config to ensure we start with sane defaults
     */
    validateServerConfig(config) {
        if (!config.cookie || typeof config.cookie !== 'object') {
            throw new utils_1.Exception('Missing "cookie" config inside the "http" block in "config/app" file.');
        }
        if (typeof config.trustProxy !== 'function') {
            throw new utils_1.Exception('Invalid "trustProxy" value inside the "http" block in "config/app" file.');
        }
    }
    /**
     * Register request and response bindings to the container
     */
    registerRequestResponse() {
        this.application.container.singleton('Adonis/Core/Request', () => {
            return require('../src/Request').Request;
        });
        this.application.container.singleton('Adonis/Core/Response', () => {
            return require('../src/Response').Response;
        });
    }
    /**
     * Registering middleware store to the container
     */
    registerMiddlewareStore() {
        this.application.container.bind('Adonis/Core/MiddlewareStore', () => {
            return require('../src/MiddlewareStore').MiddlewareStore;
        });
    }
    /**
     * Registering the HTTP context
     */
    registerHTTPContext() {
        this.application.container.bind('Adonis/Core/HttpContext', () => {
            const { HttpContext } = require('../src/HttpContext');
            HttpContext.app = this.application.container.resolveBinding('Adonis/Core/Application');
            return HttpContext;
        });
    }
    /**
     * Register the HTTP server
     */
    registerHttpServer() {
        this.application.container.singleton('Adonis/Core/Server', () => {
            const { Server } = require('../src/Server');
            const Config = this.application.container.resolveBinding('Adonis/Core/Config');
            const Encryption = this.application.container.resolveBinding('Adonis/Core/Encryption');
            const serverConfig = Config.get('app.http', {});
            this.validateServerConfig(serverConfig);
            return new Server(this.application, Encryption, serverConfig);
        });
    }
    /**
     * Register the router. The router points to the instance of router used
     * by the middleware
     */
    registerRouter() {
        this.application.container.singleton('Adonis/Core/Route', () => {
            return this.application.container.resolveBinding('Adonis/Core/Server').router;
        });
    }
    /**
     * Registers the cookie client with the container
     */
    registerCookieClient() {
        this.application.container.singleton('Adonis/Core/CookieClient', () => {
            const { CookieClient } = require('../src/Cookie/Client');
            const Encryption = this.application.container.resolveBinding('Adonis/Core/Encryption');
            return new CookieClient(Encryption);
        });
    }
    /**
     * Registering all bindings
     */
    register() {
        this.registerRequestResponse();
        this.registerMiddlewareStore();
        this.registerHttpServer();
        this.registerHTTPContext();
        this.registerRouter();
        this.registerCookieClient();
    }
}
exports.default = HttpServerProvider;
