"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const btoa_lite_1 = __importDefault(require("btoa-lite"));
const actions_core_1 = require("@segment/actions-core");
const upsertCustomAudiences_1 = __importDefault(require("./upsertCustomAudiences"));
const constants_1 = require("./constants");
const destination = {
    name: 'Rokt Audiences (Actions)',
    slug: 'actions-rokt-audiences',
    mode: 'cloud',
    description: `
  This destination allows user to engage audiences using Rokt's Public APIs.
  User can connect Rokt Audiences (Actions) as a destination to their Engage Audience in Segment,
  which will create/update custom audiences in the Rokt data platform. Audiences can be defined with either
  email values or hashed (sha256) email values.
  `,
    authentication: {
        scheme: 'custom',
        fields: {
            rpub: {
                label: 'Rokt public key',
                description: 'Rokt public key, starts with `rpub-`',
                type: 'string',
                required: true
            },
            rsec: {
                label: 'Rokt secret key',
                description: 'Rokt secret key, starts with `rsec-`',
                type: 'password',
                required: true
            },
            accountid: {
                label: 'Rokt Account ID',
                description: 'Rokt ID assigned to your particular account.',
                type: 'string',
                required: true
            }
        },
        testAuthentication: async (request, { settings }) => {
            return await request(constants_1.CONSTANTS.ROKT_API_BASE_URL + constants_1.CONSTANTS.ROKT_API_AUTH_ENDPOINT, {
                method: 'POST',
                headers: {
                    Authorization: `Basic ${(0, btoa_lite_1.default)(settings.rpub + ':' + settings.rsec)}`
                },
                json: {
                    accountId: settings.accountid
                }
            });
        }
    },
    extendRequest({ settings }) {
        return {
            headers: { Authorization: `Basic ${(0, btoa_lite_1.default)(settings.rpub + ':' + settings.rsec)}` }
        };
    },
    actions: {
        upsertCustomAudiences: upsertCustomAudiences_1.default
    },
    presets: [
        {
            name: 'Sync Engage Audience to Rokt',
            subscribe: 'type = "track" or type = "identify"',
            partnerAction: 'upsertCustomAudiences',
            mapping: (0, actions_core_1.defaultValues)(upsertCustomAudiences_1.default.fields),
            type: 'automatic'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map