import { g as getActiveEndpoint, l as listEndpoints, v as validateEndpointUrl, a as addEndpoint } from './endpoints-UdQGgKsR.js';
import { d as getRequestId, r as requireAdmin, k as jsonResponse, w as withAdminBody, e as errorResponse } from './helpers-kzwksNq1.js';
import './state-fxN45gBp.js';
import './src-CidJbygh.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
import './provider-constants-DPrj3yBX.js';
import './dist-DOFcG0Zp.js';
import 'node:fs';
import 'node:path';
import 'buffer';
import 'fs';
import 'path';
import 'node:child_process';
import 'node:url';
import 'node:crypto';
import 'events';
import 'node:events';
import 'node:stream';
import 'node:string_decoder';
import 'assert';
import 'zlib';
import 'node:assert';
import 'node:fs/promises';
import 'node:os';

//#region src/routes/admin/endpoints/+server.ts
function publish(e) {
	return {
		id: e.id,
		label: e.label,
		url: e.url,
		isDefault: e.isDefault,
		hasPassword: Boolean(e.password)
	};
}
var GET = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	return jsonResponse(200, {
		endpoints: listEndpoints().map(publish),
		activeId: publish(getActiveEndpoint()).id
	}, requestId);
};
var POST = async (event) => withAdminBody(event, async ({ requestId, body }) => {
	const label = typeof body.label === "string" ? body.label : "";
	const url = typeof body.url === "string" ? body.url : "";
	const password = typeof body.password === "string" && body.password.length > 0 ? body.password : void 0;
	const urlCheck = validateEndpointUrl(url);
	if (!urlCheck.ok) {
		if (urlCheck.reason === "http_not_allowed") return errorResponse(400, "http_not_allowed", "Plain HTTP is only allowed for loopback addresses. Use https:// for remote OpenPalm instances.", {}, requestId);
		return errorResponse(400, "invalid_endpoint", "URL must be a valid http(s) URL", {}, requestId);
	}
	try {
		return jsonResponse(201, { endpoint: publish({
			...addEndpoint({
				label,
				url: urlCheck.url,
				password
			}),
			isDefault: false
		}) }, requestId);
	} catch (e) {
		return errorResponse(400, "invalid_endpoint", e instanceof Error ? e.message : "failed to create endpoint", {}, requestId);
	}
});

export { GET, POST };
//# sourceMappingURL=_server.ts-D8kr1zcQ.js.map
