import { readJson, writeJson, projectDataDir } from './base-store.js';
// --- internal helpers ---
function patternsFile(projectId) {
    return `${projectDataDir(projectId)}/patterns.json`;
}
async function loadAll(projectId) {
    return readJson(patternsFile(projectId), []);
}
async function saveAll(projectId, patterns) {
    await writeJson(patternsFile(projectId), patterns);
}
// --- public API ---
/**
 * List every learned pattern for a project.
 */
export async function listPatterns(projectId) {
    return loadAll(projectId);
}
/**
 * Get a single pattern by ID. Returns `null` when not found.
 */
export async function getPattern(projectId, patternId) {
    const patterns = await loadAll(projectId);
    return patterns.find((p) => p.id === patternId) ?? null;
}
/**
 * Add a new learned pattern.
 */
export async function addPattern(projectId, pattern) {
    const patterns = await loadAll(projectId);
    // If a pattern with the same ID already exists, update it instead
    const idx = patterns.findIndex((p) => p.id === pattern.id);
    if (idx !== -1) {
        patterns[idx] = pattern;
    }
    else {
        patterns.push(pattern);
    }
    await saveAll(projectId, patterns);
    return pattern;
}
/**
 * Update an existing pattern. Throws if not found.
 */
export async function updatePattern(projectId, patternId, updates) {
    const patterns = await loadAll(projectId);
    const idx = patterns.findIndex((p) => p.id === patternId);
    if (idx === -1) {
        throw new Error(`Pattern "${patternId}" not found in project "${projectId}"`);
    }
    const existing = patterns[idx];
    if (!existing) {
        throw new Error(`Pattern "${patternId}" not found in project "${projectId}"`);
    }
    const updated = {
        ...existing,
        ...updates,
        id: patternId, // never allow id override
    };
    patterns[idx] = updated;
    await saveAll(projectId, patterns);
    return updated;
}
/**
 * Increment the usage count of a pattern.
 */
export async function incrementPatternUsage(projectId, patternId) {
    const patterns = await loadAll(projectId);
    const idx = patterns.findIndex((p) => p.id === patternId);
    if (idx === -1) {
        throw new Error(`Pattern "${patternId}" not found in project "${projectId}"`);
    }
    const pattern = patterns[idx];
    if (!pattern) {
        throw new Error(`Pattern "${patternId}" not found in project "${projectId}"`);
    }
    pattern.usageCount += 1;
    await saveAll(projectId, patterns);
    return pattern;
}
/**
 * Delete a pattern by ID. Returns `true` if removed, `false` if not found.
 */
export async function deletePattern(projectId, patternId) {
    const patterns = await loadAll(projectId);
    const idx = patterns.findIndex((p) => p.id === patternId);
    if (idx === -1) {
        return false;
    }
    patterns.splice(idx, 1);
    await saveAll(projectId, patterns);
    return true;
}
/**
 * Find patterns by type.
 */
export async function findPatternsByType(projectId, type) {
    const patterns = await loadAll(projectId);
    return patterns.filter((p) => p.type === type);
}
/**
 * Find patterns matching a predicate.
 */
export async function findPatterns(projectId, predicate) {
    const patterns = await loadAll(projectId);
    return patterns.filter(predicate);
}
/**
 * Clear all patterns for a project.
 */
export async function clearPatterns(projectId) {
    await saveAll(projectId, []);
}
//# sourceMappingURL=pattern-store.js.map