"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.shareService = exports.ShareService = void 0;
const models_1 = require("../../models");
/**
 * 分享服务
 * 生成分享链接和二维码
 */
class ShareService {
    baseUrl;
    constructor() {
        // 从环境变量获取基础 URL，默认使用当前域名
        this.baseUrl = process.env.APP_URL || 'http://localhost:3000';
    }
    /**
     * 生成书籍章节分享链接
     * @param chapterId 章节 ID
     */
    generateShareLink(chapterId) {
        return `${this.baseUrl}/#/pages/player/index?id=${chapterId}`;
    }
    /**
     * 生成分享卡片数据
     * @param chapterId 章节 ID
     */
    async generateShareCard(chapterId) {
        const chapter = await models_1.prisma.bookChapter.findUnique({
            where: { id: parseInt(chapterId) },
            include: { book: true },
        });
        if (!chapter) {
            throw new Error('章节不存在');
        }
        // 生成更完整的描述
        let description = '';
        if (chapter.summary) {
            description = chapter.summary;
        }
        else if (chapter.content && chapter.content.length > 100) {
            description = chapter.content.slice(0, 100) + '...';
        }
        else if (chapter.content) {
            description = chapter.content;
        }
        else {
            description = 'AI 有声书精彩内容';
        }
        return {
            title: `${chapter.book?.title || 'AI 有声书'} - 第${chapter.number}章 ${chapter.title}`,
            description: description,
            imageUrl: this.generateCoverImage(chapter),
            link: this.generateShareLink(chapterId),
            chapterId: chapter.id,
            wordCount: chapter.wordCount,
            audioDuration: chapter.audioDuration,
        };
    }
    /**
     * 生成封面图片
     */
    generateCoverImage(chapter) {
        // 实际项目中可以生成真实的图片
        return {
            type: 'gradient',
            colors: ['#4f46e5', '#818cf8'],
            icon: '🎵',
        };
    }
    /**
     * 生成二维码数据
     * @param chapterId 章节 ID
     */
    generateQRCodeData(chapterId) {
        return this.generateShareLink(chapterId);
    }
    /**
     * 记录分享行为
     */
    async trackShare(chapterId, userId, platform) {
        console.log(`📤 用户 ${userId} 分享了章节 ${chapterId} 到 ${platform}`);
    }
}
exports.ShareService = ShareService;
// 导出单例
exports.shareService = new ShareService();
