import { mountInitiativesCollection, readInitiative, } from '../../core/collections/initiatives/index.js';
import { formatContextStoreBinding, formatContextStoreBindingSelector, resolveContextStoreBinding, } from '../../core/context-store/index.js';
import { getWorkspaceContextInitiativeId, } from '../../core/workspace/index.js';
import { asErrorMessage, makeStatus } from './types.js';
function contextStoreBindingWarningToStatus(warning) {
    return makeStatus('warning', warning.code, warning.message, {
        target: warning.target ? `workspace.context.store.${warning.target}` : 'workspace.context.store',
        ...(warning.fix ? { fix: warning.fix } : {}),
    });
}
export async function collectWorkspaceContextStatuses(context) {
    if (!context) {
        return [];
    }
    const initiativeId = getWorkspaceContextInitiativeId(context);
    const contextStoreLabel = formatContextStoreBinding(context.store);
    const selector = formatContextStoreBindingSelector(context.store);
    let resolvedStore;
    try {
        resolvedStore = await resolveContextStoreBinding(context.store);
    }
    catch (error) {
        return [
            makeStatus('error', 'workspace_context_store_unavailable', `Workspace context store '${contextStoreLabel}' could not be read: ${asErrorMessage(error)}`, {
                target: 'workspace.context.store',
                fix: context.store.selector.kind === 'registry'
                    ? 'projectx context-store doctor'
                    : `Check the path in .projectx-workspace/view.yaml or run projectx initiative show ${initiativeId} ${selector}`,
            }),
        ];
    }
    const statuses = resolvedStore.warnings.map(contextStoreBindingWarningToStatus);
    try {
        const initiative = await readInitiative({
            collection: mountInitiativesCollection(resolvedStore.root),
            id: initiativeId,
        });
        if (!initiative) {
            return [
                ...statuses,
                makeStatus('error', 'workspace_initiative_missing', `Workspace initiative '${contextStoreLabel}/${initiativeId}' was not found.`, {
                    target: 'workspace.context.initiative',
                    fix: `projectx initiative show ${initiativeId} ${selector}`,
                }),
            ];
        }
        return statuses;
    }
    catch (error) {
        return [
            ...statuses,
            makeStatus('error', 'workspace_initiative_unavailable', `Workspace initiative '${contextStoreLabel}/${initiativeId}' could not be read: ${asErrorMessage(error)}`, {
                target: 'workspace.context.initiative',
                fix: `projectx initiative show ${initiativeId} ${selector}`,
            }),
        ];
    }
}
//# sourceMappingURL=context-status.js.map