# Copyright (c) 2023 Yuki Kimoto
# MIT License

class Regex::Match {
  version_from Regex;
  
  allow Regex;
  
  use Hash;
  use Array;
  
  has success : ro byte;
  has captures : string[];
  has match_start : ro int;
  has match_length : ro int;
  
  # Class Method
  static method new : Regex::Match ($options : object[] = undef) {
    
    my $self = new Regex::Match;
    
    my $options_h = Hash->new($options);
    
    $self->{success} = (byte)$options_h->delete_or_default_int("success", 0);
    
    $self->{match_start} = $options_h->delete_or_default_int("match_start", -1);
    
    $self->{match_length} = $options_h->delete_or_default_int("match_length", -1);
    
    my $captures = (string[])$options_h->delete_or_default("captures", new string[0]);
    
    $self->{captures} = Array->copy_string_address($captures);
    
    for my $name (@{$options_h->keys}) {
      die "The \"$name\" option is not supported";
    }
    
    return $self;
  }
  
  private static method _new : Regex::Match ($success : int, $captures : string[], $match_start : int, $match_length : int) {
    my $self = &new({
      success => $success,
      captures => $captures,
      match_start => $match_start,
      match_length => $match_length,
    });
    
    return $self;
  }
  
  # Instance Method
  method captures : string ($index : int) {
    return $self->{captures}->[$index];
  }
  
  method captures_length : int () {
    return @{$self->{captures}};
  }
  
  method cap1 : string () { return $self->{captures}->[1]; }
  
  method cap2 : string () { return $self->{captures}->[2]; }
  
  method cap3 : string () { return $self->{captures}->[3]; }
  
  method cap4 : string () { return $self->{captures}->[4]; }
  
  method cap5 : string () { return $self->{captures}->[5]; }
  
  method cap6 : string () { return $self->{captures}->[6]; }
  
  method cap7 : string () { return $self->{captures}->[7]; }
  
  method cap8 : string () { return $self->{captures}->[8]; }
  
  method cap9 : string () { return $self->{captures}->[9]; }
  
  method cap10 : string () { return $self->{captures}->[10]; }
  
  method cap11 : string () { return $self->{captures}->[11]; }
  
  method cap12 : string () { return $self->{captures}->[12]; }
  
  method cap13 : string () { return $self->{captures}->[13]; }
  
  method cap14 : string () { return $self->{captures}->[14]; }
  
  method cap15 : string () { return $self->{captures}->[15]; }
  
  method cap16 : string () { return $self->{captures}->[16]; }
  
  method cap17 : string () { return $self->{captures}->[17]; }
  
  method cap18 : string () { return $self->{captures}->[18]; }
  
  method cap19 : string () { return $self->{captures}->[19]; }
  
  method cap20 : string () { return $self->{captures}->[20]; }
}