//! Cross-crate path normalization helpers.

use std::path::{Path, PathBuf};

/// Strip a Windows `\\?\` verbatim drive prefix from `path` so callers
/// that interpolate it into a path-concatenating template (`%~dp0\{rel}`
/// in the bin-shim generator, `starts_with` ownership checks in
/// `scan_packages` / `remove_package`, `CreateDirectoryW` calls in the
/// linker) all see the plain `C:\…` form.
///
/// `\\?\UNC\server\share\…` identifies a real network share and has no
/// non-verbatim equivalent — callers that strip it would produce an
/// unresolvable drive-rooted path, so leave UNC verbatim prefixes
/// intact.
///
/// No-op on non-Windows.
pub fn strip_verbatim_prefix(path: &Path) -> PathBuf {
    #[cfg(windows)]
    {
        let s = path.to_string_lossy();
        if let Some(rest) = s.strip_prefix(r"\\?\")
            && !rest.starts_with("UNC\\")
        {
            return PathBuf::from(rest);
        }
    }
    path.to_path_buf()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(windows)]
    #[test]
    fn strips_verbatim_drive_prefix() {
        let p = Path::new(r"\\?\C:\Users\foo");
        assert_eq!(strip_verbatim_prefix(p), PathBuf::from(r"C:\Users\foo"));
    }

    #[cfg(windows)]
    #[test]
    fn preserves_verbatim_unc_share() {
        let p = Path::new(r"\\?\UNC\server\share\foo");
        assert_eq!(
            strip_verbatim_prefix(p),
            PathBuf::from(r"\\?\UNC\server\share\foo")
        );
    }

    #[cfg(windows)]
    #[test]
    fn leaves_plain_drive_path_unchanged() {
        let p = Path::new(r"C:\Users\foo");
        assert_eq!(strip_verbatim_prefix(p), PathBuf::from(r"C:\Users\foo"));
    }

    #[cfg(not(windows))]
    #[test]
    fn is_noop_on_unix() {
        let p = Path::new("/home/foo");
        assert_eq!(strip_verbatim_prefix(p), PathBuf::from("/home/foo"));
    }
}
