"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const utils_1 = require("./utils");
const versioning_info_1 = require("./versioning-info");
const syncAudiencesToDSP_1 = __importDefault(require("./syncAudiencesToDSP"));
const destination = {
    name: 'Amazon Ads DSP and AMC',
    slug: 'actions-amazon-amc',
    mode: 'cloud',
    authentication: {
        scheme: 'oauth2',
        fields: {
            region: {
                label: 'Region',
                description: 'Region for API Endpoint, either NA, EU, FE.',
                choices: [
                    { label: 'North America (NA)', value: 'https://advertising-api.amazon.com' },
                    { label: 'Europe (EU)', value: 'https://advertising-api-eu.amazon.com' },
                    { label: 'Far East (FE)', value: 'https://advertising-api-fe.amazon.com' }
                ],
                default: 'https://advertising-api.amazon.com',
                type: 'string',
                required: true
            }
        },
        testAuthentication: async (request, { auth, settings }) => {
            if (!auth?.accessToken) {
                throw new actions_core_1.InvalidAuthenticationError('Please authenticate via Oauth before enabling the destination.');
            }
            try {
                await request(`${settings.region}/${versioning_info_1.AMAZON_AMC_AUTH_API_VERSION}/profiles`, {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    timeout: 15000
                });
            }
            catch (e) {
                const error = e;
                if (error.message === 'Unauthorized') {
                    throw new actions_core_1.InvalidAuthenticationError('Invalid Amazon Oauth access token. Please reauthenticate to retrieve a valid access token before enabling the destination.');
                }
                throw e;
            }
        },
        refreshAccessToken: async (request, { auth, settings }) => {
            const authToken = await (0, utils_1.getAuthToken)(request, settings, auth);
            return { accessToken: authToken };
        }
    },
    extendRequest({ auth }) {
        return {
            headers: {
                authorization: `Bearer ${auth?.accessToken}`,
                'Amazon-Advertising-API-ClientID': process.env.ACTIONS_AMAZON_AMC_CLIENT_ID || ''
            }
        };
    },
    audienceFields: {
        description: {
            type: 'string',
            label: 'Description',
            required: true,
            description: 'The audience description. Must be an alphanumeric, non-null string between 0 to 1000 characters in length.'
        },
        countryCode: {
            type: 'string',
            label: 'Country Code',
            required: true,
            description: 'A String value representing ISO 3166-1 alpha-2 country code for the members in this audience.'
        },
        externalAudienceId: {
            type: 'string',
            label: 'External Audience Id',
            required: true,
            description: 'The user-defined audience identifier.'
        },
        cpmCents: {
            label: 'CPM Cents',
            type: 'number',
            description: `Cost per thousand impressions (CPM) in cents. For example, $1.00 = 100 cents.`
        },
        currency: {
            label: 'Currency',
            type: 'string',
            description: `Currency code for the CPM value.`,
            choices: [
                { value: 'USD', label: 'USD' },
                { value: 'CAD', label: 'CAD' },
                { value: 'JPY', label: 'JPY' },
                { value: 'GBP', label: 'GBP' },
                { value: 'EUR', label: 'EUR' },
                { value: 'SAR', label: 'SAR' },
                { value: 'AUD', label: 'AUD' },
                { value: 'AED', label: 'AED' },
                { value: 'CNY', label: 'CNY' },
                { value: 'MXN', label: 'MXN' },
                { value: 'INR', label: 'INR' },
                { value: 'SEK', label: 'SEK' },
                { value: 'TRY', label: 'TRY' }
            ],
            default: ''
        },
        ttl: {
            type: 'number',
            label: 'Time-to-live',
            required: false,
            description: 'Time-to-live in seconds. The amount of time the record is associated with the audience.'
        },
        advertiserId: {
            label: 'Advertiser ID',
            description: 'Advertiser Id',
            type: 'string',
            required: true
        }
    },
    audienceConfig: {
        mode: {
            type: 'synced',
            full_audience_sync: false
        },
        async createAudience(request, createAudienceInput) {
            const { audienceName, audienceSettings, settings } = createAudienceInput;
            const endpoint = settings.region;
            const description = audienceSettings?.description;
            const advertiser_id = audienceSettings?.advertiserId;
            const external_audience_id = audienceSettings?.externalAudienceId;
            const country_code = audienceSettings?.countryCode;
            const ttl = audienceSettings?.ttl;
            const currency = audienceSettings?.currency;
            const cpm_cents = audienceSettings?.cpmCents;
            if (!advertiser_id) {
                throw new actions_core_1.IntegrationError('Missing advertiserId Value', 'MISSING_REQUIRED_FIELD', 400);
            }
            if (!description) {
                throw new actions_core_1.IntegrationError('Missing description Value', 'MISSING_REQUIRED_FIELD', 400);
            }
            if (!external_audience_id) {
                throw new actions_core_1.IntegrationError('Missing externalAudienceId Value', 'MISSING_REQUIRED_FIELD', 400);
            }
            if (!audienceName) {
                throw new actions_core_1.IntegrationError('Missing audienceName Value', 'MISSING_REQUIRED_FIELD', 400);
            }
            if (!country_code) {
                throw new actions_core_1.IntegrationError('Missing countryCode Value', 'MISSING_REQUIRED_FIELD', 400);
            }
            if (ttl && ttl > utils_1.TTL_MAX_VALUE) {
                throw new actions_core_1.IntegrationError(`TTL must have value less than or equal to ${utils_1.TTL_MAX_VALUE}`, 'INVALID_INPUT', 400);
            }
            const payload = {
                name: audienceName,
                description: description,
                targetResource: {
                    advertiserId: advertiser_id
                },
                metadata: {
                    externalAudienceId: external_audience_id
                },
                countryCode: country_code
            };
            if (ttl) {
                payload.metadata.ttl = ttl;
            }
            if (cpm_cents && currency) {
                payload.metadata.audienceFees = [];
                payload.metadata?.audienceFees.push({
                    currency,
                    cpmCents: cpm_cents
                });
            }
            let payloadString = JSON.stringify(payload);
            payloadString = payloadString.replace(utils_1.REGEX_ADVERTISERID, '"advertiserId":$1');
            const response = await request(`${endpoint}/amc/audiences/metadata`, {
                method: 'POST',
                body: payloadString,
                headers: {
                    'Content-Type': 'application/vnd.amcaudiences.v1+json'
                },
                timeout: 15000
            });
            const res = await response.text();
            const resp = (0, utils_1.extractNumberAndSubstituteWithStringValue)(res, utils_1.REGEX_AUDIENCEID, '"audienceId":"$1"');
            return {
                externalId: resp.audienceId
            };
        },
        async getAudience(request, getAudienceInput) {
            const audience_id = getAudienceInput.externalId;
            const { settings } = getAudienceInput;
            const endpoint = settings.region;
            if (!audience_id) {
                throw new actions_core_1.IntegrationError('Missing audienceId value', 'MISSING_REQUIRED_FIELD', 400);
            }
            const response = await request(`${endpoint}/amc/audiences/metadata/${audience_id}`, {
                method: 'GET',
                timeout: 15000
            });
            const res = await response.text();
            const resp = (0, utils_1.extractNumberAndSubstituteWithStringValue)(res, utils_1.REGEX_AUDIENCEID, '"audienceId":"$1"');
            return {
                externalId: resp.audienceId
            };
        }
    },
    actions: {
        syncAudiencesToDSP: syncAudiencesToDSP_1.default
    },
    presets: [
        {
            name: 'Entities Audience Membership Changed',
            partnerAction: 'syncAudiencesToDSP',
            mapping: {
                ...(0, actions_core_1.defaultValues)(syncAudiencesToDSP_1.default.fields)
            },
            type: 'specificEvent',
            eventSlug: 'warehouse_audience_membership_changed_identify'
        },
        {
            name: 'Associated Entity Added',
            partnerAction: 'syncAudiencesToDSP',
            mapping: (0, actions_core_1.defaultValues)(syncAudiencesToDSP_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_added_track'
        },
        {
            name: 'Associated Entity Removed',
            partnerAction: 'syncAudiencesToDSP',
            mapping: (0, actions_core_1.defaultValues)(syncAudiencesToDSP_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_removed_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'syncAudiencesToDSP',
            mapping: {
                ...(0, actions_core_1.defaultValues)(syncAudiencesToDSP_1.default.fields)
            },
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map