/**
 * @fileoverview Parses terminal magic links that request attachment cards.
 */
import { isAbsolute } from 'node:path';
import { isSupportedAttachmentExtension } from './attachment-registry.js';
const MAGIC_LINK_RE = /codeman:\/\/attach\?([^\s<>"']+)/g;
export function parseAttachmentMagicLinks(data) {
    const results = [];
    const seen = new Set();
    for (const match of data.matchAll(MAGIC_LINK_RE)) {
        const query = trimTrailingPunctuation(match[1] || '');
        try {
            const params = new URLSearchParams(query);
            const filePath = params.get('path');
            if (!filePath || !isAbsolute(filePath))
                continue;
            const extension = filePath.split('.').pop()?.toLowerCase() || '';
            if (!isSupportedAttachmentExtension(extension))
                continue;
            if (seen.has(filePath))
                continue;
            seen.add(filePath);
            results.push(filePath);
        }
        catch {
            // Ignore malformed terminal text. Magic links are advisory.
        }
    }
    return results;
}
function trimTrailingPunctuation(value) {
    return value.replace(/[),.;:]+$/g, '');
}
//# sourceMappingURL=attachment-magic.js.map