package BookPub::Import::Importer::Bookmate;

use strict;
use Spreadsheet::ParseExcel::Utility;

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

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

sub _fieldMap {
    {
        my $self = shift;

        my %fieldMap = (
            1 => {
                currencyCode           => 'N',
                countryCode            => 'E',
                dateBegin              => 'F',
                rIsbn13                => 'A',
                rTitle                 => 'B',
                rAuthor                => 'C',
                rListPrice             => 'H',
                rListPriceCurrency     => 'I',
                rPurchasePrice         => 'K',
                rPurchasePriceCurrency => 'L',
                rDiscount              => 'J',
                rRevenue               => 'M',
            },
        );

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

sub _headerIdentifier { ( rIsbn13 => 'ISBN' ) }
sub _autoParseTitleAndSubtitle { 1 }
sub _useIsSaleRec              { 1 }
sub _dateFormat                { [ 'eur', 'iso' ] }

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeLoan }
sub priceType    { 'rrp' }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub units { 1 }

sub _getDateBegin {
    my $date = shift->SUPER::_getDateBegin() || return;

    # time is also being reported in this field, we just need the date off the front.
    my $d = substr( $date, 0, index( $date, ' ' ) );

    return $d;
}

sub countryCode {
    my $self        = shift;
    my $countryCode = $self->_getByFieldName('countryCode');
    if ( $countryCode =~ /world/i ) {
        return 'RU';
    } else {
        return $countryCode;
    }
}

1;
