# Copyright (c) 2023 Yuki Kimoto
# MIT License

class Cwd {
  version_from File::Spec;
  
  use Fn;
  use Sys::IO;
  use Sys;
  use File::Spec;
  use Sys::OS;

  static method getcwd : string () {
    my $cwd = Sys::IO->getcwd(undef, 0);
    
    if (Sys::OS->is_windows) {
      Fn->replace_chars($cwd, '\\', '/');
    }
    
    return $cwd;
  }
  
  static method realpath : string ($file : string) {
    return Sys->_realpath($file);
  }
  
  static method abs_path : string ($file :string) { return &realpath($file); }
  
  static method getdcwd : string ($drive : string = undef) {
    unless (Sys::OS->is_windows) {
      die Error::NotSupported "The getdcwd is not supported on this system(_WIN32)";
    }
    my $drive_id = 0;
    if ($drive) {
      unless (length $drive == 2) {
        die "The length of the \$drive must be 2";
      }
      
      my $drive_char = $drive->[0];
      unless (($drive_char >= 'A' && $drive_char <= 'Z') || ($drive_char >= 'a' && $drive_char <= 'z')) {
        die "The first character of the \$drive must be [a-zA-Z]";
      }
      unless ($drive->[1]) {
        die "The second character of the \$drive must be \":\"";
      }
      
      my $drive_upper = Fn->ucfirst($drive);
      
      $drive_id = $drive_char - 'A' + 1;
    }
    
    my $cwd = Sys::IO->_getdcwd($drive_id, undef, 1);
    
    Fn->replace_chars($cwd, '\\', '/');
    
    return $cwd;
  }
}