package BookPub::Import::Importer::Bibliu::v3;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use parent 'BookPub::Import::Importer::Bibliu::v2';

sub _fieldMap {
    {
        dateBegin              => 'S',
        dateEnd                => 'S',
        purchaseType           => 'R',
        rFormat                => 'F',
        rTitle                 => 'C',
        rIsbn13                => 'E',
        rInstitution           => 'T',
        countryCode            => 'V',
        rReturns               => 'H',
        rSales                 => 'G',
        rUnits                 => 'I',
        rUnitPrice             => 'P',
        rListPrice             => 'K',
        rListPriceCurrency     => 'J',
        rPurchasePrice         => 'N',
        rPurchasePriceCurrency => 'J',
        rDiscount              => 'O',
        rRevenue               => 'Q',
        currencyCode           => 'J',
        rDuration              => 'R',
        rModel                 => 'W'
    };
}

sub purchaseType {
    my $self = shift;

    my $type = $self->_getByFieldName('purchaseType') || '';
    if ( $type =~ /^Perpetual$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    } elsif ( $type =~ /^\d{2,3} (?:month|day)s?$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    }

    $self->fail("Unable to determine purchase type from '$type'.");
}

sub rFormat {
    my $self = shift;

    my $rFormat = $self->_getByFieldName('rFormat') || '';
    return 'ePub' if $rFormat =~ /^Ebook$/i;
}

sub isFree {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || 0;
    my $units = $self->_getByFieldName('rUnits') || 0;

    return "Y" if ($revenue == 0 && $units != 0);
    return "N";
}

1;
