### ----------------------------------------------------
### -- ASTQuery
### -- Licenses: Artistic-2.0
### -- Authors: Fernando Corrêa de Oliveira
### -- File: lib/ASTQuery/Matcher.rakumod
### ----------------------------------------------------
### -- Chunk 2 of 2
	self.validate-class: $node, ::($class)
}

multi method validate-class($node, Mu:U $class) {
	print-validator-begin $node, $class;
	POST print-validator-end $node, $class, $_;
	do if $node ~~ $class {
		ASTQuery::Match.new: :list[$node]
	} else {
		False
	}
}

multi method validate-class($node, @classes) {
	print-validator-begin $node, @classes;
	POST print-validator-end $node, @classes, $_;
	my %done := :{};
	@classes.flatmap(-> $class {
		next if %done{$class}++;
		self.validate-class: $node, $class
	}).first(*.so) // False
}

multi method validate-ids($node, @ids) {
	print-validator-begin $node, @ids;
	POST print-validator-end $node, @ids, $_;
	my $key = self.get-id-field: $node;
	return False unless $key;
	@ids.unique.map(-> $id {
		self.validate-atts: $node, %($key => $id)
	}).first(*.so) // False
}

multi method validate-ids($node, $id) {
	print-validator-begin $node, $id;
	POST print-validator-end $node, $id, $_;
	self.validate-ids: $node, [$id,]
}

method validate-atts($node, %atts) {
	print-validator-begin $node, %atts;
	POST print-validator-end $node, %atts, $_;
	return ASTQuery::Match.new: :list[$node,]
		if ASTQuery::Match.merge-and: |%atts.kv.map: -> $key, $value is copy {
			$value = $value.($node) if $value ~~ Callable;
			self.validate-value: $node, $key, $value;
		}
	;
	False
}

multi method validate-value($node, $key, $ where * =:= False) {
	print-validator-begin $node, True;
	POST print-validator-end $node, True, $_;
	False
}

multi method validate-value($node, $key, $ where * =:= True) {
	print-validator-begin $node, True;
	POST print-validator-end $node, True, $_;
	ASTQuery::Match.new: :list[$node,]
}

multi method validate-value($node, $key, $value) {
	print-validator-begin $node, $value;
	POST print-validator-end $node, $value, $_;
	do if $node.^name.starts-with("RakuAST") && $value !~~ ::?CLASS {
		return False unless $key;
		return False unless $node.^can: $key; # it can be a problem if $key is 'none', for example
		my Any $nnode = $node."$key"();
		self.validate-value($nnode, $.get-id-field($nnode), $value)
	} else { 
		$value.ACCEPTS($node) && ASTQuery::Match.new(:list[$node,])
	} || False
}


