package RPS::Import::Merlin::v46;
# Merlin/Pulselocker (ATO)

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;

use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        labelName    => 'F',
        countryCode  => 'T',
        dateBegin    => 'A',
        dateEnd      => 'B',
        productType  => 'L',
        formatType   => 'J',
        artistName   => 'K',
        upc          => 'M',
        albumName    => 'N',
        isrc         => 'P',
        trackName    => 'O',
        units        => 'Q',
        currencyCode => 'V1',
        price        => 'V'
    };
}

sub _unverifiedFieldMap {
    {
        salesTypeKey => 'G',
    };
}

sub _headerIdentifier { upc => 'UPC' }
sub serviceID         { Client::Service::DSP_PULSELOCKER }
sub currencyCode      { 'USD' }
sub mediaType         { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';
    if ( $type =~ /^track$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    }

    $self->fail("Unknown productType '$type'");
}

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || '';

    if ( $type =~ /^stream$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    if ( $type =~ /^tethered download$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    }

    $self->fail("Unknown formatType '$type'");
}

sub countryCode {
    my $self = shift;

    my $country = $self->_getByFieldName('countryCode');
    my $oCountry = Common::Country->new( search => $country );
    return $oCountry->alpha2() if $oCountry;

    $self->fail("Unable to determine country code from '$country'");
}

sub free {
    my $self = shift;

    my $salesTypeKey = $self->_getByFieldName('salesTypeKey') || '';
    if ( !$self->price && $salesTypeKey =~ /^promo$/i ) {
        return 1;
    }

    return 0;
}


1;
