package BookPub::Import::Importer::VitalSource::Version23;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use parent 'BookPub::Import::Importer::VitalSource::Version21';

# This importer is former Capella v4.
# It was renamed to Vital Source as part of RSD-6811

sub _fieldMap {
    my $self = shift;

    my %map = (
        23 => {
            rServiceName         => 'G',
            dateBegin            => 'B',
            dateEnd              => 'B',
            rProductType         => 'T',
            priceType            => 'AF',
            rAuthor              => 'AB',
            rTitle               => 'AA',
            rIsbn13              => 'Y',
            rInstitution         => 'G',
            rState               => 'K',
            rPostalCode          => 'J',
            countryCode          => 'L',
            serviceProductID     => 'Z',
            rUnits               => 'AE',
            rUnitPrice           => 'AJ',
            rListPrice           => 'AG',
            rListPriceCurrency   => 'AI',
            rRevenue             => 'AH',
            currencyCode         => 'AI',
            rTaxAmount           => 'AL',
            rFee                 => 'AK',
            rDuration            => 'AD',
            rDeliveryMethod      => 'M',
            rTransactionCategory => 'C',
        },
        24 => {
            dateBegin            => 'B',
            dateEnd              => 'B',
            rProductType         => 'T',
            #priceType            => 'AF',
            rAuthor              => 'AB',
            rTitle               => 'AA',
            rIsbn13              => 'Y',
            rInstitution         => 'G',
            rState               => 'K',
            rPostalCode          => 'J',
            countryCode          => 'L',
            serviceProductID     => 'Z',
            rUnits               => 'AE',
            rUnitPrice           => 'AI',
            rListPrice           => 'AF',
            rListPriceCurrency   => 'AH',
            rRevenue             => 'AG',
            currencyCode         => 'AH',
            rTaxAmount           => 'AK',
            rFee                 => 'AJ',
            rDuration            => 'AD',
            rDeliveryMethod      => 'M',
            rTransactionCategory => 'C',
        },
    );

    return $map{ $self->version };
}

sub rServiceName { 'Vital Source' }

sub rProductType   { shift->_getByFieldName('rProductType') }
sub productType  {
    my $self = shift;

    my $type = $self->_getByFieldName('rProductType') || '';
    return BookPub::DB::Item::Sale::kProductTypeEBook if $type =~ /^(?:book|custom)$/i;

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

sub priceType {
    my $self = shift;

    my $type = $self->_getByFieldName('priceType');
    return 'rrp' if !$type || $type =~ /^(?:Custom|DLP|Fixed)$/i;

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

sub rDuration {
    my $self = shift;

    my $rDuration = $self->_getByFieldName('rDuration');

    if ($rDuration =~ /\d{5}/) {
        $self->fail("Invalid rDuration: '$rDuration'.");
    }

    return $rDuration;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

1;
