import { renderDisplayPath } from "../utils/fs.js";
import { PRODUCT_METADATA } from "./product.js";
export function buildBootstrapNextFileToRead() {
    return ".agent/context/context-tree.json";
}
export function buildBootstrapNextSuggestedCommand(targetRoot) {
    void targetRoot;
    return `${PRODUCT_METADATA.cli.primaryCommand} checkpoint "context seeded"`;
}
export function chooseNextFileToRead(options) {
    return (options.latestTaskPacket ??
        options.latestHandoff ??
        options.latestReconcile ??
        options.latestDispatchManifest ??
        options.latestCheckpoint ??
        options.fallback ??
        buildBootstrapNextFileToRead());
}
export function buildFirstReadContract(options) {
    const authority = uniqueStrings([
        ...(options.authorityOrder ?? []),
        ...(options.promotedAuthorityDocs ?? [])
    ].map((item) => renderDisplayPath(options.targetRoot, item))).filter((item) => !item.startsWith(".agent/context/"));
    return uniqueStrings([
        ...authority,
        ".agent/state/active-role-hints.json",
        ...buildCanonicalReadNext(options)
    ]);
}
export function buildCanonicalReadNext(options) {
    const authority = uniqueStrings([
        ...(options.authorityOrder ?? []),
        ...(options.promotedAuthorityDocs ?? [])
    ].map((item) => renderDisplayPath(options.targetRoot, item))).filter((item) => !item.startsWith(".agent/context/"));
    return uniqueStrings([
        ...authority,
        ".agent/state/current-phase.json",
        ".agent/memory/current-focus.json",
        ".agent/state/checkpoints/latest.json",
        ".agent/state/latest-task-packets.json",
        ".agent/state/handoff/latest.json",
        ".agent/state/reconcile/latest.json",
        ".agent/state/dispatch/latest-manifest.json",
        ".agent/context/commands.md",
        ".agent/context/specialists.md",
        ".agent/context/tool-capabilities.md",
        ".agent/context/mcp-capabilities.md",
        ".agent/context/context-tree.json",
        ".agent/state/context-tree.json",
        ".agent/memory/repo-facts.json",
        ".agent/memory/open-risks.json",
        ...options.contract.instructionSurfaces,
        ".github/agents/shrey-junior.md",
        ...options.contract.agentSurfaces.filter((item) => item !== ".github/agents/shrey-junior.md"),
        ...options.contract.roleSurfaces,
        ".agent/checks.yaml",
        ".agent/scripts/verify-contract.sh",
        ".agent/project.yaml"
    ]);
}
export function buildWriteTargets(contract, extraTargets = []) {
    return uniqueStrings([
        ...extraTargets,
        ".agent/tasks/*",
        ".agent/state/current-phase.json",
        ".agent/state/checkpoints/*",
        ".agent/state/handoff/*",
        ".agent/state/dispatch/*",
        ".agent/state/reconcile/*",
        ".agent/memory/current-focus.json",
        ".agent/memory/open-risks.json",
        ...contract.roleSurfaces
    ]);
}
export function buildChecksToRun(validationSteps = []) {
    return uniqueStrings([
        ".agent/checks.yaml",
        "bash .agent/scripts/verify-contract.sh",
        ...validationSteps,
        `${PRODUCT_METADATA.cli.primaryCommand} push-check when the CLI is available`
    ]);
}
export function buildStopConditions(options) {
    const conditions = [
        "stop when explicit repo authority or promoted canonical docs conflict with the requested action",
        "stop when active phase, handoff, dispatch, or reconcile state is blocked or stale",
        "stop when the change expands beyond the stated task packet scope or latest next step",
        "stop when a stable contract, auth flow, data boundary, or release surface must change without updated checks",
        "stop when required packets, role outputs, or continuity artifacts are missing"
    ];
    if (options?.riskLevel === "medium" || options?.riskLevel === "high") {
        conditions.push("stop when reviewer or tester separation becomes necessary and is not yet captured");
    }
    if (options?.taskType === "release-readiness" || options?.taskType === "migration") {
        conditions.push("stop when release or migration impact is unclear and no explicit validation evidence exists");
    }
    return uniqueStrings(conditions);
}
export function buildSearchGuidance(options) {
    const useExternalLookupWhen = [
        "repo authority or promoted docs explicitly point to external docs, APIs, or live systems",
        "the task depends on current tool behavior, version-sensitive docs, cloud service behavior, or browser/runtime behavior",
        "repo-local artifacts are insufficient to resolve an ambiguity after inspecting the codebase first"
    ];
    const avoidExternalLookupWhen = [
        "the repo-local contract, packets, or codebase already answers the question",
        "the task is a local mechanical edit, refactor, formatting fix, or narrow docs update",
        "external search would replace reading the active packet, latest handoff, or latest reconcile first"
    ];
    if (options?.projectType === "docs") {
        avoidExternalLookupWhen.push("the repo already contains the canonical docs being updated");
    }
    if (options?.fileArea === "infra" || options?.taskType === "migration") {
        useExternalLookupWhen.push("live provider or platform documentation is needed to confirm current behavior");
    }
    return {
        inspectCodebaseFirst: true,
        repoDocsFirst: true,
        useExternalLookupWhen: uniqueStrings(useExternalLookupWhen),
        avoidExternalLookupWhen: uniqueStrings(avoidExternalLookupWhen)
    };
}
export function buildBootstrapNextAction() {
    return "Review .agent/context/context-tree.json, then record a checkpoint before non-trivial implementation or handoff.";
}
function uniqueStrings(items) {
    return [...new Set(items.filter((item) => Boolean(item.trim())))];
}
