"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const typedi_1 = require("typedi");
const ProxyServer_1 = require("../../ProxyServer");
const middleware_1 = require("../middleware");
const plugin_manager_1 = __importDefault(require("../plugin-manager"));
const services_1 = require("../services");
let Proxy = class Proxy {
    listen(port) {
        return __awaiter(this, void 0, void 0, function* () {
            this.server.listen(port);
        });
    }
    ignore(pattern) {
        this.ignorer.addPattern(pattern);
    }
    init() {
        return __awaiter(this, void 0, void 0, function* () {
            this.server = yield ProxyServer_1.ProxyServer.create();
            this.ignorer = new middleware_1.Ignorer();
            this.server.use(this.ignorer.middleware.bind(this.ignorer));
            this.server.use(middleware_1.ip());
            this.server.use(middleware_1.user(typedi_1.Container.get(services_1.ProfileService)));
            this.server.use(middleware_1.endPoint(typedi_1.Container.get(services_1.HttpTrafficService)));
            this.server.use(middleware_1.rule({
                mockDataService: typedi_1.Container.get(services_1.MockDataService),
                profileService: typedi_1.Container.get(services_1.ProfileService),
                ruleService: typedi_1.Container.get(services_1.RuleService),
            }));
            const pluginManager = typedi_1.Container.get(plugin_manager_1.default);
            pluginManager.loadProxyMiddleware(this.server);
            this.server.use(middleware_1.host(typedi_1.Container.get(services_1.HostService), typedi_1.Container.get(services_1.ProfileService)));
            this.server.use(middleware_1.actualRequest(typedi_1.Container.get(services_1.HttpTrafficService)));
        });
    }
};
Proxy = __decorate([
    typedi_1.Service()
], Proxy);
exports.Proxy = Proxy;
//# sourceMappingURL=index.js.map