"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get MiddlewareManager () {
        return MiddlewareManager;
    },
    get middlewareManager () {
        return middlewareManager;
    }
});
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
let MiddlewareManager = class MiddlewareManager {
    /**
   * 添加中间件
   */ use(middleware) {
        this.middlewares.push(middleware);
    }
    /**
   * 移除中间件
   */ remove(middleware) {
        const index = this.middlewares.indexOf(middleware);
        if (index > -1) {
            this.middlewares.splice(index, 1);
        }
    }
    /**
   * 清空所有中间件
   */ clear() {
        this.middlewares = [];
    }
    /**
   * 执行中间件链
   */ async execute(options, finalHandler) {
        let index = 0;
        const next = async ()=>{
            if (index >= this.middlewares.length) {
                return finalHandler();
            }
            const middleware = this.middlewares[index++];
            return middleware(options, next);
        };
        return next();
    }
    /**
   * 获取中间件数量
   */ get count() {
        return this.middlewares.length;
    }
    constructor(){
        _define_property(this, "middlewares", []);
    }
};
const middlewareManager = new MiddlewareManager();

//# sourceMappingURL=middleware.js.map