// tools/resolve-project-id.ts — Shared helpers for project resolution
//
// SPEC-304: resolveProjectId (sync, fast — for callers that always have a path/id)
// SPEC-341: resolveProjectIdOrAutoDetect (async — zero-config auto-detection)
//           buildMissingProjectError (async — includes registeredProjects list)
import { hashProjectPath } from '../storage/base-store.js';
import { resolveProjectPath } from './resolve-project-path.js';
/**
 * Resolve projectId from either projectId or projectPath.
 * projectPath takes precedence when both are provided.
 * Returns null when neither is provided.
 *
 * This is the SYNC fast path for callers that always have an explicit param.
 * For zero-config auto-detection use resolveProjectIdOrAutoDetect().
 */
export function resolveProjectId(params) {
    if (params.projectPath?.trim()) {
        return hashProjectPath(params.projectPath.trim());
    }
    if (params.projectId?.trim()) {
        return params.projectId.trim();
    }
    return null;
}
/**
 * Resolve a projectId with full zero-config auto-detection (SPEC-341).
 *
 * Falls back to PLANU_PROJECT_PATH env var and the global project registry
 * when neither projectId nor projectPath is passed explicitly.
 *
 * Returns a discriminated union so callers can early-return on failure:
 *
 * ```ts
 * const resolved = await resolveProjectIdOrAutoDetect(params);
 * if (!resolved.ok) return resolved.errorResult;
 * const { projectId, projectPath, autoDetected } = resolved;
 * ```
 */
export async function resolveProjectIdOrAutoDetect(params) {
    const result = await resolveProjectPath(params);
    switch (result.kind) {
        case 'resolved':
            return {
                ok: true,
                projectId: result.projectId,
                projectPath: result.projectPath,
                autoDetected: result.autoDetected,
            };
        case 'ambiguous':
            return {
                ok: false,
                errorResult: buildAmbiguousProjectError(result.registeredProjects),
            };
        case 'missing':
            return {
                ok: false,
                errorResult: buildMissingProjectError(result.registeredProjects),
            };
    }
}
// ---------------------------------------------------------------------------
// Error builders
// ---------------------------------------------------------------------------
/**
 * Build a ToolResult error for the "missing project" case.
 * Includes a registeredProjects[] list so the user can see their options.
 */
export function buildMissingProjectError(registeredProjects) {
    const hint = 'Provide projectPath to identify the project. Example: { "projectPath": "/path/to/your/project" }';
    const content = [{ type: 'text', text: hint }];
    return {
        content,
        isError: true,
        registeredProjects,
    };
}
/**
 * Build a ToolResult error for the "ambiguous project" case (multiple registered).
 * Lists all known projects with their id + path so the user can pick one.
 */
export function buildAmbiguousProjectError(registeredProjects) {
    const lines = registeredProjects.map((p) => `  • ${p.name}  path: "${p.path}"  id: "${p.id}"`);
    const text = `Multiple projects are registered. Pass projectPath or projectId to select one:\n` +
        lines.join('\n');
    return {
        content: [{ type: 'text', text }],
        isError: true,
        registeredProjects,
    };
}
// Static error result for backwards-compatibility.
// Prefer buildMissingProjectError() when registeredProjects data is available.
// New code should use resolveProjectIdOrAutoDetect() + its built-in error instead.
export const missingProjectIdError = {
    content: [
        {
            type: 'text',
            text: 'Provide projectId or projectPath to identify the project. Example: { "projectPath": "/path/to/your/project" }',
        },
    ],
    isError: true,
};
//# sourceMappingURL=resolve-project-id.js.map