package Logic::Join;

use Logic;

use strict;

sub new {
	my ($package, $a, $b, $c) = @_;

	my $self = bless {
		a => $a,
		b => $b,
		c => $c,
	}, $package;

	Logic::attach($self, $a, $b, $c);

	return $self;
}

sub activate {
	my ($self) = @_;

	my ($a, $b, $c) = @$self{qw(a b c)};

	if (defined $$a && defined $$b) {
		let	$c => "$$a$$b";
	} elsif (defined $$c && defined $$a) {
		my $l = length $$a;
		if ($$a eq substr($$c, 0, $l)) {
			let	$b => substr($$c, $l);
		} else {
			fail;
		}
	} elsif (defined $$c && defined $$b) {
		my $l = length($$c) - length($$b);
		if ($$b eq substr($$c, $l)) {
			let	$a => substr($$c, 0, $l);
		} else {
			fail;
		}
	} elsif (defined $$c) {
		my $n = $self->{n} ++;
		if ($n > length $$c) {
			$self->{n} = 0;
			fail;
		} else {
			guess	$a => substr($$c, 0, $n),
				$b => substr($$c, $n);
		}
	}
}

1
