import { c as asString } from './coercion-TNFJisCC.js';

//#region src/routes/admin/providers/_helpers.ts
/**
* Shared coercion + parsing helpers for the per-action provider routes.
*
* Each handler under `packages/ui/src/routes/admin/providers/<action>/`
* imports from this file to keep the per-route +server.ts files focused on
* their own request shape and response.
*
* Coercion primitives (asString, asRecord) live in `$lib/server/coercion.ts`.
* The per-route helpers here trim string inputs and fall back to `''` because
* the form-encoded provider payloads always treat missing fields as empty
* strings before validation.
*/
/** Coerce `unknown` to a trimmed string, returning `''` when missing/non-string. */
function asStringOrEmpty(value) {
	return (asString(value) ?? "").trim();
}
function updateNumberOption(target, key, value) {
	if (!value) {
		delete target[key];
		return;
	}
	const parsed = Number(value);
	if (!Number.isNaN(parsed)) target[key] = parsed;
	else delete target[key];
}
function updateBooleanOption(target, key, value) {
	if (value) target[key] = true;
	else delete target[key];
}
function extractInputs(body) {
	const inputs = {};
	for (const [key, value] of Object.entries(body)) {
		if (!key.startsWith("inputs[") || !key.endsWith("]") || typeof value !== "string") continue;
		const inputKey = key.slice(7, -1).trim();
		if (!inputKey || value.trim().length === 0) continue;
		inputs[inputKey] = value.trim();
	}
	return inputs;
}
function parseModels(modelsJson) {
	if (!modelsJson) return [];
	return JSON.parse(modelsJson).filter((m) => typeof m.id === "string" && m.id.trim().length > 0).map((m) => ({
		id: m.id.trim(),
		name: typeof m.name === "string" ? m.name.trim() : "",
		contextLimit: parseLimit(m.contextLimit),
		outputLimit: parseLimit(m.outputLimit)
	}));
}
function buildModelConfig(model) {
	const limit = {
		...model.contextLimit ? { context: model.contextLimit } : {},
		...model.outputLimit ? { output: model.outputLimit } : {}
	};
	return {
		...model.name ? { name: model.name } : {},
		...Object.keys(limit).length > 0 ? { limit } : {}
	};
}
function parseLimit(value) {
	if (typeof value !== "number" && typeof value !== "string") return void 0;
	const parsed = Number(value);
	return Number.isInteger(parsed) && parsed > 0 ? parsed : void 0;
}
function parseHeaders(headersJson) {
	if (!headersJson) return {};
	const parsed = JSON.parse(headersJson);
	return Object.fromEntries(parsed.filter((h) => typeof h.key === "string" && typeof h.value === "string").map((h) => [h.key.trim(), h.value.trim()]).filter((e) => e[0].length > 0 && e[1].length > 0));
}

export { asStringOrEmpty as a, buildModelConfig as b, parseModels as c, updateNumberOption as d, extractInputs as e, parseHeaders as p, updateBooleanOption as u };
//# sourceMappingURL=_helpers-B_lb4-jB.js.map
