import path from 'node:path';
import { exists, nowIso, readJson, writeJsonAtomic } from './fsx.js';
export const MANAGED_PATHS_SCHEMA = 'sks.managed-paths.v2';
export const ROLLBACK_SCHEMA = 'sks.rollback.v1';
export function managedPathManifestPath(root) {
    return path.join(root, '.sneakoscope', 'managed-paths.json');
}
export async function managedPathManifest(root) {
    const file = managedPathManifestPath(root);
    const current = await readJson(file, null);
    if (current?.schema === MANAGED_PATHS_SCHEMA)
        return current;
    const manifest = {
        schema: MANAGED_PATHS_SCHEMA,
        generated_at: nowIso(),
        paths: defaultManagedPaths()
    };
    await writeJsonAtomic(file, manifest);
    return manifest;
}
export function defaultManagedPaths() {
    return [
        row('.sneakoscope/git-policy.json', 'tracked SKS git collaboration policy', false, 'shared_memory', 'tracked'),
        row('.sneakoscope/shared-memory-manifest.json', 'tracked shared/local memory plane manifest', false, 'shared_memory', 'tracked'),
        row('.sneakoscope/wiki/records', 'tracked sharded TriWiki claim records', false, 'shared_memory', 'tracked'),
        row('.sneakoscope/wiki/wrongness', 'tracked sharded wrongness negative evidence', false, 'shared_memory', 'tracked'),
        row('.sneakoscope/wiki/image-voxels', 'tracked sharded visual anchor evidence', false, 'shared_memory', 'tracked'),
        row('.sneakoscope/wiki/avoidance-rules', 'tracked sharded avoidance rules', false, 'shared_memory', 'tracked'),
        row('.sneakoscope/wiki/summaries', 'tracked shared markdown summaries', false, 'shared_memory', 'tracked'),
        row('.sneakoscope/wiki/indexes', 'ignored regenerated shared-memory indexes', true, 'generated_index', 'ignored'),
        row('.sneakoscope/wiki/context-packs', 'ignored regenerated context-pack snapshots', true, 'generated_index', 'ignored'),
        row('.sneakoscope/missions', 'local runtime mission state and proof artifacts', true, 'local_runtime', 'ignored'),
        row('.sneakoscope/reports', 'local runtime reports', true, 'local_runtime', 'ignored'),
        row('.sneakoscope/tmp', 'local temporary runtime files', true, 'local_runtime', 'ignored'),
        row('.sneakoscope/cache', 'local cache files', true, 'local_runtime', 'ignored'),
        row('.sneakoscope/logs', 'local log files', true, 'local_runtime', 'ignored'),
        row('.codex', 'Codex App hook/config surface generated by SKS setup', true, 'harness', 'ignored'),
        row('.agents/skills', 'Codex App SKS skill surface generated by SKS setup', true, 'harness', 'ignored'),
        row('AGENTS.md', 'repository-level SKS managed instructions block', false, 'harness', 'tracked')
    ];
}
export async function rollbackList(root) {
    const manifest = await managedPathManifest(root);
    const actions = [];
    for (const entry of manifest.paths.filter((row) => row.rollback)) {
        actions.push({
            id: rollbackId(entry.path),
            path: entry.path,
            exists: await exists(path.join(root, entry.path)),
            action: 'remove-managed-path',
            requires_confirmation: true
        });
    }
    return {
        schema: ROLLBACK_SCHEMA,
        generated_at: nowIso(),
        root,
        actions
    };
}
export function rollbackId(relPath = '') {
    return `rollback-${String(relPath).replace(/[^A-Za-z0-9]+/g, '-').replace(/^-|-$/g, '').toLowerCase() || 'root'}`;
}
function row(relPath, purpose, rollback, plane, git) {
    return {
        path: relPath,
        owner: 'sks',
        created_by: 'setup',
        plane,
        git,
        purpose,
        rollback: Boolean(rollback),
        requires_confirmation_for_delete: plane === 'shared_memory'
    };
}
//# sourceMappingURL=managed-paths.js.map