import { messageBookmarksDb } from '../../../modules/database/index.js';
import { AppError } from '../../../shared/utils.js';
function normalizeInput(input) {
    return {
        messageUuid: input.messageUuid.trim(),
        sessionId: input.sessionId.trim(),
        contentSnippet: String(input.contentSnippet || '').slice(0, 500),
        provider: input.provider || 'claude',
        role: input.role || 'assistant',
        messageTimestamp: input.messageTimestamp || new Date().toISOString(),
        projectId: input.projectId?.trim() || null,
    };
}
function toBookmarkResponse(row) {
    return {
        id: row.id,
        messageUuid: row.message_uuid,
        sessionId: row.session_id,
        contentSnippet: row.content_snippet,
        provider: row.provider,
        role: row.role,
        messageTimestamp: row.message_timestamp,
        projectId: row.project_id,
        createdAt: row.created_at,
    };
}
export function toggleMessageBookmark(userId, input) {
    const normalized = normalizeInput(input);
    if (!normalized.messageUuid || !normalized.sessionId) {
        throw new AppError('messageUuid and sessionId are required', {
            code: 'BOOKMARK_INPUT_REQUIRED',
            statusCode: 400,
        });
    }
    return messageBookmarksDb.toggle(userId, normalized);
}
export function getBookmarkedMessageUuids(userId, messageUuids) {
    const set = messageBookmarksDb.getBookmarkedMessageUuids(userId, messageUuids);
    return { bookmarkedUuids: [...set] };
}
export function deleteBookmark(userId, messageUuid) {
    if (!messageUuid || !messageUuid.trim()) {
        throw new AppError('messageUuid is required', {
            code: 'BOOKMARK_DELETE_REQUIRED',
            statusCode: 400,
        });
    }
    const deleted = messageBookmarksDb.delete(userId, messageUuid.trim());
    return { deleted };
}
export function listBookmarks(userId, opts) {
    const result = messageBookmarksDb.listByUser(userId, opts);
    return {
        bookmarks: result.bookmarks.map(toBookmarkResponse),
        total: result.total,
    };
}
//# sourceMappingURL=message-bookmark.service.js.map