import { r as __require } from "../../chunk-BEJ448es.mjs";
import { resetModes } from "../services/mode-service.mjs";
import { resetOnboardingFlows } from "../services/onboarding-flow.mjs";
import { existsSync, readdirSync, rmSync } from "node:fs";
import { join } from "node:path";
//#region extensions/crypto/src/commands/reset-command.ts
/**
* /reset command — wipe onboarding state, sessions, WC pairing, and credentials.
* Requires double confirmation: /reset → /reset confirm
*
* After reset, the next message from any user triggers pairing + onboarding again.
*/
/** Track pending confirmations by sender (expire after 60s). */
const pendingConfirmations = /* @__PURE__ */ new Map();
/** Resolve persistent state dir (volume mount). Checks both old and new names. */
function getStateRoot() {
	return "/workspace/.openclaw-state";
}
/** Resolve OpenClaw/OpenClawnch home state dir. */
function getOpenClawHome() {
	const base = process.env.HOME ?? "/root";
	const newPath = join(base, ".openclawnch");
	const oldPath = join(base, ".openclaw");
	try {
		const { existsSync } = __require("node:fs");
		if (existsSync(newPath)) return newPath;
	} catch {}
	return oldPath;
}
const resetCommand = {
	name: "factoryreset",
	description: "Factory reset: wipe all state. Tap /factoryreset_confirm after.",
	acceptsArgs: true,
	requireAuth: true,
	handler: async (ctx) => {
		const senderId = ctx?.senderId ?? ctx?.from ?? "unknown";
		if ((ctx?.args ?? ctx?.commandBody ?? "").trim().toLowerCase() === "confirm") {
			const pending = pendingConfirmations.get(senderId);
			if (!pending || Date.now() - pending > 6e4) return { text: "No pending reset. Run /factoryreset first, then /factoryreset_confirm within 60 seconds." };
			pendingConfirmations.delete(senderId);
			const wiped = [];
			const errors = [];
			const stateRoot = getStateRoot();
			const openclawHome = getOpenClawHome();
			const onboardingDir = join(stateRoot, "onboarding");
			if (existsSync(onboardingDir)) try {
				rmSync(onboardingDir, {
					recursive: true,
					force: true
				});
				wiped.push("onboarding state");
			} catch (e) {
				errors.push(`onboarding: ${e instanceof Error ? e.message : String(e)}`);
			}
			const wcDir = join(stateRoot, "wc");
			if (existsSync(wcDir)) try {
				rmSync(wcDir, {
					recursive: true,
					force: true
				});
				wiped.push("WalletConnect session");
			} catch (e) {
				errors.push(`wc: ${e instanceof Error ? e.message : String(e)}`);
			}
			const sessionsDir = join(stateRoot, "sessions");
			if (existsSync(sessionsDir)) try {
				for (const f of readdirSync(sessionsDir)) rmSync(join(sessionsDir, f), {
					recursive: true,
					force: true
				});
				wiped.push("conversation sessions");
			} catch (e) {
				errors.push(`sessions: ${e instanceof Error ? e.message : String(e)}`);
			}
			const credsDir = join(stateRoot, "credentials");
			if (existsSync(credsDir)) try {
				for (const f of readdirSync(credsDir)) rmSync(join(credsDir, f), { force: true });
				wiped.push("credentials/pairing");
			} catch (e) {
				errors.push(`credentials: ${e instanceof Error ? e.message : String(e)}`);
			}
			for (const sub of ["credentials", "agents/main/sessions"]) {
				const dir = join(openclawHome, sub);
				if (existsSync(dir)) try {
					for (const f of readdirSync(dir)) rmSync(join(dir, f), {
						recursive: true,
						force: true
					});
				} catch {}
			}
			resetOnboardingFlows();
			resetModes();
			const modesDir = join(stateRoot, "modes");
			if (existsSync(modesDir)) try {
				rmSync(modesDir, {
					recursive: true,
					force: true
				});
				wiped.push("mode preferences");
			} catch (e) {
				errors.push(`modes: ${e instanceof Error ? e.message : String(e)}`);
			}
			const txDir = join(stateRoot, "tx");
			if (existsSync(txDir)) try {
				rmSync(txDir, {
					recursive: true,
					force: true
				});
				wiped.push("transaction history");
			} catch (e) {
				errors.push(`tx: ${e instanceof Error ? e.message : String(e)}`);
			}
			let msg = `Factory reset complete.\n\nWiped: ${wiped.length > 0 ? wiped.join(", ") : "nothing found"}`;
			if (errors.length > 0) msg += `\n\nErrors:\n${errors.map((e) => `  - ${e}`).join("\n")}`;
			msg += "\n\nYour next message will require re-pairing. Send any message to start fresh.";
			return { text: msg };
		}
		pendingConfirmations.set(senderId, Date.now());
		return { text: `WARNING: This will permanently delete:

  - Onboarding preferences (persona, capabilities)
  - Conversation history (all sessions)
  - WalletConnect wallet pairing
  - Sender credentials (you'll need to re-pair)
  - Transaction history

This cannot be undone.

To confirm, tap /factoryreset_confirm within 60 seconds.` };
	}
};
/** Tappable alias for /factoryreset confirm. */
const resetConfirmCommand = {
	name: "factoryreset_confirm",
	description: "Confirm a pending factory reset",
	acceptsArgs: false,
	requireAuth: true,
	handler: async (ctx) => {
		return resetCommand.handler({
			...ctx,
			args: "confirm",
			commandBody: "confirm"
		});
	}
};
//#endregion
export { resetCommand, resetConfirmCommand };

//# sourceMappingURL=reset-command.mjs.map