import { SITE_SHARE_CONFIG_DEFAULTS } from "./site-share-types.js";
import { z } from "zod";
//#region src/share/site-share-config.ts
const SiteShareStaticPatchSchema = z.object({
	enabled: z.boolean().optional(),
	maxRootDirSize: z.number().int().min(1048576).max(10737418240).optional(),
	maxFileCount: z.number().int().min(1).max(1e5).optional(),
	rewriteEnabledByDefault: z.boolean().optional()
});
const SiteShareProxyPatchSchema = z.object({
	enabled: z.boolean().optional(),
	allowedUpstreamHosts: z.array(z.string().min(1)).optional(),
	allowedUpstreamPorts: z.array(z.number().int().min(1).max(65535)).optional(),
	forwardWebSocket: z.boolean().optional(),
	bodySizeLimit: z.number().int().min(0).max(1073741824).optional(),
	requestTimeoutMs: z.number().int().min(1e3).max(6e5).optional(),
	wsIdleTimeoutMs: z.number().int().min(1e4).max(36e5).optional(),
	rewriteSetCookiePath: z.boolean().optional()
});
const SiteSharePatchSchema = z.object({
	enabled: z.boolean().optional(),
	publicHostSuffix: z.string().min(1).optional(),
	defaultTtlMs: z.number().int().min(6e4).max(6048e5).optional(),
	maxTtlMs: z.number().int().min(6e4).max(2592e6).optional(),
	maxActiveSites: z.number().int().min(1).max(1e3).optional(),
	static: SiteShareStaticPatchSchema.optional(),
	proxy: SiteShareProxyPatchSchema.optional()
});
function resolveSiteShareConfig(service) {
	const raw = service.currentConfig.gateway?.siteShare;
	return mergeWithDefaults(raw);
}
function mergeWithDefaults(raw) {
	const base = SITE_SHARE_CONFIG_DEFAULTS;
	if (!raw || typeof raw !== "object" || Array.isArray(raw)) return JSON.parse(JSON.stringify(base));
	const r = raw;
	return {
		enabled: r.enabled ?? base.enabled,
		publicHostSuffix: r.publicHostSuffix ?? base.publicHostSuffix,
		defaultTtlMs: r.defaultTtlMs ?? base.defaultTtlMs,
		maxTtlMs: r.maxTtlMs ?? base.maxTtlMs,
		maxActiveSites: r.maxActiveSites ?? base.maxActiveSites,
		static: {
			enabled: r.static?.enabled ?? base.static.enabled,
			maxRootDirSize: r.static?.maxRootDirSize ?? base.static.maxRootDirSize,
			maxFileCount: r.static?.maxFileCount ?? base.static.maxFileCount,
			rewriteEnabledByDefault: r.static?.rewriteEnabledByDefault ?? base.static.rewriteEnabledByDefault
		},
		proxy: {
			enabled: r.proxy?.enabled ?? base.proxy.enabled,
			allowedUpstreamHosts: r.proxy?.allowedUpstreamHosts ?? [...base.proxy.allowedUpstreamHosts],
			allowedUpstreamPorts: r.proxy?.allowedUpstreamPorts ?? [...base.proxy.allowedUpstreamPorts],
			forwardWebSocket: r.proxy?.forwardWebSocket ?? base.proxy.forwardWebSocket,
			bodySizeLimit: r.proxy?.bodySizeLimit ?? base.proxy.bodySizeLimit,
			requestTimeoutMs: r.proxy?.requestTimeoutMs ?? base.proxy.requestTimeoutMs,
			wsIdleTimeoutMs: r.proxy?.wsIdleTimeoutMs ?? base.proxy.wsIdleTimeoutMs,
			rewriteSetCookiePath: r.proxy?.rewriteSetCookiePath ?? base.proxy.rewriteSetCookiePath
		}
	};
}
function mergeSiteShareConfigPatch(config, patch) {
	const parsed = SiteSharePatchSchema.safeParse(patch);
	if (!parsed.success) return {
		ok: false,
		message: parsed.error.issues.map((i) => i.message).join("; ")
	};
	if (!config.gateway) config.gateway = {
		bind: "loopback",
		port: 18790,
		auth: { mode: "token" },
		heartbeat: {
			enabled: true,
			intervalMs: 18e5,
			includeSystemPromptSection: false
		},
		maxSseConnections: 100,
		corsOrigins: []
	};
	const cfg = config.gateway;
	const current = mergeWithDefaults(cfg.siteShare);
	const next = {
		...current,
		...parsed.data,
		static: parsed.data.static ? {
			...current.static,
			...parsed.data.static
		} : current.static,
		proxy: parsed.data.proxy ? {
			...current.proxy,
			...parsed.data.proxy
		} : current.proxy
	};
	if (next.defaultTtlMs > next.maxTtlMs) return {
		ok: false,
		message: "siteShare.defaultTtlMs must not exceed siteShare.maxTtlMs"
	};
	cfg.siteShare = next;
	return { ok: true };
}
//#endregion
export { mergeSiteShareConfigPatch, mergeWithDefaults, resolveSiteShareConfig };

//# sourceMappingURL=site-share-config.js.map