/**
 * Workflows staleness — one directory per workflow (must contain
 * WORKFLOW.md), first-wins across project > user > system > extras.
 *
 * Not tracked in v1 manifests; treated as a new section that's empty on old
 * files, which causes one re-sync (filling the field) and then steady-state.
 */
import * as fs from 'fs';
import * as path from 'path';
import { firstWinsLayers, listAcrossLayers, resolveByName } from '../layers.js';
import { fingerprintDir, isDirStale } from '../fingerprint.js';
function isWorkflowDir(full) {
    try {
        return fs.statSync(full).isDirectory() && fs.existsSync(path.join(full, 'WORKFLOW.md'));
    }
    catch {
        return false;
    }
}
export const workflowsChecker = {
    type: 'workflows',
    listNames(cwd) {
        return listAcrossLayers(firstWinsLayers(cwd), 'workflows', (_, full) => isWorkflowDir(full));
    },
    build(name, cwd) {
        const resolved = resolveByName(firstWinsLayers(cwd), path.join('workflows', name), isWorkflowDir);
        if (!resolved)
            return null;
        return { dirPath: resolved.path, files: fingerprintDir(resolved.path) };
    },
    isFresh(name, stored, cwd) {
        const resolved = resolveByName(firstWinsLayers(cwd), path.join('workflows', name), isWorkflowDir);
        if (!resolved)
            return false;
        return !isDirStale(stored.dirPath, stored.files, resolved.path);
    },
};
