import { createHash } from 'node:crypto';
import { basename, resolve } from 'node:path';
import { PROJECT_KNOWLEDGE_SPACE_PREFIX, isProjectKnowledgeSpace, normalizeKnowledgeSpaceId, normalizeProjectId, normalizeSpaceComponent, projectKnowledgeSpaceId, } from '../spaces.js';
export const PROJECT_PLANNING_CONNECTOR_ID = 'goodvibes-project-planning';
export const PROJECT_PLANNING_TAG = 'project-planning';
export function projectPlanningProjectIdFromPath(path) {
    const resolved = resolve(path);
    const name = normalizeSpaceComponent(basename(resolved) || 'project');
    const digest = createHash('sha256').update(resolved).digest('hex').slice(0, 10);
    return `${name}-${digest}`;
}
export function resolveProjectPlanningSpace(input = {}, defaultProjectId = 'default') {
    const explicitSpace = typeof input.knowledgeSpaceId === 'string'
        ? normalizeKnowledgeSpaceId(input.knowledgeSpaceId)
        : '';
    if (explicitSpace && isProjectKnowledgeSpace(explicitSpace)) {
        return {
            knowledgeSpaceId: explicitSpace,
            projectId: projectIdFromSpace(explicitSpace),
        };
    }
    const projectId = normalizeProjectId(input.projectId ?? defaultProjectId);
    return {
        projectId,
        knowledgeSpaceId: projectKnowledgeSpaceId(projectId),
    };
}
export function projectIdFromSpace(spaceId) {
    const normalized = normalizeKnowledgeSpaceId(spaceId);
    if (!normalized.startsWith(PROJECT_KNOWLEDGE_SPACE_PREFIX)) {
        return normalizeProjectId(normalized);
    }
    return normalizeProjectId(normalized.slice(PROJECT_KNOWLEDGE_SPACE_PREFIX.length));
}
export function projectPlanningCanonicalUri(spaceId, kind, id) {
    return `goodvibes://planning/${encodeURIComponent(spaceId)}/${kind}/${encodeURIComponent(id)}`;
}
export function projectPlanningSourceId(spaceId, kind, id) {
    const digest = createHash('sha256').update(`${spaceId}:${kind}:${id}`).digest('hex').slice(0, 16);
    return `project-planning-${kind}-${digest}`;
}
export function projectPlanningArtifactSummary(kind, value) {
    if (kind === 'state') {
        const state = value;
        return state.goal ? `Planning state for ${state.goal}` : 'Project planning state.';
    }
    if (kind === 'decision') {
        const decision = value;
        return decision.decision ? `${decision.title ?? 'Decision'}: ${decision.decision}` : 'Project planning decision record.';
    }
    const language = value;
    return `Project language artifact with ${language.terms?.length ?? 0} terms and ${language.ambiguities?.length ?? 0} resolved ambiguities.`;
}
export function stablePlanningId(prefix, value) {
    const normalized = normalizeSpaceComponent(value);
    if (normalized !== 'default')
        return normalized;
    const digest = createHash('sha256').update(value || prefix).digest('hex').slice(0, 10);
    return `${prefix}-${digest}`;
}
export function readPlanningMetadataObject(value) {
    return value && typeof value === 'object' && !Array.isArray(value)
        ? value
        : {};
}
