import { createLogger } from "../../../../../src/utils/logger/index.js";
import { init_logger } from "../../../../../src/utils/logger.js";
//#region extensions/feishu/src/transport/client/lark-sdk-logger.ts
init_logger();
function normalizeLarkLogArgs(args) {
	const raw = args.length === 1 ? args[0] : args;
	if (Array.isArray(raw)) {
		const errIdx = raw.findIndex((x) => x instanceof Error);
		if (errIdx >= 0) {
			const err = raw[errIdx];
			return {
				parts: raw.filter((_, i) => i !== errIdx),
				err
			};
		}
		return { parts: raw };
	}
	return { parts: [raw] };
}
function stringifyParts(parts) {
	const text = parts.map((p) => {
		if (typeof p === "string") return p;
		if (p instanceof Error) return p.message;
		if (p === null || p === void 0) return "";
		try {
			return JSON.stringify(p);
		} catch {
			return String(p);
		}
	}).join(" ").replace(/\r\n/g, "\n").replace(/\r/g, "\n").trim();
	return {
		text,
		hasLongLines: text.includes("\n") || text.length > 240
	};
}
function linePreview(text, max = 240) {
	const oneLine = text.replace(/\s+/g, " ").trim();
	if (oneLine.length <= max) return oneLine;
	return oneLine.slice(0, max - 1) + "…";
}
/**
* Adapts `@larksuiteoapi/node-sdk`'s `Logger` interface to xopc's pino-based logger.
*
* The SDK wraps calls like `this.logger.info('a', 'b')` into `LoggerProxy.info` which forwards a *single* array
* argument to the underlying logger — which is why the default output looks like: `[info]: [ 'a', 'b' ]`.
*/
function createFeishuLarkSdkPinoLogger(accountId) {
	const log = createLogger("FeishuLarkSDK");
	const write = (level, args) => {
		const { parts, err } = normalizeLarkLogArgs(args);
		const { text, hasLongLines } = stringifyParts(parts);
		if (!text && !err) return;
		if (err) {
			log[level]({
				err,
				accountId,
				linePreview: text ? linePreview(text) : void 0,
				component: "lark-sdk"
			}, text || err.message);
			return;
		}
		if (level === "info" && (text === "client ready" || /^\[ws\]\s*ws client ready$/.test(text))) {
			log.info({
				accountId,
				component: "lark-sdk",
				lark: "api+ws"
			}, "Lark SDK is ready (HTTP client + websocket)");
			return;
		}
		if (level === "info" && text === "event-dispatch is ready") {
			log.info({
				accountId,
				component: "lark-sdk",
				lark: "event-dispatch"
			}, "Lark event dispatcher is ready");
			return;
		}
		if (level === "info" && hasLongLines) {
			log.info({
				accountId,
				component: "lark-sdk",
				lark: "event-subscription",
				linePreview: linePreview(text)
			}, "Lark subscription guidance: enable persistent connection mode in Developer Console (self-build + Feishu app)");
			return;
		}
		log[level]({
			accountId,
			component: "lark-sdk",
			linePreview: linePreview(text)
		}, text);
	};
	return {
		error: (...args) => write("error", args),
		warn: (...args) => write("warn", args),
		info: (...args) => write("info", args),
		debug: (...args) => write("debug", args),
		trace: (...args) => write("trace", args)
	};
}
//#endregion
export { createFeishuLarkSdkPinoLogger };

//# sourceMappingURL=lark-sdk-logger.js.map