import { TypedEventBus } from "../typed-event-bus.js";
import { serve } from "@hono/node-server";
import { EventEmitter, once } from "node:events";
import { Hono } from "hono";
//#region src/extensions/sdk/testing.ts
const noopLogger = {
	debug: () => {},
	info: () => {},
	warn: () => {},
	error: () => {}
};
function createMockExtensionApi(options = {}) {
	return new MockExtensionApiImpl(options);
}
var MockExtensionApiImpl = class {
	id;
	name;
	version;
	source;
	config;
	extensionConfig;
	logger;
	runtime;
	events;
	_tools = [];
	_hooks = [];
	_commands = [];
	_http = [];
	_services = [];
	_gw = [];
	_channels = [];
	_reload = [];
	_emitted = [];
	_bus = new EventEmitter();
	_manifestCommands = /* @__PURE__ */ new Map();
	constructor(opts) {
		this.id = opts.id ?? "test-extension";
		this.name = opts.name ?? "Test Extension";
		this.version = opts.version;
		this.source = opts.source ?? "/test";
		this.extensionConfig = opts.extensionConfig ?? {};
		this.config = opts.config ?? {};
		this.logger = opts.logger ?? noopLogger;
		this.runtime = {
			config: this.config,
			log: this.logger
		};
		this.events = new TypedEventBus({ logger: this.logger });
		if (opts.manifestCommands?.length) for (const c of opts.manifestCommands) this._manifestCommands.set(c.id, c);
	}
	_setManifestCommands(commands) {
		this._manifestCommands.clear();
		for (const c of commands) this._manifestCommands.set(c.id, c);
	}
	getRegisteredTools() {
		return [...this._tools];
	}
	getRegisteredHooks() {
		return [...this._hooks];
	}
	getRegisteredCommands() {
		return [...this._commands];
	}
	getRegisteredHttpRoutes() {
		return [...this._http];
	}
	getRegisteredServices() {
		return [...this._services];
	}
	getRegisteredGatewayMethods() {
		return [...this._gw];
	}
	getRegisteredChannelPlugins() {
		return [...this._channels];
	}
	getRegisteredReloadHandlers() {
		return [...this._reload];
	}
	getEmittedEvents() {
		return [...this._emitted];
	}
	registerTool(tool) {
		this._tools.push(tool);
	}
	registerHook(event, handler, _opts) {
		this._hooks.push({
			event,
			handler
		});
	}
	onHook(_hookName, _handler) {}
	registerChannel(registration) {
		this._channels.push(registration.plugin);
	}
	registerHttpRoute(path, handler) {
		this._http.push({
			path,
			handler
		});
	}
	registerCommand(command) {
		this._commands.push(command);
	}
	onCommand(commandId, handler) {
		const meta = this._manifestCommands.get(commandId);
		if (!meta) {
			const name = commandId.includes(".") ? (commandId.split(".").pop() ?? commandId).trim() : commandId;
			this.registerCommand({
				name: name || commandId,
				description: commandId,
				handler
			});
			return;
		}
		let name;
		if (meta.chatAlias?.trim()) name = meta.chatAlias.trim().replace(/^\//, "");
		else name = meta.id.includes(".") ? meta.id.split(".").pop() ?? meta.id : meta.id;
		if (!name) name = meta.id;
		this.registerCommand({
			name,
			description: meta.title,
			handler
		});
	}
	registerReload(handler) {
		this._reload.push(handler);
	}
	registerService(service) {
		this._services.push(service);
	}
	registerGatewayMethod(method, handler) {
		this._gw.push({
			method,
			handler
		});
	}
	registerCli(_factory, _opts) {}
	resolvePath(input) {
		return `${this.source.replace(/\/$/, "")}/${input.replace(/^\//, "")}`;
	}
	emit(event, data) {
		this._emitted.push({
			event,
			data
		});
		this._bus.emit(event, data);
	}
	on(event, handler) {
		this._bus.on(event, handler);
	}
	off(event, handler) {
		this._bus.off(event, handler);
	}
	registerProvider(_plugin) {}
	registerProviderPlugin(_plugin) {}
	registerSpeechProvider(_plugin) {}
	registerMediaUnderstandingProvider(_plugin) {}
	registerMarketplaceAdapter(_registration) {}
	registerFlag(_name, _config) {}
	getFlag(_name) {}
	registerShortcut(_key, _config) {}
	registerTui(_register) {}
};
async function createTestGateway() {
	const app = new Hono();
	app.get("/health", (c) => c.json({ ok: true }));
	const server = serve({
		fetch: app.fetch,
		port: 0,
		hostname: "127.0.0.1"
	});
	if (!server.listening) await once(server, "listening");
	const baseUrl = `http://127.0.0.1:${server.address().port}`;
	return {
		baseUrl,
		fetch: (path, init) => fetch(`${baseUrl}${path.startsWith("/") ? path : `/${path}`}`, init),
		close: () => new Promise((resolve, reject) => {
			server.close((err) => err ? reject(err) : resolve());
		})
	};
}
//#endregion
export { createMockExtensionApi, createTestGateway };

//# sourceMappingURL=testing.js.map