# Copyright (c) 2023 Yuki Kimoto
# MIT License

class File::Basename::Instance::Win32 extends File::Basename::Instance::Unix {
  use Regex;
  
  our $RE_FILEPARSE1 : Regex;
  our $RE_FILEPARSE2 : Regex;
  our $RE_STRIP_TRAILING_SEP : Regex;
  
  INIT {
    Regex->INIT;
    $RE_FILEPARSE1 = Regex->new("(?s)^((?:.*[:\\\/])?)(.*)");
    $RE_FILEPARSE2 = Regex->new("[\\\/]\z");
    $RE_STRIP_TRAILING_SEP = Regex->new("([^:])[\\\/]*\z");
  }
  
  # Class Methods
  static method new : File::Basename::Instance::Win32 () {
    
    my $self = new File::Basename::Instance::Win32;
    
    return $self;
  }
  
  method fileparse : string[] ($path : string) {
    unless ($path) {
      die "The \$path must be defined";
    }

    my $dirpath = (string)undef;
    my $basename = (string)undef;
    
    if (my $match = $RE_FILEPARSE1->match($path)) {
      $dirpath = $match->cap1;
      $basename = $match->cap2;
    }
    
    unless ($RE_FILEPARSE2->match($dirpath)) {
      $dirpath .= ".\\";
    }
    
    return [$basename, $dirpath, ""];
  }
  
  method _strip_trailing_sep : string ($path : string)  {
    
    $path = $RE_STRIP_TRAILING_SEP->replace($path, method : string ($re : Regex, $match : Regex::Match) {
      return $match->cap1;
    });
    
    return $path;
  }
}