package BookPub::Import::Importer::AdamsBookCompany::Version4;

use strict;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::Import::Importer::AdamsBookCompany';

sub _headerIdentifier { ( rIsbn13 => 'ISBN #' ) }

sub _autoParseTitleAndSubtitle { 1 }

sub _processLine {
    my $self = shift;
    my $isbn = $self->rIsbn13();

    if ( defined $isbn && $isbn =~ /Note:/i ) {
        $self->{_endOfData} = 1;
        return undef;
    }

    return $self->SUPER::_processLine(@_);
}

sub _isSaleRec {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

    # Ignore the subtotal line.
    return
      if ( !$self->rIsbn13
        && !$self->rTitle
        && !$self->rAuthor
        && !$self->dateBegin
        && !$self->rInstitution
        && !$self->rState
        && !$self->rPostalCode );

    return $self->SUPER::_isSaleRec();
}

sub rFormat {
    my $self = shift;

    # used cached version, if it exists
    if ( $self->{_rFormat} ) { return $self->{_rFormat} }

    # cache for other records
    my $format = $self->_getByFieldName('rFormat');
    if ( $format =~ /EPUB/i ) {
        $self->{_rFormat} = BookPub::DB::Item::Sale::kFormatTypeEPub;
        return $self->{_rFormat};
    } else {
        $self->fail("Unexpected format $format");
    }
}

sub countryCode {
    my $self = shift;

    # used cached version, if it exists
    if ( $self->{_countryCode} ) { return $self->{_countryCode} }

    # cache for other records
    my $country = $self->_getByFieldName('countryCode');
    my $countryObj = Common::Country->Get($country) || $self->fail("Unknown country code '$country'");
    $self->{_countryCode} = $countryObj->alpha2();
    return $self->{_countryCode};
}

1;
