import { SHARE_CONFIG_DEFAULTS } from "./share-types.js";
import { z } from "zod";
//#region src/share/share-config.ts
const ShareDirectoryPatchSchema = z.object({
	enabled: z.boolean().optional(),
	maxFolderSize: z.number().int().min(1048576).max(10737418240).optional(),
	maxFileCount: z.number().int().min(1).max(1e5).optional(),
	maxDepth: z.number().int().min(1).max(64).optional(),
	listingCacheMs: z.number().int().min(0).max(6e5).optional(),
	zipConcurrency: z.number().int().min(1).max(8).optional()
});
const ShareThumbnailPatchSchema = z.object({
	enabled: z.boolean().optional(),
	concurrency: z.number().int().min(1).max(8).optional(),
	maxBytes: z.number().int().min(8192).max(4194304).optional(),
	viewportWidth: z.number().int().min(320).max(2560).optional(),
	viewportHeight: z.number().int().min(320).max(2560).optional(),
	generationTimeoutMs: z.number().int().min(1e3).max(6e4).optional(),
	failureCooldownMs: z.number().int().min(0).max(36e5).optional(),
	internalGatewayUrl: z.string().min(1).optional()
});
const ShareConfigPatchSchema = z.object({
	enabled: z.boolean().optional(),
	defaultTtlMs: z.number().int().min(6e4).max(6048e5).optional(),
	maxTtlMs: z.number().int().min(6e4).max(2592e6).optional(),
	maxActiveShares: z.number().int().min(1).max(1e4).optional(),
	maxFileSize: z.number().int().min(1048576).max(10737418240).optional(),
	inlinePreviewMimes: z.array(z.string().min(1)).optional(),
	directory: ShareDirectoryPatchSchema.optional(),
	thumbnail: ShareThumbnailPatchSchema.optional()
});
function resolveDirectoryConfig(raw) {
	const base = SHARE_CONFIG_DEFAULTS.directory;
	if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { ...base };
	const patch = raw;
	return {
		enabled: patch.enabled ?? base.enabled,
		maxFolderSize: patch.maxFolderSize ?? base.maxFolderSize,
		maxFileCount: patch.maxFileCount ?? base.maxFileCount,
		maxDepth: patch.maxDepth ?? base.maxDepth,
		listingCacheMs: patch.listingCacheMs ?? base.listingCacheMs,
		zipConcurrency: patch.zipConcurrency ?? base.zipConcurrency
	};
}
function resolveThumbnailConfig(raw) {
	const base = SHARE_CONFIG_DEFAULTS.thumbnail;
	if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { ...base };
	const patch = raw;
	return {
		enabled: patch.enabled ?? base.enabled,
		concurrency: patch.concurrency ?? base.concurrency,
		maxBytes: patch.maxBytes ?? base.maxBytes,
		viewportWidth: patch.viewportWidth ?? base.viewportWidth,
		viewportHeight: patch.viewportHeight ?? base.viewportHeight,
		generationTimeoutMs: patch.generationTimeoutMs ?? base.generationTimeoutMs,
		failureCooldownMs: patch.failureCooldownMs ?? base.failureCooldownMs,
		internalGatewayUrl: patch.internalGatewayUrl ?? base.internalGatewayUrl
	};
}
function resolveShareConfig(raw) {
	const patch = raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
	return {
		enabled: patch.enabled ?? SHARE_CONFIG_DEFAULTS.enabled,
		defaultTtlMs: patch.defaultTtlMs ?? SHARE_CONFIG_DEFAULTS.defaultTtlMs,
		maxTtlMs: patch.maxTtlMs ?? SHARE_CONFIG_DEFAULTS.maxTtlMs,
		maxActiveShares: patch.maxActiveShares ?? SHARE_CONFIG_DEFAULTS.maxActiveShares,
		maxFileSize: patch.maxFileSize ?? SHARE_CONFIG_DEFAULTS.maxFileSize,
		inlinePreviewMimes: Array.isArray(patch.inlinePreviewMimes) ? patch.inlinePreviewMimes.filter((x) => typeof x === "string" && x.trim().length > 0) : [...SHARE_CONFIG_DEFAULTS.inlinePreviewMimes],
		directory: resolveDirectoryConfig(patch.directory),
		thumbnail: resolveThumbnailConfig(patch.thumbnail)
	};
}
function mergeShareConfigPatch(config, patch) {
	const parsed = ShareConfigPatchSchema.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 current = resolveShareConfig(config.gateway.share);
	const next = {
		...current,
		...parsed.data,
		inlinePreviewMimes: parsed.data.inlinePreviewMimes ? parsed.data.inlinePreviewMimes.map((x) => x.trim()).filter(Boolean) : current.inlinePreviewMimes,
		directory: parsed.data.directory ? {
			...current.directory,
			...parsed.data.directory
		} : current.directory,
		thumbnail: parsed.data.thumbnail ? {
			...current.thumbnail,
			...parsed.data.thumbnail
		} : current.thumbnail
	};
	if (next.defaultTtlMs > next.maxTtlMs) return {
		ok: false,
		message: "gateway.share.defaultTtlMs must not exceed gateway.share.maxTtlMs"
	};
	config.gateway.share = next;
	return { ok: true };
}
//#endregion
export { mergeShareConfigPatch, resolveShareConfig };

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