package BookPub::Import::Importer::MBSTEX::Version1;

use strict;

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

sub _fieldMap {
    {
        dateBegin        => 'P',
        dateEnd          => 'P',
        rAuthor          => 'L',
        rTitle           => 'M',
        isbn             => 'U',
        rInstitution     => 'C',
        rState           => 'I',
        rPostalCode      => 'J',
        serviceProductID => 'K',
        rUnits           => 'O',
        rListPrice       => 'N',
        rRevenue         => 'S',
    };
}

sub _headerIdentifier { ( rTitle => 'Title' ) }
sub priceType         { 'rrp' }
sub purchaseType      { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType       { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rServiceName      { 'MBS' }
sub countryCode       { 'US' }
sub currencyCode      { 'USD' }
sub _useIsSaleRec     { 1 }

sub rTitle {
    my $self  = shift;
    my $title = $self->_getByFieldName('rTitle');

    $title =~ s/^ebk //i;
    return $title;
}

sub postalCode {
    my $self = shift;
    my $zip  = $self->rPostalCode;

    # Only zip post codes that look like #####, #####-#### or #########
    # and return just the 5-digit portion
    if ( $zip =~ m/^(\d\d\d\d\d)(?:\-?\d\d\d\d)?$/ ) { return $1; }

    # Everything else is a failure state
    $self->fail("Invalid postal code: $zip");
}

sub _isSaleRec {
    my $self = shift;
    return 0 unless $self->SUPER::_isSaleRec;    # Bail out early on blank rows
    return 0 unless $self->rTitle;               # Required field or bail
    return 1;
}

1;
