import { getCredentialVault } from "../services/credential-vault.mjs";
import { getUserMode, setSafetyMode, setSigningMode } from "../services/mode-service.mjs";
//#region extensions/crypto/src/commands/mode-commands.ts
/**
* Mode commands — toggle safety and signing modes.
*
* /safemode    — Enable intent confirmation (default)
* /dangermode  — Disable intent confirmation (agent acts immediately)
* /walletsign  — Use WalletConnect for signing (default)
* /autosign    — Use private key for auto-signing (requires CLAWNCHER_PRIVATE_KEY)
* /mode        — Show current mode status
*/
function getSenderId(ctx) {
	return ctx?.senderId ?? ctx?.from ?? ctx?.metadata?.senderId ?? "unknown";
}
const safemodeCommand = {
	name: "safemode",
	description: "Enable intent confirmation — agent confirms before taking any action",
	acceptsArgs: false,
	requireAuth: true,
	handler: async (ctx) => {
		return { text: `Safe mode enabled.

The agent will confirm its intent before executing any action. You'll see a summary of what it plans to do and can approve or reject.

Current settings:
  Intent confirmation: ON
  Signing: ${setSafetyMode(getSenderId(ctx), "safe").signingMode === "wallet" ? "WalletConnect (phone approval)" : "Auto-sign (private key)"}` };
	}
};
const dangermodeCommand = {
	name: "dangermode",
	description: "Disable intent confirmation — agent acts immediately on your requests",
	acceptsArgs: false,
	requireAuth: true,
	handler: async (ctx) => {
		const mode = setSafetyMode(getSenderId(ctx), "danger");
		const isDualDanger = mode.signingMode === "autosign";
		const signingNote = mode.signingMode === "wallet" ? "Transactions still require wallet approval on your phone." : "CRITICAL WARNING: With auto-sign enabled, transactions execute without ANY confirmation. A safety cap of 0.1 ETH per transaction applies in this mode. Consider using /walletsign for larger amounts.";
		return { text: `${isDualDanger ? "⚠️ MAXIMUM RISK MODE ⚠️\n\n" : ""}Danger mode enabled.

The agent will act immediately on your requests without confirming intent first.

${signingNote}

Current settings:
  Intent confirmation: OFF
  Signing: ${mode.signingMode === "wallet" ? "WalletConnect (phone approval)" : "Auto-sign (private key)"}

Use /safemode to re-enable confirmations.` };
	}
};
const walletsignCommand = {
	name: "walletsign",
	description: "Use WalletConnect for transaction signing (phone approval required)",
	acceptsArgs: false,
	requireAuth: true,
	handler: async (ctx) => {
		return { text: `Wallet signing enabled.

All transactions will be sent to your phone wallet for approval. You always have the final say.

Current settings:
  Intent confirmation: ${setSigningMode(getSenderId(ctx), "wallet").safetyMode === "safe" ? "ON" : "OFF"}
  Signing: WalletConnect (phone approval)` };
	}
};
const autosignCommand = {
	name: "autosign",
	description: "Enable auto-signing with private key (no wallet approval needed)",
	acceptsArgs: false,
	requireAuth: true,
	handler: async (ctx) => {
		const userId = getSenderId(ctx);
		if (!getCredentialVault().getSecret("wallet.privateKey", "mode-commands")) return { text: `Auto-sign is not available.

A private key (CLAWNCHER_PRIVATE_KEY) must be configured at deploy time to enable auto-signing. This instance uses WalletConnect only.

To enable auto-sign, set the CLAWNCHER_PRIVATE_KEY environment variable:
  Fly.io: \`fly secrets set CLAWNCHER_PRIVATE_KEY="0x..." -a <your-app>\`
  Docker: add to your \`.env\` file

WARNING: Auto-sign means the agent can execute transactions without your approval. Only use with a dedicated hot wallet containing limited funds.` };
		return { text: `WARNING: Auto-sign enabled.

Transactions will be signed automatically using the configured private key. NO wallet approval will be requested.

Only use this with a dedicated hot wallet containing limited funds.

Current settings:
  Intent confirmation: ${setSigningMode(userId, "autosign").safetyMode === "safe" ? "ON" : "OFF"}
  Signing: Auto-sign (private key)

Use /walletsign to switch back to phone approval.` };
	}
};
const readonlyCommand = {
	name: "readonly",
	description: "Enable read-only mode — no on-chain writes allowed (view portfolio, prices, analytics only)",
	acceptsArgs: false,
	requireAuth: true,
	handler: async (ctx) => {
		return { text: `Read-only mode enabled.

All on-chain write operations are BLOCKED: swaps, transfers, token launches, approvals, bridging, etc.

You can still:
  - Check prices (/portfolio, defi_price)
  - View balances (defi_balance)
  - Run analytics (analytics, market_intel)
  - View cost basis and trade history

Use /safemode or /dangermode to re-enable write operations.

Current settings:
  Mode: READ-ONLY
  Signing: ${setSafetyMode(getSenderId(ctx), "readonly").signingMode === "wallet" ? "WalletConnect (phone approval)" : "Auto-sign (private key)"}` };
	}
};
const modeCommand = {
	name: "mode",
	description: "Show current safety and signing mode",
	acceptsArgs: false,
	requireAuth: true,
	handler: async (ctx) => {
		const mode = getUserMode(getSenderId(ctx));
		const hasPrivateKey = !!getCredentialVault().getSecret("wallet.privateKey", "mode-commands");
		return { text: `Current mode:

  Intent confirmation: ${mode.safetyMode === "readonly" ? "READ-ONLY (/readonly)" : mode.safetyMode === "safe" ? "ON (/safemode)" : "OFF (/dangermode)"}
  Signing: ${mode.signingMode === "wallet" ? "WalletConnect (/walletsign)" : "Auto-sign (/autosign)"}
  Private key available: ${hasPrivateKey ? "Yes" : "No"}

Commands:
  /safemode    — Confirm before acting
  /dangermode  — Act immediately
  /readonly    — View only, no on-chain writes
  /walletsign  — Phone approval for transactions
  /autosign    — Auto-sign (requires private key)` };
	}
};
//#endregion
export { autosignCommand, dangermodeCommand, modeCommand, readonlyCommand, safemodeCommand, walletsignCommand };

//# sourceMappingURL=mode-commands.mjs.map