import {
  mergeDeep
} from "./chunk-FKHYE7Y6.mjs";

// src/plugin.ts
import debug from "debug";
import Koa from "koa";
import compose from "koa-compose";
var log = debug("@tomrpc/core/plugin");
var Plugable = class {
  constructor(cfg) {
    this.init = [];
    this.load = [];
    this.config = mergeDeep(
      {
        bind: {}
      },
      cfg
    );
    this.name = "base";
    this.app = new Koa();
    this.init = [];
    this.load = [];
    this.prefix = this.config.prefix ? this.config.prefix : "";
    this.compose = compose;
    this.load.push(this.getMiddleware());
  }
  getConfig(ctx) {
    return ctx[this.name];
  }
  getMiddleware() {
    const pre = this.pre();
    const process = this.process();
    const post = this.post();
    return compose([pre, process, post]);
  }
  pre() {
    return async (ctx, next) => {
      log("pre");
      await next();
      log("pre end");
    };
  }
  post() {
    return async (ctx, next) => {
      log("after");
      await next();
      log("after end");
    };
  }
  process() {
    return async (ctx, next) => {
      log("process default");
      await next();
      log("process default end");
    };
  }
  use(x) {
    this.app.use(x);
  }
  addInit(i) {
    this.init.push(i);
  }
  addLoad(i) {
    this.load.push(i);
  }
};

export {
  Plugable
};
