package BookPub::Import::Importer::JBHiFi;

use strict;

use lib '/app/tools/common/lib';
use Common::Country;

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

sub _fieldMap {
    {
        # Detail
        rPriceType         => 'X',
        currencyCode       => 'G',
        conversionRate     => 'AO',
        countryCode        => 'U',
        dateBegin          => 'I',
        rFormat            => 'Q',
        rIsbn13            => 'L',
        rTitle             => 'M',
        rAuthor            => 'N',
        rUnits             => 'T',
        rSales             => 'R',
        rReturns           => 'S',
        rListPrice         => 'V',
        rListPriceCurrency => 'Y',
        rDiscount          => 'Z',
        rRevenue           => 'AD',
    };
}

sub _headerIdentifier { ( rTitle => 'Product title' ) }

sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub _dateFormat   { ['iso'] }
sub _useIsSaleRec { 1 }

sub dateBegin {
    my $self    = shift;
    my $dateStr = $self->_getByFieldName('dateBegin');
    my $date;

    # date is in format YYYYMMDDTHHMMSS, note the T that we need to strip
    if ( $dateStr =~ /^(\d{4})(\d{2})(\d{2})T(\d{6})$/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    } else {
        $date = undef;
    }

    return $date;
}

sub dateEnd {
    my $self = shift;

    return $self->dateBegin();
}

sub priceType {
    my $self = shift;
    my $type = lc( $self->rPriceType ) || return;

    if ( $type == 42 ) {
        return 'agency';
    }

    return undef;
}

1;
