package BookPub::Import::Importer::VitalSource::Version3;

use strict;

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

sub _headerIdentifier { ( rTitle => 'TITLE' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        3 => {
            currencyCode => 'M',
            dateBegin    => 'A',
            rIsbn13      => 'C',
            rTitle       => 'D',
            rUnits       => 'I',
            rListPrice   => 'J',
            rDiscount    => 'K',
            rRevenue     => 'L',
        },
        8 => {
            dateBegin            => 'A',
            purchaseType         => 'E',
            rAuthor              => 'G',
            rTitle               => 'F',
            rIsbn13              => 'C',
            rUnits               => 'K',
            rListPrice           => 'L',
            rListPriceCurrency   => 'O',
            rDiscount            => 'M',
            rRevenue             => 'N',
            currencyCode         => 'O',
            rDuration            => 'E',
            rTransactionCategory => 'I',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub priceType                  { 'rrp' }
sub countryCode                { 'US' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub purchaseType {
    my $self = shift;

    if ( defined $self->_getByFieldName('purchaseType') ) {
        my $purchaseType = $self->_getByFieldName('purchaseType');

        if ($purchaseType) {
            return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
        }
    }

    return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
}

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin') || return;

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    return $date

}

###
1;    # Play nicely.
###
