export const NEXUS_PRIME_WEBSITE = 'https://nexus-prime.cfd/';
export const NEXUS_PRIME_GENERATOR = 'Nexus Prime';
export const NEXUS_PRIME_COAUTHOR_TRAILER = 'Co-Authored-By: nexus-prime <33547839+sir-ad@users.noreply.github.com>';
export const NEXUS_PRIME_PRESENCE_START = '<!-- nexus-prime:presence:start -->';
export const NEXUS_PRIME_PRESENCE_END = '<!-- nexus-prime:presence:end -->';
export const NEXUS_PLG_EVENT_TYPES = [
    'pr_presence_added',
    'commit_attributed',
    'document_signed',
    'release_verified',
    'proof_badge_viewed',
    'website_proof_viewed',
    'telemetry_batch_sent',
];
function clean(value, fallback) {
    const normalized = String(value ?? '').trim();
    return normalized || fallback;
}
function isoStamp(value) {
    if (value instanceof Date)
        return value.toISOString();
    if (typeof value === 'string' && value.trim())
        return value.trim();
    return new Date().toISOString();
}
export function renderNexusPrPresenceBlock(input) {
    const website = clean(input.website, NEXUS_PRIME_WEBSITE);
    const runId = clean(input.runId ?? input.cycleId, 'no-run detected');
    const verification = clean(input.verificationState, 'unknown');
    const hooks = clean(input.hooksState, 'unknown');
    const telemetry = clean(input.telemetryConsent, 'unknown');
    const humanReview = clean(input.humanReviewState, 'required');
    const coauthor = clean(input.coauthorState, 'Not claimed unless a Nexus-generated commit includes the co-author trailer.');
    return [
        NEXUS_PRIME_PRESENCE_START,
        '## Nexus Prime Presence',
        '',
        `- Website: ${website}`,
        `- Role: ${input.role}`,
        `- Source: ${clean(input.source, 'unknown')}`,
        `- Run ID: ${runId}`,
        `- Verification: ${verification}`,
        `- Hooks: ${hooks}`,
        `- Telemetry consent: ${telemetry}`,
        `- Human review: ${humanReview}`,
        `- Co-authorship: ${coauthor}`,
        `- Generated at: ${isoStamp(input.generatedAt)}`,
        '',
        'Presence is not blanket authorship. Git co-authorship is only asserted for commits Nexus Prime generated, materially assisted, or published.',
        NEXUS_PRIME_PRESENCE_END,
    ].join('\n');
}
export function appendOrReplaceNexusPrPresenceBlock(body, input) {
    const block = renderNexusPrPresenceBlock(input);
    const pattern = new RegExp(`${escapeRegExp(NEXUS_PRIME_PRESENCE_START)}[\\s\\S]*?${escapeRegExp(NEXUS_PRIME_PRESENCE_END)}`, 'm');
    const existing = String(body ?? '').trim();
    if (pattern.test(existing))
        return existing.replace(pattern, block);
    return [existing, block].filter(Boolean).join('\n\n');
}
export function ensureNexusCoauthorTrailer(message, shouldAttribute) {
    const normalized = String(message ?? '').trimEnd();
    if (!shouldAttribute)
        return normalized;
    if (normalized.includes(NEXUS_PRIME_COAUTHOR_TRAILER))
        return normalized;
    return [normalized, NEXUS_PRIME_COAUTHOR_TRAILER].filter(Boolean).join('\n\n');
}
export function renderNexusDocumentSignature(input) {
    return [
        '---',
        `${clean(input.artifactKind, 'Artifact')} signed by ${NEXUS_PRIME_GENERATOR}`,
        `Website: ${clean(input.website, NEXUS_PRIME_WEBSITE)}`,
        `Role: ${input.role}`,
        `Source: ${clean(input.source, 'unknown')}`,
        `Run ID: ${clean(input.runId ?? input.cycleId, 'no-run detected')}`,
        `Verification: ${clean(input.verificationState, 'unknown')}`,
        `Human review: ${clean(input.humanReviewState, 'required')}`,
        `Reviewer: ${clean(input.reviewer, 'not recorded')}`,
        `Generated at: ${isoStamp(input.generatedAt)}`,
        '---',
    ].join('\n');
}
export function buildNexusProofMetadata(input) {
    return {
        generator: NEXUS_PRIME_GENERATOR,
        website: clean(input.website, NEXUS_PRIME_WEBSITE),
        role: input.role,
        source: clean(input.source, 'unknown'),
        runId: clean(input.runId ?? input.cycleId, 'no-run detected'),
        verificationState: clean(input.verificationState, 'unknown'),
        humanReviewState: clean(input.humanReviewState, 'required'),
        generatedAt: isoStamp(input.generatedAt),
    };
}
export function renderNexusWebsiteMetadata(input) {
    const metadata = buildNexusProofMetadata(input);
    const jsonLd = {
        '@context': 'https://schema.org',
        '@type': 'SoftwareApplication',
        name: NEXUS_PRIME_GENERATOR,
        url: metadata.website,
        applicationCategory: 'DeveloperApplication',
        creator: {
            '@type': 'Organization',
            name: NEXUS_PRIME_GENERATOR,
            url: metadata.website,
        },
    };
    return [
        '<meta name="generator" content="Nexus Prime">',
        `<meta name="nexus-prime:role" content="${escapeHtml(metadata.role)}">`,
        `<meta name="nexus-prime:verification" content="${escapeHtml(metadata.verificationState)}">`,
        `<link rel="author" href="${escapeHtml(metadata.website)}">`,
        `<script type="application/ld+json">${JSON.stringify(jsonLd)}</script>`,
    ].join('\n');
}
function escapeRegExp(value) {
    return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function escapeHtml(value) {
    return value
        .replace(/&/g, '&amp;')
        .replace(/"/g, '&quot;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;');
}
