import { AuthError, FatalError, QuotaExhaustedError, RateLimitError, TransientError } from "./index.js";
//#region src/mock.ts
const SENTINEL_RE = /@@MOCK:([A-Z_]+)(?::([^@]*))?@@/;
const THROW_DIRECTIVES = /* @__PURE__ */ new Set([
	"ERROR",
	"RATELIMIT",
	"TRANSIENT",
	"QUOTA",
	"AUTH"
]);
function parseSentinel(text) {
	const m = SENTINEL_RE.exec(text);
	if (!m) return null;
	return {
		kind: m[1],
		arg: m[2]?.trim() || void 0
	};
}
function modelDirective(model) {
	if (!model.startsWith("TEST")) return null;
	const suffix = model.slice(4).replace(/^-/, "");
	if (suffix === "RATELIMIT") return { kind: "RATELIMIT" };
	if (suffix === "TRANSIENT") return { kind: "TRANSIENT" };
	if (suffix === "QUOTA") return { kind: "QUOTA" };
	if (suffix === "AUTH") return { kind: "AUTH" };
	if (suffix === "ERROR") return { kind: "ERROR" };
	return null;
}
function extractMessageText(messages) {
	return messages.map((message) => typeof message.content === "string" ? message.content : message.content.filter((block) => block.type === "text").map((block) => block.text).join("")).join("\n");
}
function resolveMockDirective(req) {
	return parseSentinel(extractMessageText(req.messages)) ?? modelDirective(req.model);
}
function throwForMockDirective(kind, arg) {
	const msg = arg ?? `[TEST] mock ${kind}`;
	if (kind === "RATELIMIT") throw new RateLimitError(msg, "mock");
	if (kind === "TRANSIENT") throw new TransientError(msg, "mock");
	if (kind === "QUOTA") throw new QuotaExhaustedError(msg, "mock");
	if (kind === "AUTH") throw new AuthError(msg, "mock");
	throw new FatalError(msg, "mock");
}
function makeMockAdapter() {
	return {
		provider: "mock",
		supportsImage: false,
		async call(req) {
			const directive = resolveMockDirective(req);
			if (directive && THROW_DIRECTIVES.has(directive.kind)) throwForMockDirective(directive.kind, directive.arg);
			return {
				content: "[TEST] mock ok",
				model: req.model,
				provider: "mock",
				usage: {
					promptTokens: 1,
					completionTokens: 1
				}
			};
		}
	};
}
//#endregion
export { extractMessageText, makeMockAdapter, resolveMockDirective, throwForMockDirective };
