package BookPub::Import::Importer::Bookt::v1;

use strict;
use warnings;

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


sub _fieldMap {
    {
        dateBegin          => 'A',
        priceType          => 'I',
        rTitle             => 'D',
        rIsbn13            => 'C',
        rPostalCode        => 'G',
        rUnits             => 'F',
        rListPrice         => 'I',
        rListPriceCurrency => 'H',
        rDiscount          => 'K',
        rRevenue           => 'L',
        currencyCode       => 'H',
        rModel             => 'E',
    }
}

sub _headerIdentifier { rTitle => 'Title' }

sub rServiceName  { 'Bookt' }
sub priceType     { 'rrp' }
sub _useIsSaleRec { 1 }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub countryCode   { 'US' }

my %months = (
    'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6,
    'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12,
);

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';

    # Mon Oct 02 2023
    if ( my($m, $d, $y) = $date =~ /(\w{3}).(\d{1,2}).(\d{4})$/) {
        return sprintf "%04d-%02d-%02d", $y, $months{lc($m)}, $d;

    }
}
sub isFree {
    my $self = shift;

    return (((!$self->rRevenue || $self->rRevenue =~ /^0.0+/) && $self->rUnits) ? 'Y' : 'N');
}

1;
