import "./policy-types.mjs";
import { getPolicyStore } from "./policy-store.mjs";
import { randomUUID } from "node:crypto";
//#region extensions/crypto/src/services/autonomy-profiles.ts
const PROFILES = {
	supervised: {
		id: "supervised",
		name: "Supervised",
		description: "All actions require wallet approval via ClawnchConnect.",
		summary: [
			"Every transaction goes to your wallet for manual approval.",
			"No spending limits — you review each action.",
			"No on-chain delegation created.",
			"This is the safest mode. The agent cannot act without you."
		],
		rules: [],
		scopeCategories: [],
		delegation: {
			expirySec: 0,
			maxTxUsd: 0
		}
	},
	training: {
		id: "training",
		name: "Training Wheels",
		description: "Small limits, short expiry. Good for getting started.",
		summary: [
			"Max $50 per transaction.",
			"Max $200/day total spending.",
			"Max 10 write actions per day.",
			"24-hour expiry — delegation auto-expires.",
			"Only DeFi and transfer tools allowed."
		],
		rules: [
			{
				type: "max_amount",
				maxAmountUsd: 50
			},
			{
				type: "spending_limit",
				maxAmountUsd: 200,
				period: "daily"
			},
			{
				type: "rate_limit",
				maxCalls: 10,
				periodMs: 864e5
			}
		],
		scopeCategories: ["defi", "transfer"],
		delegation: {
			expirySec: 86400,
			maxTxUsd: 50
		}
	},
	autonomous: {
		id: "autonomous",
		name: "Autonomous",
		description: "Production-level autonomy with weekly budgets.",
		summary: [
			"Max $500 per transaction.",
			"Max $2,000/week total spending.",
			"Max 50 write actions per day.",
			"30-day expiry — renew monthly.",
			"DeFi, transfers, and orchestration tools allowed.",
			"Confirmation required above $500."
		],
		rules: [
			{
				type: "max_amount",
				maxAmountUsd: 500
			},
			{
				type: "spending_limit",
				maxAmountUsd: 2e3,
				period: "weekly"
			},
			{
				type: "rate_limit",
				maxCalls: 50,
				periodMs: 864e5
			},
			{
				type: "approval_threshold",
				amountUsd: 500
			}
		],
		scopeCategories: [
			"defi",
			"transfer",
			"orchestration"
		],
		delegation: {
			expirySec: 2592e3,
			maxTxUsd: 500
		}
	},
	custom: {
		id: "custom",
		name: "Custom",
		description: "Define your own rules using natural language policies.",
		summary: [
			"No preset rules — you define everything.",
			"Use `/policies` to create and manage rules.",
			"Use `/delegate create <name>` to compile to on-chain delegation."
		],
		rules: [],
		scopeCategories: [],
		delegation: {
			expirySec: 0,
			maxTxUsd: 0
		}
	}
};
/** Prefix for policies created by profile activation. */
const PROFILE_POLICY_PREFIX = "profile:";
/** In-memory cache of active profile per user. */
const _activeProfiles = /* @__PURE__ */ new Map();
/** Get the list of all available profiles. */
function listProfiles() {
	return Object.values(PROFILES);
}
/** Get a profile by ID. */
function getProfile(id) {
	return PROFILES[id];
}
/** Get the active profile for a user (checks existing policies). */
function getActiveProfile(userId) {
	const cached = _activeProfiles.get(userId);
	if (cached) return cached;
	const policies = getPolicyStore().listPolicies(userId);
	const profilePolicy = policies.find((p) => p.name.startsWith(PROFILE_POLICY_PREFIX));
	if (profilePolicy) {
		const id = profilePolicy.name.replace(PROFILE_POLICY_PREFIX, "");
		if (PROFILES[id]) {
			_activeProfiles.set(userId, id);
			return id;
		}
	}
	if (policies.length > 0) {
		_activeProfiles.set(userId, "custom");
		return "custom";
	}
	_activeProfiles.set(userId, "supervised");
	return "supervised";
}
/**
* Activate a profile for a user.
*
* - Removes any existing profile-generated policies.
* - Creates new policies from the profile template.
* - Does NOT compile to on-chain delegation (use /delegate create after).
* - Returns the created policies (empty for supervised/custom).
*/
function activateProfile(userId, profileId) {
	const profile = PROFILES[profileId];
	if (!profile) throw new Error(`Unknown profile: ${profileId}`);
	const store = getPolicyStore();
	const existing = store.listPolicies(userId);
	for (const p of existing) if (p.name.startsWith(PROFILE_POLICY_PREFIX)) store.deletePolicy(userId, p.id);
	const created = [];
	if (profile.rules.length > 0) {
		const now = Date.now();
		const hasCategories = profile.scopeCategories.length > 0;
		const scope = {
			type: hasCategories ? "categories" : "all_write",
			tools: void 0,
			categories: hasCategories ? profile.scopeCategories : void 0
		};
		const policy = {
			id: randomUUID(),
			name: `${PROFILE_POLICY_PREFIX}${profileId}`,
			description: `${profile.name} profile — ${profile.description}`,
			rules: profile.rules,
			scope,
			status: "active",
			confirmedAt: now,
			createdAt: now,
			updatedAt: now,
			userId
		};
		store.savePolicy(policy);
		created.push(policy);
	}
	_activeProfiles.set(userId, profileId);
	return {
		profile,
		policies: created
	};
}
/**
* Deactivate a profile — removes profile-generated policies.
* Reverts to supervised.
*/
function deactivateProfile(userId) {
	const store = getPolicyStore();
	const existing = store.listPolicies(userId);
	for (const p of existing) if (p.name.startsWith(PROFILE_POLICY_PREFIX)) store.deletePolicy(userId, p.id);
	_activeProfiles.set(userId, "supervised");
}
/** Format a profile for display. */
function formatProfileDisplay(profile, isActive) {
	const lines = [];
	const marker = isActive ? " (active)" : "";
	lines.push(`**${profile.name}**${marker} — \`/profile ${profile.id}\``);
	for (const s of profile.summary) lines.push(`  ${s}`);
	return lines.join("\n");
}
/** Reset profile cache (for testing). */
function resetProfileCache() {
	_activeProfiles.clear();
}
//#endregion
export { activateProfile, deactivateProfile, formatProfileDisplay, getActiveProfile, getProfile, listProfiles, resetProfileCache };

//# sourceMappingURL=autonomy-profiles.mjs.map