import { archivePaseoWorktree, killTerminalsUnderPath } from "../paseo-worktree-archive-service.js";
import { isSameOrDescendantPath } from "../path-utils.js";
import { isPaseoOwnedWorktreeCwd } from "../../utils/worktree.js";
const defaultDependencies = {
    archivePaseoWorktree,
    isPaseoOwnedWorktreeCwd,
    killTerminalsUnderPath,
    isPathWithinRoot: isSameOrDescendantPath,
};
export async function archiveIfSafe(input) {
    const { cwd, pullRequest, inFlight, options, log } = input;
    const deps = input.deps ?? defaultDependencies;
    if (!pullRequest?.isMerged) {
        return;
    }
    if (options.daemonConfigStore.get().autoArchiveAfterMerge !== true) {
        return;
    }
    if (inFlight.has(cwd)) {
        return;
    }
    inFlight.add(cwd);
    try {
        let snapshot;
        try {
            snapshot = await options.workspaceGitService.getSnapshot(cwd, {
                reason: "auto-archive-on-merge",
            });
        }
        catch (error) {
            log.warn({ err: error, cwd }, "Failed to read snapshot for auto-archive; skipping");
            return;
        }
        if (!snapshot) {
            return;
        }
        if (snapshot.git.isDirty === true || snapshot.git.aheadOfOrigin === null) {
            return;
        }
        if (snapshot.git.aheadOfOrigin > 0) {
            return;
        }
        const ownership = await deps.isPaseoOwnedWorktreeCwd(cwd, {
            paseoHome: options.paseoHome,
            worktreesRoot: options.worktreesRoot,
        });
        if (!ownership.allowed) {
            return;
        }
        try {
            await deps.archivePaseoWorktree({
                paseoHome: options.paseoHome,
                worktreesRoot: options.worktreesRoot,
                github: options.github,
                workspaceGitService: options.workspaceGitService,
                agentManager: options.agentManager,
                agentStorage: options.agentStorage,
                archiveWorkspaceRecord: options.archiveWorkspaceRecord,
                emitWorkspaceUpdatesForWorkspaceIds: options.emitWorkspaceUpdatesForWorkspaceIds,
                markWorkspaceArchiving: options.markWorkspaceArchiving,
                clearWorkspaceArchiving: options.clearWorkspaceArchiving,
                isPathWithinRoot: deps.isPathWithinRoot,
                killTerminalsUnderPath: (rootPath) => deps.killTerminalsUnderPath({
                    terminalManager: options.terminalManager,
                    isPathWithinRoot: deps.isPathWithinRoot,
                    killTrackedTerminal: () => { },
                    sessionLogger: log,
                }, rootPath),
                sessionLogger: log,
            }, {
                targetPath: cwd,
                repoRoot: ownership.repoRoot ?? null,
                worktreesRoot: ownership.worktreeRoot,
                worktreesBaseRoot: options.worktreesRoot,
                requestId: "auto-archive-on-merge",
            });
            log.info({ cwd }, "Auto-archived worktree after PR merge");
        }
        catch (error) {
            log.warn({ err: error, cwd }, "Auto-archive after merge failed");
        }
    }
    finally {
        inFlight.delete(cwd);
    }
}
//# sourceMappingURL=archive-if-safe.js.map