import { StorageManager } from './storage.js';
import { VersioningConfig } from './page-versioning.js';
import './consolidation-operator.js';
import './types.js';
import './index-1qIcnbG1.js';
import './memory-projection-store-CY8TU40w.js';
import 'better-sqlite3';

/**
 * Consolidation undo (issue #561 PR 5).
 *
 * Reverts a consolidated memory by restoring each source memory from its
 * `derived_from` snapshot and archiving the target.
 *
 * Contract:
 *   - Load the target memory markdown file via its absolute path.
 *   - For every `"<rel>:<version>"` entry in `derived_from`, fetch the
 *     snapshot content via `page-versioning.getVersion` and restore it
 *     to the original relative path.  If the restore target file
 *     already exists, we skip overwriting it (the source was never
 *     archived, or was re-created since) and record the skip.
 *   - Archive the target with reason code `"consolidation-undo"` so the
 *     lifecycle ledger records the undo.
 *   - Dry-run mode produces the same plan without touching disk.
 *
 * The helper is kept pure over `StorageManager` so the CLI can reuse it
 * without additional wiring, and tests can exercise the plan logic
 * directly.
 */

/**
 * Outcome of restoring a single `derived_from` source.
 */
interface ConsolidationUndoRestore {
    /** The raw `"<relpath>:<version>"` entry from `derived_from`. */
    entry: string;
    /** Absolute path where the source would be / was restored. */
    sourcePath: string;
    /** What actually happened. */
    outcome: "restored" | "skipped_file_exists" | "skipped_non_regular_file" | "skipped_snapshot_missing" | "skipped_malformed_entry" | "skipped_outside_memory_dir" | "skipped_non_active_path" | "skipped_self_referential" | "skipped_write_failed" | "skipped_blocked_by_other_failures" | "skipped_dry_run";
    /** Human-readable detail. */
    detail?: string;
}
/**
 * Plan + result of a `remnic consolidate undo` invocation.
 */
interface ConsolidationUndoResult {
    /** Absolute path to the target memory. */
    targetPath: string;
    /** True when the target was archived successfully. */
    targetArchived: boolean;
    /** Per-source restore outcomes. */
    restores: ConsolidationUndoRestore[];
    /** Whether the run was a dry-run plan only. */
    dryRun: boolean;
    /** Fatal error, if any — the run bails early. */
    error?: string;
}
/**
 * Verify that `candidate` resolves inside `root` (defense against
 * path-traversal in `derived_from` entries and user-facing target
 * paths).  Path-string normalization only; for symlink-aware checks
 * use `isInsideDirectoryRealpath`.  Both paths are resolved to
 * absolute form before comparison so `..` segments, symlinks-as-
 * strings, and relative prefixes are normalized.
 */
declare function isInsideDirectory(candidate: string, root: string): boolean;
/**
 * Symlink-aware containment check (PR #637 round-2 review, codex P1).
 *
 * `isInsideDirectory` only normalizes path strings — if a `derived_from`
 * entry resolves through a symlink inside `memoryDir` that points
 * outside, the string check passes but the subsequent `writeFile` would
 * land outside the memory tree.  Use this guard for any path that is
 * about to be written.
 *
 * Walks every parent directory between `candidate` and `root`,
 * `realpath`-ing each segment that exists and rejecting when any
 * segment escapes `root`.  Non-existent parents are resolved as the
 * canonicalized deepest-existing ancestor plus the trailing segments,
 * so a not-yet-created target file still gets the symlink check on its
 * existing parent directories.
 */
declare function isInsideDirectoryRealpath(candidate: string, root: string): Promise<boolean>;
/**
 * Check that a relative path (relative to memoryDir) points to an
 * active memory location rather than an internal/archive directory.
 * Returns `true` when the normalised `pagePath` does NOT start with
 * a known non-active prefix.
 *
 * @param pagePath   Relative path from `derived_from` entry.
 * @param sidecarDir Optional versioning sidecar directory name
 *                   (e.g. `".versions"`).  When provided, paths
 *                   under this directory are also rejected as
 *                   non-active.
 */
declare function isActiveMemoryRelativePath(pagePath: string, sidecarDir?: string): boolean;
/**
 * Perform a consolidation-undo operation.
 *
 * @param options.storage        Storage manager for the memory directory.
 * @param options.memoryDir      Absolute memory directory root.
 * @param options.targetPath     Absolute path to the consolidated memory.
 * @param options.versioning     Page-versioning config (sidecarDir must
 *                               match the sidecar layout used when the
 *                               snapshots were created).
 * @param options.dryRun         When true, compute the plan but do not
 *                               write or archive.
 */
declare function runConsolidationUndo(options: {
    storage: StorageManager;
    memoryDir: string;
    targetPath: string;
    versioning: VersioningConfig;
    dryRun?: boolean;
}): Promise<ConsolidationUndoResult>;
/**
 * Render a consolidation-undo result as a human-readable multi-line
 * string for the CLI.  Extracted so tests can snapshot the formatting
 * without parsing stdout.
 */
declare function formatConsolidationUndoResult(result: ConsolidationUndoResult): string;

export { type ConsolidationUndoRestore, type ConsolidationUndoResult, formatConsolidationUndoResult, isActiveMemoryRelativePath, isInsideDirectory, isInsideDirectoryRealpath, runConsolidationUndo };
