"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 addToAudience_1 = __importDefault(require("./addToAudience"));
const removeFromAudience_1 = __importDefault(require("./removeFromAudience"));
const constants_1 = require("./constants");
const shared_1 = require("./shared");
const errors_1 = require("./errors");
const destination = {
    name: 'Display and Video 360 (Actions)',
    slug: 'actions-display-video-360',
    mode: 'cloud',
    audienceFields: {
        advertiserId: {
            type: 'string',
            label: 'Advertiser ID',
            required: true,
            description: 'The ID of your advertiser, used throughout Display & Video 360. Use this ID when you contact Display & Video 360 support to help our teams locate your specific account.'
        },
        accountType: {
            type: 'string',
            label: 'Account Type',
            description: 'The type of the advertiser account you have linked to this Display & Video 360 destination.',
            required: true,
            choices: [
                { label: 'Advertiser', value: 'DISPLAY_VIDEO_ADVERTISER' },
                { label: 'Partner', value: 'DISPLAY_VIDEO_PARTNER' },
                { label: 'Publisher', value: 'GOOGLE_AD_MANAGER' }
            ]
        }
    },
    audienceConfig: {
        mode: {
            type: 'synced',
            full_audience_sync: false
        },
        async createAudience(request, createAudienceInput) {
            const { audienceName, audienceSettings, statsContext } = createAudienceInput;
            const { accountType } = audienceSettings || {};
            const advertiserId = audienceSettings?.advertiserId.trim();
            const { statsClient, tags: statsTags } = statsContext || {};
            const statsName = 'createAudience';
            statsTags?.push(`slug:${destination.slug}`);
            statsClient?.incr(`${statsName}.call`, 1, statsTags);
            const authSettings = (0, shared_1.getAuthSettings)();
            if (!audienceName) {
                statsTags?.push('error:missing-settings');
                statsClient?.incr(`${statsName}.error`, 1, statsTags);
                throw new actions_core_1.IntegrationError('Missing audience name value', 'MISSING_REQUIRED_FIELD', 400);
            }
            if (!advertiserId) {
                statsTags?.push('error:missing-settings');
                statsClient?.incr(`${statsName}.error`, 1, statsTags);
                throw new actions_core_1.IntegrationError('Missing advertiser ID value', 'MISSING_REQUIRED_FIELD', 400);
            }
            if (!accountType) {
                statsTags?.push('error:missing-settings');
                statsClient?.incr(`${statsName}.error`, 1, statsTags);
                throw new actions_core_1.IntegrationError('Missing account type value', 'MISSING_REQUIRED_FIELD', 400);
            }
            const listTypeMap = { basicUserList: {}, type: 'REMARKETING', membershipStatus: 'OPEN' };
            const partnerCreateAudienceUrl = constants_1.CREATE_AUDIENCE_URL.replace('advertiserID', advertiserId).replace('accountType', accountType);
            let response;
            try {
                const authToken = await (0, shared_1.getAuthToken)(request, authSettings);
                response = await request(partnerCreateAudienceUrl, {
                    headers: (0, shared_1.buildHeaders)(createAudienceInput.audienceSettings, authToken),
                    method: 'POST',
                    json: {
                        operations: [
                            {
                                create: {
                                    ...listTypeMap,
                                    name: audienceName,
                                    description: 'Created by Segment',
                                    membershipLifeSpan: '540'
                                }
                            }
                        ]
                    }
                });
                const r = await response?.json();
                statsClient?.incr(`${statsName}.success`, 1, statsTags);
                return {
                    externalId: r['results'][0]['resourceName']
                };
            }
            catch (error) {
                throw (0, errors_1.handleRequestError)(error, statsName, statsContext);
            }
        },
        async getAudience(request, getAudienceInput) {
            const { statsContext, audienceSettings } = getAudienceInput;
            const { statsClient, tags: statsTags } = statsContext || {};
            const { accountType } = audienceSettings || {};
            const advertiserId = audienceSettings?.advertiserId.trim();
            const statsName = 'getAudience';
            statsTags?.push(`slug:${destination.slug}`);
            statsClient?.incr(`${statsName}.call`, 1, statsTags);
            const authSettings = (0, shared_1.getAuthSettings)();
            if (!advertiserId) {
                statsTags?.push('error:missing-settings');
                statsClient?.incr(`${statsName}.error`, 1, statsTags);
                throw new actions_core_1.IntegrationError('Missing required advertiser ID value', 'MISSING_REQUIRED_FIELD', 400);
            }
            if (!accountType) {
                statsTags?.push('error:missing-settings');
                statsClient?.incr(`${statsName}.error`, 1, statsTags);
                throw new actions_core_1.IntegrationError('Missing account type value', 'MISSING_REQUIRED_FIELD', 400);
            }
            const advertiserGetAudienceUrl = constants_1.GET_AUDIENCE_URL.replace('advertiserID', advertiserId).replace('accountType', accountType);
            try {
                const authToken = await (0, shared_1.getAuthToken)(request, authSettings);
                const response = await request(advertiserGetAudienceUrl, {
                    headers: (0, shared_1.buildHeaders)(audienceSettings, authToken),
                    method: 'POST',
                    json: {
                        query: `SELECT user_list.name, user_list.description, user_list.membership_status, user_list.match_rate_percentage FROM user_list WHERE user_list.resource_name = "${getAudienceInput.externalId}"`
                    }
                });
                const r = await response.json();
                const externalId = r[0]?.results[0]?.userList?.resourceName;
                if (externalId !== getAudienceInput.externalId) {
                    statsClient?.incr(`${statsName}.error`, 1, statsTags);
                    throw new actions_core_1.IntegrationError("Unable to verify ownership over audience. Segment Audience ID doesn't match Googles Audience ID.", 'INVALID_REQUEST_DATA', 400);
                }
                statsClient?.incr(`${statsName}.success`, 1, statsTags);
                return {
                    externalId: externalId
                };
            }
            catch (error) {
                throw (0, errors_1.handleRequestError)(error, statsName, statsContext);
            }
        }
    },
    actions: {
        addToAudience: addToAudience_1.default,
        removeFromAudience: removeFromAudience_1.default
    },
    presets: [
        {
            name: 'Entities Audience Entered',
            partnerAction: 'addToAudience',
            mapping: (0, actions_core_1.defaultValues)(addToAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_audience_entered_track'
        },
        {
            name: 'Entities Audience Exited',
            partnerAction: 'removeFromAudience',
            mapping: (0, actions_core_1.defaultValues)(removeFromAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_audience_exited_track'
        },
        {
            name: 'Associated Entity Added',
            partnerAction: 'addToAudience',
            mapping: (0, actions_core_1.defaultValues)(addToAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_added_track'
        },
        {
            name: 'Associated Entity Removed',
            partnerAction: 'removeFromAudience',
            mapping: (0, actions_core_1.defaultValues)(removeFromAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_removed_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'addToAudience',
            mapping: (0, actions_core_1.defaultValues)(addToAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map