import { r as isRecord } from "./record-coerce-DVQGe6I0.js";
import { n as defineStrayPluginEntryConfigMigration } from "./runtime-doctor-migrations-M02m0S_a.js";
import { _ as unknown, d as object, i as boolean, m as string, s as email, t as _enum, u as number } from "./schemas-DhCQ9c0o.js";
import "./string-coerce-runtime-8A3QK7DM.js";
new TextDecoder("utf-8", { fatal: true });
//#endregion
//#region extensions/reef/protocol/guard.ts
const GUARD_RULES_MAX_CHARS = 2e3;
//#endregion
//#region extensions/reef/src/config-schema.ts
const HandleSchema = string().regex(/^[a-z0-9][a-z0-9_-]{0,62}$/);
const GuardRuleTextSchema = string().max(GUARD_RULES_MAX_CHARS).regex(/\S/, "rules text must not be blank");
const RelayUrlSchema = string().regex(/^[hH][tT][tT][pP][sS]?:\/\/[^\\/?#@]+\/?$/, "Reef relay URL must be an HTTP(S) origin without credentials, path, query, or hash").url();
const ReefChannelConfigSchema = object({
	enabled: boolean().default(true),
	configWrites: boolean().optional(),
	relayUrl: RelayUrlSchema.default("https://reefwire.ai"),
	handle: HandleSchema.optional(),
	email: email().optional(),
	guard: object({
		provider: _enum(["anthropic", "openai"]),
		pinnedModel: string().min(1),
		apiKeyEnv: string().regex(/^[A-Z_][A-Z0-9_]*$/),
		policyVersion: string().min(1),
		timeoutMs: number().int().min(100).max(12e4),
		rules: object({
			outbound: GuardRuleTextSchema.optional(),
			inbound: GuardRuleTextSchema.optional()
		}).strict().optional()
	}).strict().optional(),
	stateDir: string().min(1).optional(),
	requestPolicy: _enum([
		"code-only",
		"friends-of-friends",
		"open"
	]).default("code-only"),
	friends: unknown().optional()
}).strict();
//#endregion
//#region extensions/reef/config-doctor-api.ts
function hasRetiredReefPolicyConfig(value) {
	return isRecord(value) && ["dmPolicy", "allowFrom"].some((key) => Object.hasOwn(value, key));
}
const reefStrayEntryConfigMigration = defineStrayPluginEntryConfigMigration({
	pluginId: "reef",
	channelId: "reef",
	validateMergedChannelConfig: (merged) => ReefChannelConfigSchema.safeParse(merged).success
});
const legacyConfigRules = [{
	path: ["channels", "reef"],
	message: "channels.reef dmPolicy/allowFrom are legacy; run \"openclaw doctor --fix\" to remove them. Peer trust is SQLite-backed.",
	match: hasRetiredReefPolicyConfig
}, reefStrayEntryConfigMigration.legacyConfigRule];
function normalizeCompatibilityConfig({ cfg }) {
	const reef = cfg.channels?.reef;
	let config = cfg;
	const changes = [];
	if (isRecord(reef) && hasRetiredReefPolicyConfig(reef)) {
		const next = structuredClone(cfg);
		const nextReef = next.channels?.reef;
		if (isRecord(nextReef)) {
			for (const key of ["dmPolicy", "allowFrom"]) if (Object.hasOwn(nextReef, key)) {
				delete nextReef[key];
				changes.push(`Removed retired Reef ${key} field.`);
			}
			config = next;
		}
	}
	const stray = reefStrayEntryConfigMigration.normalizeConfig({ cfg: config });
	return {
		config: stray.config,
		changes: [...changes, ...stray.changes]
	};
}
//#endregion
export { legacyConfigRules, normalizeCompatibilityConfig };
