"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 syncAudience_1 = __importDefault(require("./syncAudience"));
const destination = {
    name: 'Reddit Audiences',
    slug: 'actions-reddit-audiences',
    mode: 'cloud',
    authentication: {
        scheme: 'oauth-managed',
        fields: {
            ad_account_id: {
                type: 'string',
                label: 'Ad Account ID',
                description: 'Unique identifier of an ad account. This can be found in the Reddit UI.',
                required: true
            }
        },
        refreshAccessToken: async (request, { auth }) => {
            const authToken = Buffer.from(`${auth.clientId}:${auth.clientSecret}`).toString('base64');
            const res = await request('https://www.reddit.com/api/v1/access_token', {
                method: 'POST',
                headers: {
                    Authorization: `Basic ${authToken}`,
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'User-Agent': 'segmentaudienceapi:v1 (by /u/segment_audiences)'
                },
                body: new URLSearchParams({
                    refresh_token: auth.refreshToken,
                    grant_type: 'refresh_token'
                })
            });
            const responseData = res.data;
            return { accessToken: responseData.access_token };
        }
    },
    extendRequest({ auth }) {
        return {
            headers: {
                'Content-Type': 'application/json',
                authorization: `Bearer ${auth?.accessToken}`
            }
        };
    },
    audienceFields: {
        audienceName: {
            label: 'Audience Name',
            description: 'An audience name to display in Reddit',
            type: 'string',
            required: true
        }
    },
    audienceConfig: {
        mode: {
            type: 'synced',
            full_audience_sync: false
        },
        async createAudience(request, createAudienceInput) {
            const response = await request(`https://ads-api.reddit.com/api/v3/ad_accounts/${createAudienceInput.settings.ad_account_id}/custom_audiences`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                json: {
                    data: {
                        name: createAudienceInput.audienceName,
                        type: 'CUSTOMER_LIST'
                    }
                }
            });
            const jsonOutput = await response.json();
            return { externalId: jsonOutput.data.id };
        },
        async getAudience(_, getAudienceInput) {
            return {
                externalId: getAudienceInput.externalId
            };
        }
    },
    actions: {
        syncAudience: syncAudience_1.default
    },
    presets: [
        {
            name: 'Entities Audience Membership Changed',
            partnerAction: 'syncAudience',
            mapping: (0, actions_core_1.defaultValues)(syncAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_audience_membership_changed_identify'
        },
        {
            name: 'Associated Entity Added',
            partnerAction: 'syncAudience',
            mapping: (0, actions_core_1.defaultValues)(syncAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_added_track'
        },
        {
            name: 'Associated Entity Removed',
            partnerAction: 'syncAudience',
            mapping: (0, actions_core_1.defaultValues)(syncAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_removed_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'syncAudience',
            mapping: (0, actions_core_1.defaultValues)(syncAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map