/**
 * Origin Metadata Types
 *
 * Defines types for tracking the origin of User Stories and Tasks:
 * - Internal: Created within SpecWeave increments
 * - External: Imported from external tools (GitHub, JIRA, ADO)
 */
/**
 * Origin badge emojis for living docs
 */
export const ORIGIN_BADGES = {
    internal: '🏠', // Internal
    github: '🔗', // GitHub
    jira: '🎫', // JIRA
    ado: '📋', // Azure DevOps
    unknown: '❓' // Unknown external source
};
/**
 * Get origin badge for display
 *
 * @param source - External source or 'internal'
 * @returns Emoji badge
 *
 * @example
 * getOriginBadge('github') // Returns: '🔗'
 * getOriginBadge('internal') // Returns: '🏠'
 */
export function getOriginBadge(source) {
    return ORIGIN_BADGES[source] || ORIGIN_BADGES.unknown;
}
/**
 * Format origin for display in living docs
 *
 * @param metadata - External item metadata
 * @returns Formatted origin string (e.g., "🔗 GitHub #638")
 *
 * @example
 * formatOrigin({
 *   id: 'US-004E',
 *   origin: 'external',
 *   source: 'github',
 *   external_id: 'GH-#638',
 *   external_url: 'https://github.com/...',
 *   imported_at: '2025-11-19T10:30:00Z'
 * })
 * // Returns: "🔗 [GitHub #638](https://github.com/...)"
 */
export function formatOrigin(metadata) {
    const badge = getOriginBadge(metadata.source);
    const displayId = metadata.external_id.replace(/^(GH|JIRA|ADO)-/, '');
    return `${badge} [${metadata.source.toUpperCase()} ${displayId}](${metadata.external_url})`;
}
/**
 * Check if ID is from external source (has E suffix)
 *
 * @param id - User Story or Task ID
 * @returns True if external, false if internal
 *
 * @example
 * isExternalId('US-004E') // Returns: true
 * isExternalId('US-003') // Returns: false
 * isExternalId('T-010E') // Returns: true
 */
export function isExternalId(id) {
    return id.endsWith('E');
}
/**
 * Extract origin from ID suffix
 *
 * @param id - User Story or Task ID
 * @returns Origin type
 *
 * @example
 * getOriginFromId('US-004E') // Returns: 'external'
 * getOriginFromId('US-003') // Returns: 'internal'
 */
export function getOriginFromId(id) {
    return isExternalId(id) ? 'external' : 'internal';
}
/**
 * Parse external ID to extract source and number
 *
 * @param externalId - External ID (e.g., "GH-#638", "JIRA-SPEC-789")
 * @returns Parsed components
 *
 * @example
 * parseExternalId('GH-#638')
 * // Returns: { source: 'github', number: '638' }
 *
 * parseExternalId('JIRA-SPEC-789')
 * // Returns: { source: 'jira', number: 'SPEC-789' }
 */
export function parseExternalId(externalId) {
    const ghMatch = externalId.match(/^GH-#?(\d+)$/);
    if (ghMatch) {
        return { source: 'github', number: ghMatch[1] };
    }
    const jiraMatch = externalId.match(/^JIRA-(.+)$/);
    if (jiraMatch) {
        return { source: 'jira', number: jiraMatch[1] };
    }
    const adoMatch = externalId.match(/^ADO-(\d+)$/);
    if (adoMatch) {
        return { source: 'ado', number: adoMatch[1] };
    }
    return null;
}
/**
 * Create external item metadata
 */
export function createExternalMetadata(params) {
    return {
        id: params.id,
        origin: 'external',
        source: params.source,
        external_id: params.externalId,
        external_url: params.externalUrl,
        imported_at: new Date().toISOString(),
        ...(params.externalTitle !== undefined && { external_title: params.externalTitle }),
        ...(params.formatPreservation !== undefined && { format_preservation: params.formatPreservation }),
    };
}
/**
 * Validate external item metadata
 *
 * @throws Error if metadata is invalid
 */
export function validateExternalMetadata(metadata) {
    const VALID_SOURCES = ['github', 'jira', 'ado', 'unknown'];
    const id = metadata.id;
    if (!id)
        throw new Error('External item metadata must have id');
    if (!id.endsWith('E'))
        throw new Error(`External item ID must end with E suffix: ${id}`);
    if (metadata.origin !== 'external')
        throw new Error(`External item must have origin='external': ${id}`);
    if (!VALID_SOURCES.includes(metadata.source))
        throw new Error(`Invalid external source: ${metadata.source}`);
    if (!metadata.external_id)
        throw new Error(`External item must have external_id: ${id}`);
    if (!metadata.external_url)
        throw new Error(`External item must have external_url: ${id}`);
    if (!metadata.imported_at)
        throw new Error(`External item must have imported_at: ${id}`);
    if (isNaN(new Date(metadata.imported_at).getTime()))
        throw new Error(`Invalid imported_at timestamp: ${metadata.imported_at}`);
}
//# sourceMappingURL=origin-metadata.js.map