package BookPub::Import::Importer::Baobab::v1;

use strict;
use warnings;

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

sub _fieldMap {
    # SERVICE: 223, VERSION: 1
    {
        rServiceName         => 'A1',
        dateBegin            => 'E',
        dateEnd              => 'E',
        purchaseType         => 'O',
        rTitle               => 'C',
        rIsbn13              => 'B',
        countryCode          => 'M',
        rReturns             => 'G',
        rUnits               => 'F',
        rListPrice           => 'J',
        rListPriceCurrency   => 'N',
        rDiscount            => 'K',
        rRevenue             => 'L',
        currencyCode         => 'N',
        rModel               => 'O',
        rTransactionCategory => 'A',
    };
}

sub _unverifiedFieldMap {
    {
        _rListPriceExtra => 'I',
    };
}

sub _headerIdentifier    { rTitle => 'Title' }
sub _useIsSaleRec        { 1 }
sub priceType            { 'rrp' }
sub rServiceName         { 'Baobab eBook Services' }
sub productType          { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType         { BookPub::DB::Item::Sale::kProductTypeEBook }
sub dateEnd              { shift->dateBegin }

sub dateBegin {
    my $self = shift;

    my $startDate = $self->_getByFieldName('dateBegin');

    if ( $startDate =~ /^\d{5}$/ ) {
        $startDate = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $startDate );
    }
    return $startDate;
}

sub purchaseType {
    my $self = shift;

    my $type = $self->_getByFieldName('purchaseType') || '';
    if ( $type =~ /^Perpetual Access$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }

    $self->fail("Can't determine purchase type: $type");
}

sub rListPrice {
    my $self = shift;

    my $price = $self->_getByFieldName('rListPrice') || $self->_getByFieldName('_rListPriceExtra');
    return $price;
}

sub _isSaleRec {
    my $self = shift;

    return if $self->{_endOfData};

    unless ( $self->SUPER::_isSaleRec ) {
        $self->{_endOfData} = 1;
        return;
    }

    return 1;
}


1;