package BookPub::Import::Importer::Storytel::Version2;

use strict;
use Date::Calc qw(Days_in_Month);

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

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my $sheet = lc( $self->sheetname ) || return {};
    return {} if ( $sheet =~ /(invoice)/i );

    my %fieldMap = (
        2 => {
            rAuthor                => 'A',
            rTitle                 => 'B',
            rIsbn13                => 'C',
            rUnits                 => 'E',
            rListPrice             => 'E1',
            rListPriceCurrency     => 'F1',
            rPurchasePrice         => 'C1',
            rPurchasePriceCurrency => 'D1',
            rDiscount              => 'D',
            rRevenue               => 'G',
            currencyCode           => 'G4',
        },
    );

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

sub productType { BookPub::DB::Item::Sale::kProductTypeAudioBook }

sub rDiscount {
    my $self     = shift;
    my $discount = $self->_getByFieldName('rDiscount');

    $discount =~ /(\d{1,2})/;
    my $tmp = $1 / 100;

    return $tmp;
}

sub rUnits {
    my $self  = shift;
    my $units = $self->_getByFieldName('rUnits');

    $units = sprintf( "%.0f", $units );
    return $units;
}

sub currencyCode {
    my $self  = shift;

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

    return $currencyCode =~ /\((\w+)\)/;
}
1;
