const require_helpers = require('./helpers.cjs');
const require_router = require('./router.cjs');
const require_sse_writer = require('./sse-writer.cjs');
const require_chaos = require('./chaos.cjs');
const require_recorder = require('./recorder.cjs');

//#region src/embeddings.ts
async function handleEmbeddings(req, res, raw, fixtures, journal, defaults, setCorsHeaders, providerKey = "openai") {
	const { logger } = defaults;
	setCorsHeaders(res);
	let embeddingReq;
	try {
		embeddingReq = JSON.parse(raw);
	} catch (parseErr) {
		const detail = parseErr instanceof Error ? parseErr.message : "unknown";
		journal.add({
			method: req.method ?? "POST",
			path: req.url ?? "/v1/embeddings",
			headers: require_helpers.flattenHeaders(req.headers),
			body: null,
			response: {
				status: 400,
				fixture: null
			}
		});
		require_sse_writer.writeErrorResponse(res, 400, JSON.stringify({ error: {
			message: `Malformed JSON body: ${detail}`,
			type: "invalid_request_error",
			code: "invalid_json"
		} }));
		return;
	}
	if (embeddingReq.input === void 0 || embeddingReq.input === null) {
		journal.add({
			method: req.method ?? "POST",
			path: req.url ?? "/v1/embeddings",
			headers: require_helpers.flattenHeaders(req.headers),
			body: null,
			response: {
				status: 400,
				fixture: null
			}
		});
		require_sse_writer.writeErrorResponse(res, 400, JSON.stringify({ error: {
			message: "Missing required parameter: 'input'",
			type: "invalid_request_error"
		} }));
		return;
	}
	const inputs = Array.isArray(embeddingReq.input) ? embeddingReq.input : [embeddingReq.input];
	const combinedInput = inputs.join(" ");
	const syntheticReq = {
		model: embeddingReq.model,
		messages: [],
		embeddingInput: combinedInput,
		_endpointType: "embedding",
		_context: require_helpers.getContext(req)
	};
	const testId = require_helpers.getTestId(req);
	const { fixture, skippedBySequenceOrTurn } = require_router.matchFixtureDiagnostic(fixtures, syntheticReq, journal.getFixtureMatchCountsForTest(testId), defaults.requestTransform);
	if (fixture) {
		journal.incrementFixtureMatchCount(fixture, fixtures, testId);
		logger.debug(`Fixture matched: ${JSON.stringify(fixture.match).slice(0, 120)}`);
	} else logger.debug(`No fixture matched for request`);
	if (require_chaos.applyChaos(res, fixture, defaults.chaos, req.headers, journal, {
		method: req.method ?? "POST",
		path: req.url ?? "/v1/embeddings",
		headers: require_helpers.flattenHeaders(req.headers),
		body: syntheticReq
	}, fixture ? "fixture" : "proxy", defaults.registry, defaults.logger)) return;
	if (fixture) {
		const response = await require_helpers.resolveResponse(fixture, syntheticReq);
		if (require_helpers.isErrorResponse(response)) {
			const status = response.status ?? 500;
			journal.add({
				method: req.method ?? "POST",
				path: req.url ?? "/v1/embeddings",
				headers: require_helpers.flattenHeaders(req.headers),
				body: syntheticReq,
				response: {
					status,
					fixture
				}
			});
			require_sse_writer.writeErrorResponse(res, status, require_helpers.serializeErrorResponse(response), { retryAfter: response.retryAfter });
			return;
		}
		if (require_helpers.isEmbeddingResponse(response)) {
			journal.add({
				method: req.method ?? "POST",
				path: req.url ?? "/v1/embeddings",
				headers: require_helpers.flattenHeaders(req.headers),
				body: syntheticReq,
				response: {
					status: 200,
					fixture
				}
			});
			const body = require_helpers.buildEmbeddingResponse(inputs.map(() => [...response.embedding]), embeddingReq.model);
			res.writeHead(200, { "Content-Type": "application/json" });
			res.end(JSON.stringify(body));
			return;
		}
		journal.add({
			method: req.method ?? "POST",
			path: req.url ?? "/v1/embeddings",
			headers: require_helpers.flattenHeaders(req.headers),
			body: syntheticReq,
			response: {
				status: 500,
				fixture
			}
		});
		require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
			message: "Fixture response did not match any known embedding type (must have embedding or error)",
			type: "server_error"
		} }));
		return;
	}
	if (require_helpers.resolveStrictMode(defaults.strict, req.headers)) {
		const strictMessage = require_helpers.strictNoMatchMessage(skippedBySequenceOrTurn);
		logger.error(require_helpers.strictNoMatchLogLine(req.method ?? "POST", req.url ?? "/v1/embeddings", skippedBySequenceOrTurn));
		journal.add({
			method: req.method ?? "POST",
			path: req.url ?? "/v1/embeddings",
			headers: require_helpers.flattenHeaders(req.headers),
			body: syntheticReq,
			response: {
				status: 503,
				fixture: null,
				...require_helpers.strictOverrideField(defaults.strict, req.headers)
			}
		});
		require_sse_writer.writeErrorResponse(res, 503, JSON.stringify({ error: {
			message: strictMessage,
			type: "invalid_request_error",
			code: "no_fixture_match"
		} }));
		return;
	}
	if (defaults.record) {
		const outcome = await require_recorder.proxyAndRecord(req, res, syntheticReq, providerKey, req.url ?? "/v1/embeddings", fixtures, defaults, raw);
		if (outcome === "handled_by_hook") return;
		if (outcome !== "not_configured") {
			journal.add({
				method: req.method ?? "POST",
				path: req.url ?? "/v1/embeddings",
				headers: require_helpers.flattenHeaders(req.headers),
				body: syntheticReq,
				response: {
					status: res.statusCode ?? 200,
					fixture: null,
					source: "proxy"
				}
			});
			return;
		}
	}
	logger.warn(`No embedding fixture matched for "${combinedInput.slice(0, 80)}" — returning deterministic fallback`);
	const dimensions = embeddingReq.dimensions ?? 1536;
	const embeddings = inputs.map((input) => require_helpers.generateDeterministicEmbedding(input, dimensions));
	journal.add({
		method: req.method ?? "POST",
		path: req.url ?? "/v1/embeddings",
		headers: require_helpers.flattenHeaders(req.headers),
		body: syntheticReq,
		response: {
			status: 200,
			fixture: null
		}
	});
	const body = require_helpers.buildEmbeddingResponse(embeddings, embeddingReq.model);
	res.writeHead(200, { "Content-Type": "application/json" });
	res.end(JSON.stringify(body));
}

//#endregion
exports.handleEmbeddings = handleEmbeddings;
//# sourceMappingURL=embeddings.cjs.map