package System::Introspector::Report::Publish::MediaWiki::Page;
use Moo;

use aliased 'System::Introspector::Report::Publish::MediaWiki::Parser';
use aliased 'System::Introspector::Report::Publish::MediaWiki::Producer';
use aliased 'System::Introspector::Report::Updater';

has name      => (is => 'ro', required => 1);
has timestamp => (is => 'ro', predicate => 'has_timestamp');
has content   => (is => 'rwp', required => 1);

sub is_new { not $_[0]->has_timestamp }

my $_producer = Producer->new;
my $_parser   = Parser->new;
my $_updater  = Updater->new;

my $_trim = sub {
  my ($body) = @_;
  $body =~ s{^\s+}{};
  $body =~ s{\s+$}{};
  return $body;
};

sub update {
  my ($self, $update_stream) = @_;
  my $content_before  = $self->content;
  my $original_stream = $_parser->parse($content_before);
  my $merged_stream   = $_updater->merge($original_stream, $update_stream);
  my $content_updated = $_producer->render($merged_stream);
  if ($content_updated->$_trim eq $content_before->$_trim) {
    return 0;
  }
  $self->_set_content($content_updated);
  return 1;
}

1;
