package BookPub::Import::Importer::RoyaltyShare::Version4;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Util qw(formatFixedPoint);

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

sub _fieldMap {
    {
        rServiceName             => 'B1',
        dateBegin                => 'A',
        dateEnd                  => 'B',
        rPurchaseType            => 'L',
        purchaseType             => 'L',
        rProductType             => 'J',
        rFormat                  => 'K',
        priceType                => 'U',
        priceTypeQualifierID     => 'V',
        rAuthor                  => 'G',
        rTitle                   => 'E',
        rSubtitle                => 'F',
        rIsbn13                  => 'D',
        rImprint                 => 'I',
        rInstitution             => 'P',
        rState                   => 'M',
        rPostalCode              => 'N',
        countryCode              => 'O',
        rUnits                   => 'W',
        rUnitPrice               => 'AD',
        rListPrice               => 'Z',
        rListPriceCurrency       => 'AA',
        rNativeListPrice         => 'Z',
        rNativeListPriceCurrency => 'AA',
        rPurchasePrice           => 'X',
        rPurchasePriceCurrency   => 'Y',
        rDiscount                => 'AB',
        rRevenue                 => 'AG',
        currencyCode             => 'H1',
        rTaxAmount               => 'AC',
        rDuration                => 'Q',
        rDistributor             => 'C',
        rChannel                 => 'U',
        rModel                   => 'T',
        rTransactionCategory     => 'R',
    }
}

sub _unverifiedFieldMap {
    {
        headerUnits   => 'D1',
        headerRevenue => 'F1',
    }
}

sub _parseProductType {
    my ( $self, $format ) = @_;

    return
        $format =~ /eBook/i ? BookPub::DB::Item::Sale::kProductTypeEBook
      : $format =~ /audio/i ? BookPub::DB::Item::Sale::kProductTypeAudioBook
      : $format =~ /pod/i ? BookPub::DB::Item::Sale::kProductTypePhysicalBook
      :                       undef;
}

sub priceTypeQualifierID {
    my $self         = shift;
    my $purchaseType = $self->_getByFieldName('priceTypeQualifierID');

    if ( $purchaseType =~ /^(?:Library Sale|0?6|Corporate)$/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;
    }

    return undef;
}

sub isFree {
    my $self = shift;

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

sub _headerIdentifier { dateBegin => 'Sale Start Date' }

sub _useIsSaleRec     { 1 }
sub _dateFormat       { [ 'numerical_year_first', 'iso' ] }
sub _validTabNames    { ['RS Digital Sales Template'] }

sub _isSaleRec {
    my $self = shift;

    my $sheet = $self->sheetname();
    return unless grep {/$sheet/} @{&_validTabNames};

    if ($self->rUnits() || $self->rRevenue()) {
        $self->rIsbn13 ? return 1 : $self->fail('Missing required sales data');
    }
}

1;
