/**
 * MountResolver - Resolves relative mount paths to absolute paths.
 *
 * Mount format: dot-delimited component path (no root, no scheme).
 *
 * Supports explicit relative addressing syntax:
 *   ./component      → sibling (parent context + component)
 *   ../component     → uncle (grandparent context + component)
 *   component        → absolute (pass through)
 */

/**
 * Resolve a relative mount path to an absolute path.
 *
 * Patterns:
 *   ./component      → sibling (parent context + component)
 *   ../component     → uncle (grandparent context + component)
 *   component        → absolute (pass through)
 *
 * Examples with basePath = "app.db":
 *   ./cache  → "app.cache"
 *   ../tree  → "tree"
 *   other    → "other"
 */
export function resolveRelativeMount(
  basePath: string,
  relativePath: string
): string {
  if (relativePath.startsWith('../')) {
    const segments = basePath.split('.');
    segments.pop(); // Remove current component
    segments.pop(); // Remove parent
    const remaining = relativePath.slice(3);
    return segments.length > 0
      ? [...segments, remaining].join('.')
      : remaining;
  }
  if (relativePath.startsWith('./')) {
    const segments = basePath.split('.');
    segments.pop(); // Remove current component
    const remaining = relativePath.slice(2);
    return segments.length > 0
      ? [...segments, remaining].join('.')
      : remaining;
  }
  return relativePath; // Absolute — pass through
}

/**
 * Check if a mount address is explicitly relative (uses ./ or ../ syntax).
 */
export function isRelativeMount(mount: string): boolean {
  return mount.startsWith('./') || mount.startsWith('../');
}
