from __future__ import annotations

from pathlib import PurePosixPath

def split_remote_path(value: str, file_id: str = '') -> tuple[str, str]:
  raw = (value or file_id).replace('\\', '/')
  parts = [
    part for part in PurePosixPath(raw).parts
    if part not in ('', '.', '..', '/')
  ]
  if not parts:
    return file_id, ''
  return parts[-1], '/'.join(parts[:-1])

def join_remote_path(file_name: str, file_path: str | None = None, file_id: str = '') -> str:
  name, embedded_path = split_remote_path(file_name, file_id)
  clean_path = split_remote_path(f'{file_path or ""}/placeholder')[1] if file_path else embedded_path
  return f'{clean_path}/{name}' if clean_path else name
