package BAST::Frame;

use strict;
use warnings;
use base qw/Catalyst::Request/;
use XML::LibXML;

__PACKAGE__->mk_accessors(qw/status/);

*output = __PACKAGE__->can('body');

sub create {
  my ($class, $path, $body) = @_;
  my $body_type = (ref $body ? 'document' : 'body');
  return $class->new(
                {
                    arguments  => [],
                    cookies    => {},
                    headers    => HTTP::Headers->new,
                    parameters => {},
                    snippets   => [],
                    uploads    => {},
                    path => $path,
                    $body_type => $body,
                } );
}

sub document {
  my $self = shift;
  if (@_) {
    $self->set('document', @_);
    $self->set('body', undef);
  } else {
    my $doc = $self->get('document');
    return $doc if defined $doc;
    unless (length $self->get('body')) { return }
    my $parser = XML::LibXML->new();
    $doc = $parser->parse_string($self->get('body'));
    $self->set('document', $doc);
    return $doc;
  }
}

sub body {
  my $self = shift;
  if (@_) {
    $self->set('body', @_);
    $self->set('document', undef);
  } else {
    my $body = $self->get('body');
    return $body if defined $body;
    unless (ref $self->get('document')) { return }
    $body = $self->get('document')->toString;
    $self->set('body', $body);
    return $body;
  }
}

1;
