import { v as resolveStateDir } from "./paths-CmLVBCTa.js";
import * as fs$1 from "node:fs";
import * as path$1 from "node:path";
//#region src/gateway/server-startup-legacy-workspaces-migration.ts
async function defaultRunSessionsCli(args) {
	return (await import("./migrate-legacy-sessions-DtBwYg5m.js")).runCli(args);
}
async function defaultRunSkillsCli(args) {
	return (await import("./migrate-legacy-skills-V5CgYHUX.js")).runCli(args);
}
const LEGACY_WORKSPACES_MIGRATION_SENTINEL_FILENAME = ".legacy-workspaces-migrated.json";
async function runStartupLegacyWorkspacesMigration(params) {
	const env = params.env ?? process.env;
	const stateDir = (params.deps?.resolveStateDir ?? resolveStateDir)(env);
	const sentinelPath = path$1.join(stateDir, LEGACY_WORKSPACES_MIGRATION_SENTINEL_FILENAME);
	if (fs$1.existsSync(sentinelPath)) {
		params.log.debug(`gateway: legacy workspaces migration sentinel present at ${sentinelPath}; skipping`);
		return;
	}
	const workspacesDir = path$1.join(stateDir, "workspaces");
	const now = params.deps?.now ?? Date.now;
	if (!fs$1.existsSync(workspacesDir)) {
		safeWriteSentinel(sentinelPath, {
			version: 1,
			completedAt: now(),
			workspacesDir,
			sessions: { status: "nothing-to-do" },
			skills: { status: "nothing-to-do" }
		}, params.log);
		params.log.debug(`gateway: no legacy workspaces at ${workspacesDir}; recorded nothing-to-do sentinel`);
		return;
	}
	const runSessionsCli = params.deps?.runSessionsCli ?? defaultRunSessionsCli;
	const runSkillsCli = params.deps?.runSkillsCli ?? defaultRunSkillsCli;
	const scriptEnv = {
		...env,
		OPENCLAW_STATE_DIR: stateDir
	};
	const sessions = await invokeScript(runSessionsCli, scriptEnv, params.log, "sessions");
	const skills = await invokeScript(runSkillsCli, scriptEnv, params.log, "skills");
	safeWriteSentinel(sentinelPath, {
		version: 1,
		completedAt: now(),
		workspacesDir,
		sessions,
		skills
	}, params.log);
	params.log.info(`gateway: legacy workspaces migrated: sessions=${sessions.status}, skills=${skills.status}`);
}
async function invokeScript(runCli, env, log, name) {
	try {
		const exitCode = await runCli({
			argv: [],
			env
		});
		if (exitCode === 0) return {
			status: "success",
			exitCode: 0
		};
		log.warn(`gateway: legacy ${name} migration script exited with code ${exitCode}`);
		return {
			status: "failed",
			exitCode,
			error: `exit code ${exitCode}`
		};
	} catch (err) {
		log.warn(`gateway: legacy ${name} migration script threw: ${String(err)}`);
		return {
			status: "failed",
			error: String(err)
		};
	}
}
function safeWriteSentinel(sentinelPath, payload, log) {
	try {
		writeSentinel(sentinelPath, payload);
	} catch (err) {
		log.error(`gateway: failed to write legacy workspaces migration sentinel at ${sentinelPath}: ${String(err)}`);
	}
}
function writeSentinel(sentinelPath, payload) {
	fs$1.mkdirSync(path$1.dirname(sentinelPath), { recursive: true });
	const tmpPath = `${sentinelPath}.tmp`;
	fs$1.writeFileSync(tmpPath, JSON.stringify(payload, null, 2));
	fs$1.renameSync(tmpPath, sentinelPath);
}
//#endregion
export { runStartupLegacyWorkspacesMigration };
