/**
 * Marketplace Aggregator
 *
 * Provides dashboard-facing API for marketplace scanner status,
 * submission queue management, and pipeline insights.
 */
import { SubmissionQueue } from '../../../core/fabric/submission-queue.js';
import { getJobManager } from '../../../core/background/job-manager.js';
export class MarketplaceAggregator {
    constructor(projectPath) {
        this.projectPath = projectPath;
        this.queue = new SubmissionQueue(projectPath);
    }
    /** Reload queue data from disk (external scanner worker may have updated it) */
    refreshQueue() {
        this.queue.reload();
    }
    /** Get scanner status from BackgroundJobManager state */
    async getScannerStatus() {
        this.refreshQueue();
        const jobManager = getJobManager(this.projectPath);
        const activeJobs = jobManager.getActiveJobs();
        const scannerJob = activeJobs.find(j => j.type === 'marketplace-scan' && (j.status === 'running' || j.status === 'pending'));
        // Also check completed jobs for last scan info
        const allJobs = jobManager.getJobs({ type: 'marketplace-scan' });
        const lastCompletedJob = allJobs.find(j => j.status === 'completed' || j.status === 'completed_with_warnings');
        const insights = this.queue.getInsights();
        return {
            isRunning: !!scannerJob,
            lastScanTime: lastCompletedJob
                ? new Date(lastCompletedJob.completedAt || lastCompletedJob.updatedAt).toISOString()
                : null,
            reposScanned: lastCompletedJob?.progress?.current ?? 0,
            reposDiscovered: insights.totalDiscovered,
            rateLimitRemaining: null,
            rateLimitReset: null,
            checkpoint: null,
        };
    }
    /** Get paginated/filtered submissions */
    async getQueue(filter) {
        this.refreshQueue();
        return this.queue.getSubmissions(filter || {});
    }
    /** Get only verified skills */
    async getVerifiedSkills() {
        this.refreshQueue();
        const result = this.queue.getSubmissions({ status: 'verified' });
        return result.items;
    }
    /** Get pipeline insights/analytics */
    async getInsights() {
        this.refreshQueue();
        return this.queue.getInsights();
    }
    /** Approve a submission */
    async approveSubmission(id) {
        this.queue.approve(id);
    }
    /** Reject a submission with reason */
    async rejectSubmission(id, reason) {
        this.queue.reject(id, reason);
    }
    /** Get a single submission by ID */
    async getSubmission(id) {
        return this.queue.getById(id);
    }
    /** Fetch verified skills from the vskill-platform API */
    async fetchPlatformSkills(limit = 20) {
        try {
            const res = await fetch(`https://verified-skill.com/api/v1/skills?sortBy=trendingScore7d&limit=${limit}`, { headers: { "User-Agent": "specweave-dashboard/1.0" }, signal: AbortSignal.timeout(10000) });
            if (!res.ok)
                return [];
            const data = await res.json();
            return data.skills ?? [];
        }
        catch {
            // Network errors are non-fatal — dashboard shows local data only
            return [];
        }
    }
}
//# sourceMappingURL=marketplace-aggregator.js.map