package BookPub::Import::Importer::Christianbook::Version1;

use strict;

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

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

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

sub _fieldMap {
    {
        currencyCode => 'X',
        countryCode  => 'AA',
        rState       => 'G',
        rPostalCode  => 'J',
        dateBegin    => 'D',
        rProductType => 'V',
        rIsbn13      => 'C',
        rTitle       => 'T',
        rAuthor      => 'U',
        rUnits       => 'Y',
        rListPrice   => 'W',
        rRevenue     => 'Z',
    };
}

sub priceType    { 'agency' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub productType {
    my $self = shift;
    my $productType = $self->rProductType() || return;

    return BookPub::DB::Item::Sale::kProductTypeAudioBook if ( $productType =~ /audiobook/i );
    return BookPub::DB::Item::Sale::kProductTypeEBook     if ( $productType =~ /ebook/i );

    return undef;
}

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

    return ( $revenue == 0 && $units > 0 ) ? 'Y' : 'N';
}

sub countryCode {
    my $self = shift;
    my $code = $self->SUPER::countryCode();

    my $countryObj = Common::Country->Get($code) || $self->fail("Unknown country code '$code'");

    return $countryObj->alpha2();
}

1;
