/**
 * DiapDoc Parser - 解析 Diap 去中心化身份协议文档
 *
 * 负责解析 Diap 智能体发布的 IPFS/IPNS 文档，提取：
 * - DID (去中心化身份标识)
 * - 频道信息
 * - 能力列表
 * - 网络地址
 */
/**
 * DiapDoc Parser 类
 */
export class DiapDocParser {
    /**
     * 解析 Diap 文档 (从 IPFS CID 或 IPNS 获取的内容)
     */
    parse(content) {
        try {
            const data = JSON.parse(content);
            // 验证必需字段
            if (!data.id && !data.did) {
                return {
                    success: false,
                    error: 'Missing required field: id or did'
                };
            }
            const doc = {
                id: data.id || data.did,
                name: data.name || 'Unknown Agent',
                version: data.version || '1.0',
                // 能力信息
                capabilities: this.normalizeArray(data.capabilities || data.capability || []),
                interests: this.normalizeArray(data.interests || data.interest || []),
                // 网络信息
                peerId: data.peerId || data.peer_id || data.peer,
                multiaddrs: this.normalizeArray(data.multiaddrs || data.addresses || []),
                relayAddr: data.relayAddr || data.relay_addr || data.relay,
                // 频道信息
                channels: this.parseChannels(data.channels || data.channel || []),
                // 身份信息
                publicKey: data.publicKey || data.public_key,
                signature: data.signature,
                // 时间戳
                createdAt: data.createdAt || data.created_at,
                updatedAt: data.updatedAt || data.updated_at,
                timestamp: data.timestamp || data.createdAt ? Date.now() : undefined
            };
            return { success: true, doc };
        }
        catch (e) {
            return {
                success: false,
                error: `Failed to parse DiapDoc: ${e}`
            };
        }
    }
    /**
     * 解析频道数组
     */
    parseChannels(channels) {
        if (!Array.isArray(channels))
            return [];
        return channels.map(ch => {
            if (typeof ch === 'string') {
                return {
                    id: ch,
                    name: ch
                };
            }
            return {
                id: ch.id || ch.channelId || ch.channel_id || String(Date.now()),
                name: ch.name || ch.channelName || ch.channel_name || 'Unnamed Channel',
                topic: ch.topic,
                type: ch.type,
                isPublic: ch.isPublic ?? ch.is_public ?? true,
                memberCount: ch.memberCount || ch.member_count
            };
        });
    }
    /**
     * 规范化数组
     */
    normalizeArray(value) {
        if (Array.isArray(value)) {
            return value.map(v => String(v));
        }
        if (typeof value === 'string') {
            return [value];
        }
        return [];
    }
    /**
     * 从 CID 解析文档
     */
    async parseFromCID(cid, ipfsEndpoint = 'http://127.0.0.1:5001') {
        try {
            const { IpfsClient } = await import('@diap/sdk');
            const ipfs = new IpfsClient(ipfsEndpoint, null);
            const content = await ipfs.get(cid);
            return this.parse(content);
        }
        catch (e) {
            return {
                success: false,
                error: `Failed to fetch from IPFS: ${e}`
            };
        }
    }
    /**
     * 从 IPNS 解析文档
     */
    async parseFromIPNS(ipnsName, ipfsEndpoint = 'http://127.0.0.1:5001') {
        try {
            const { IpfsClient } = await import('@diap/sdk');
            const ipfs = new IpfsClient(ipfsEndpoint, null);
            const cid = await ipfs.resolveIpns(ipnsName);
            if (!cid) {
                return {
                    success: false,
                    error: 'IPNS resolve returned no CID'
                };
            }
            return this.parseFromCID(cid, ipfsEndpoint);
        }
        catch (e) {
            return {
                success: false,
                error: `Failed to resolve IPNS: ${e}`
            };
        }
    }
    /**
     * 从 URL 解析 (用于解析网关返回的内容)
     */
    parseFromUrl(url) {
        return fetch(url)
            .then(resp => resp.text())
            .then(content => this.parse(content))
            .catch(e => ({
            success: false,
            error: `Failed to fetch URL: ${e}`
        }));
    }
    /**
     * 提取 DID
     */
    extractDID(doc) {
        return doc.id;
    }
    /**
     * 提取用于连接的信息
     */
    extractConnectionInfo(doc) {
        return {
            peerId: doc.peerId,
            multiaddrs: doc.multiaddrs || [],
            relayAddr: doc.relayAddr
        };
    }
    /**
     * 提取频道列表
     */
    extractChannels(doc) {
        return doc.channels || [];
    }
    /**
     * 验证文档签名 (如果实现)
     */
    async verifySignature(doc) {
        if (!doc.publicKey || !doc.signature) {
            return true; // 无签名时默认通过
        }
        // TODO: 实现签名验证
        return true;
    }
}
// 全局解析器实例
let parserInstance = null;
export function createDiapDocParser() {
    return new DiapDocParser();
}
export function getDiapDocParser() {
    if (!parserInstance) {
        parserInstance = new DiapDocParser();
    }
    return parserInstance;
}
/**
 * 从 DiapAnnouncement 提取 DiapDoc 信息
 */
export function extractDocFromAnnouncement(announcement) {
    return {
        id: announcement.leaderDid,
        name: announcement.channelName,
        version: '1.0',
        capabilities: announcement.capabilities || [],
        interests: announcement.interests || [],
        channels: announcement.channelId ? [{
                id: announcement.channelId,
                name: announcement.channelName,
                topic: announcement.topic
            }] : []
    };
}
