/**
 * Artifact Handoff Module (Module D)
 *
 * SPARC Architecture Reference: §2.5
 *
 * PURPOSE: Pass artifacts between services without mutation
 *
 * DEPENDENCIES: None (pure data passthrough)
 *
 * FORBIDDEN:
 * - Artifact Validation (NO schema checking)
 * - Artifact Transformation (NO field mapping)
 * - Artifact Storage (NO caching or persistence)
 * - Content Interpretation (NO parsing of artifact data)
 */
// ============================================================================
// Artifact Handoff Implementation
// ============================================================================
export class ArtifactHandoff {
    /**
     * Pass artifact through unchanged
     * SPARC: Artifacts are never modified by CLI
     */
    passthrough(artifact) {
        // Return artifact unchanged - pure passthrough
        return artifact;
    }
    /**
     * Extract reference from response body
     * SPARC: No validation, just structural extraction
     *
     * Handles multiple response formats:
     * 1. Standard reference: { id, type, repository, ... }
     * 2. Planner response: { success, plan: { metadata: { planId, ... } }, ... }
     * 3. Manifests response: { id, type, repository, execution_metadata, ... }
     */
    extractReference(body) {
        if (!body || typeof body !== 'object') {
            return null;
        }
        const obj = body;
        // Format 1: Standard reference format (has id, type, repository at root)
        if ('id' in obj && 'type' in obj && 'repository' in obj) {
            return {
                id: String(obj['id']),
                type: obj['type'],
                repository: String(obj['repository']),
                created_at: 'created_at' in obj ? String(obj['created_at']) : new Date().toISOString(),
                checksum: 'checksum' in obj ? String(obj['checksum']) : '',
            };
        }
        // Format 2: Planner service response { success, plan: { metadata: { planId } }, execution_metadata }
        if ('plan' in obj && typeof obj['plan'] === 'object' && obj['plan'] !== null) {
            const plan = obj['plan'];
            if ('metadata' in plan && typeof plan['metadata'] === 'object' && plan['metadata'] !== null) {
                const metadata = plan['metadata'];
                const planId = metadata['planId'];
                const createdAt = metadata['createdAt'];
                const contentHash = metadata['contentHash'];
                if (planId) {
                    return {
                        id: planId,
                        type: 'PlanReference',
                        repository: 'agentics-simulation-planner',
                        created_at: createdAt ?? new Date().toISOString(),
                        checksum: contentHash ? `sha256:${contentHash}` : '',
                    };
                }
            }
        }
        // Format 3: Simulation engine response { success, result: { simulationId } }
        if ('result' in obj && typeof obj['result'] === 'object' && obj['result'] !== null) {
            const result = obj['result'];
            if ('simulationId' in result) {
                return {
                    id: String(result['simulationId']),
                    type: 'SimulationReference',
                    repository: 'agentics-simulation-engine',
                    created_at: new Date().toISOString(),
                    checksum: '',
                };
            }
        }
        // Format 4: Nested response with id inside a domain object
        // Handles: { export: { id }, execution_metadata }, { diligence_package: { id }, execution_metadata },
        // { roi_report: { id }, execution_metadata }, { intent: { id }, execution_metadata }
        const domainKeys = ['export', 'diligence_package', 'roi_report', 'intent', 'deployment', 'simulation'];
        for (const key of domainKeys) {
            if (key in obj && typeof obj[key] === 'object' && obj[key] !== null) {
                const domain = obj[key];
                if ('id' in domain) {
                    const typeMap = {
                        'export': 'ExportReference',
                        'diligence_package': 'DiligencePackage',
                        'roi_report': 'ROIReport',
                        'intent': 'IntentReference',
                        'deployment': 'DeploymentReference',
                        'simulation': 'SimulationReference',
                    };
                    return {
                        id: String(domain['id']),
                        type: typeMap[key] ?? 'Reference',
                        repository: key,
                        created_at: 'generated_at' in domain ? String(domain['generated_at']) : new Date().toISOString(),
                        checksum: '',
                    };
                }
            }
        }
        return null;
    }
    /**
     * Build payload for downstream service
     * SPARC: Reference passed verbatim, additional params merged
     */
    buildPayload(ref, additionalParams) {
        return {
            ref,
            ...additionalParams,
        };
    }
}
// ============================================================================
// Reference Type Guards (for type narrowing only, no validation)
// ============================================================================
export function isPlanReference(ref) {
    return ref.type === 'PlanReference';
}
export function isSimulationReference(ref) {
    return ref.type === 'SimulationReference';
}
export function isIntentReference(ref) {
    return ref.type === 'IntentReference';
}
export function isExportReference(ref) {
    return ref.type === 'ExportReference';
}
export function isDiligencePackageReference(ref) {
    return ref.type === 'DiligencePackageReference';
}
// ============================================================================
// Factory Function
// ============================================================================
export function createArtifactHandoff() {
    return new ArtifactHandoff();
}
//# sourceMappingURL=artifact-handoff.js.map