"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 fields_1 = require("./fields");
const sync_1 = __importDefault(require("./sync"));
const functions_1 = require("./functions");
const presets_1 = require("./presets");
const destination = {
    name: 'Facebook Custom Audiences (Actions)',
    slug: 'actions-facebook-custom-audiences',
    mode: 'cloud',
    description: 'The Facebook Custom Audiences destination.',
    authentication: {
        scheme: 'oauth2',
        fields: {
            retlAdAccountId: {
                ...fields_1.adAccountId,
                description: 'Your advertiser account id. Read [more](https://www.facebook.com/business/help/1492627900875762). This is required to set up the connection, but can be overriden using the Engage Audience setting named "Advertiser Account ID".'
            }
        }
    },
    extendRequest({ auth }) {
        return {
            headers: {
                authorization: `Bearer ${auth?.accessToken}`
            }
        };
    },
    audienceFields: {
        engageAdAccountId: {
            ...fields_1.adAccountId,
            description: 'Your advertiser account id. Read [more](https://www.facebook.com/business/help/1492627900875762). This overrides the main Destination settings named "Advertiser Account ID".',
            required: false
        },
        audienceDescription: fields_1.audienceDescription
    },
    audienceConfig: {
        mode: {
            type: 'synced',
            full_audience_sync: false
        },
        async createAudience(request, createAudienceInput) {
            const { audienceName, audienceSettings: { engageAdAccountId, audienceDescription } = {}, settings: { retlAdAccountId } = {}, features, statsContext } = createAudienceInput;
            const addAccountId = (engageAdAccountId ?? retlAdAccountId);
            const { data: { externalId: id } = {}, error } = await (0, functions_1.createAudience)(request, audienceName, addAccountId, audienceDescription, features, statsContext);
            if (error) {
                throw new actions_core_1.IntegrationError(error.message, actions_core_1.ErrorCodes.CREATE_AUDIENCE_FAILED, 400);
            }
            return { externalId: id };
        },
        async getAudience(request, getAudienceInput) {
            const { externalId, features, statsContext } = getAudienceInput;
            const { data: { externalId: id } = {}, error } = await (0, functions_1.getAudience)(request, externalId, features, statsContext);
            if (error) {
                throw new actions_core_1.IntegrationError(error.message, actions_core_1.ErrorCodes.GET_AUDIENCE_FAILED, 400);
            }
            return { externalId: id };
        }
    },
    actions: {
        sync: sync_1.default
    },
    presets: presets_1.presets
};
exports.default = destination;
//# sourceMappingURL=index.js.map