var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/proxy.ts
var proxy_exports = {};
__export(proxy_exports, {
  Proxy: () => Proxy2
});
module.exports = __toCommonJS(proxy_exports);
var import_debug3 = __toESM(require("debug"));

// src/plugin.ts
var import_debug2 = __toESM(require("debug"));
var import_koa = __toESM(require("koa"));
var import_koa_compose = __toESM(require("koa-compose"));

// src/utils.ts
var import_debug = __toESM(require("debug"));
var debugLog = (0, import_debug.default)("@tomrpc/core");
function isObject(item) {
  return item && typeof item === "object" && !Array.isArray(item);
}
function mergeDeep(target, ...sources) {
  if (!sources.length)
    return target;
  const source = sources.shift();
  if (isObject(target) && isObject(source)) {
    for (const key in source) {
      if (isObject(source[key])) {
        if (!target[key])
          Object.assign(target, { [key]: {} });
        mergeDeep(target[key], source[key]);
      } else {
        Object.assign(target, { [key]: source[key] });
      }
    }
  }
  return mergeDeep(target, ...sources);
}

// src/plugin.ts
var log = (0, import_debug2.default)("@tomrpc/core/plugin");
var Plugable = class {
  constructor(cfg) {
    this.init = [];
    this.load = [];
    this.config = mergeDeep(
      {
        bind: {}
      },
      cfg
    );
    this.name = "base";
    this.app = new import_koa.default();
    this.init = [];
    this.load = [];
    this.prefix = this.config.prefix ? this.config.prefix : "";
    this.compose = import_koa_compose.default;
    this.load.push(this.getMiddleware());
  }
  /**
   * move middleware to load stage, use template pattern & compose
   */
  getMiddleware() {
    log("getMiddleware");
    const pre = this.pre();
    const process = this.process();
    const post = this.post();
    return (0, import_koa_compose.default)([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);
  }
};

// src/proxy.ts
var log2 = (0, import_debug3.default)("@tomrpc/core/fn");
var ProxyDefaultConfig = {
  proxy: {
    inject: "before",
    //init | load | before | after
    before: []
  }
};
var Proxy2 = class extends Plugable {
  constructor(cfg) {
    super(mergeDeep(ProxyDefaultConfig, cfg));
  }
  proxy() {
    return async (ctx, next) => {
      log2("proxy default");
      await next();
      log2("proxy default end");
    };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Proxy
});
