package BookPub::Import::Importer::OverDrive::Base;

use strict;

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

sub _fieldmap { die "must be overloaded" }

sub _headerIdentifier { ( rIsbn13 => 'eISBN' ) }

sub units        { 1 }
sub rSales       { 1 }
sub currencyCode { 'USD' }
sub countryCode  { 'US' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub dateEnd      { shift->dateBegin() }

sub formatType {
    my $self = shift;
    my $format = $self->rFormat() || return;

    $format =~ /\s(\w+)\saudiobook/i;
    my $type = lc($1);

    if ( $self->productType eq BookPub::DB::Item::Sale::kProductTypeAudioBook ) {
        return $type;
    } else {
        return $self->_parseFormat($format);
    }
}

sub productType {
    my $self = shift;
    my $format = $self->rFormat() || return;

    return BookPub::DB::Item::Sale::kProductTypeAudioBook if ( $format =~ /audiobook/i );
    return BookPub::DB::Item::Sale::kProductTypeEBook     if ( $format =~ /ebook/i );
    return BookPub::DB::Item::Sale::kProductTypeEBook     if ( $format =~ /overdrive read/i );

    return undef;
}

sub rIsbn10 {
    my $self = shift;
    my $isbn = $self->_getByFieldName('rIsbn13') || return;

    return length($isbn) == 10 ? $isbn : undef;
}

sub rIsbn13 {
    my $self = shift;
    my $isbn = $self->_getByFieldName('rIsbn13') || return;

    return length($isbn) == 13 ? $isbn : undef;
}

sub _isValidSaleRecord {
    my $self = shift;

    return $self->dateBegin && $self->rTitle;
}

1;
