"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const functions_1 = require("../functions");
const properties_1 = require("../properties");
const api_1 = require("../api");
const action = {
    title: 'Create Audience (Legacy)',
    description: 'Use this action to create a new audience in TikTok Audience Segment. This is required for legacy instances of the TikTok Audience destination to create a partner audience within TikTok for syncing Engage audiences to.',
    defaultSubscription: 'event = "Create Audience"',
    fields: {
        selected_advertiser_id: { ...properties_1.selected_advertiser_id },
        custom_audience_name: { ...properties_1.custom_audience_name }
    },
    dynamicFields: {
        selected_advertiser_id: async (request, { settings }) => {
            try {
                const tiktok = new api_1.TikTokAudiences(request);
                if (settings.advertiser_ids) {
                    return tiktok.fetchAdvertisers(settings.advertiser_ids);
                }
                return {
                    choices: [],
                    error: {
                        message: JSON.stringify('BAD REQUEST - expected settings.advertiser_ids and got nothing!'),
                        code: '400'
                    }
                };
            }
            catch (err) {
                return {
                    choices: [],
                    error: {
                        message: JSON.stringify(err),
                        code: '500'
                    }
                };
            }
        }
    },
    perform: async (request, { payload, statsContext }) => {
        statsContext?.statsClient?.incr('createAudience.legacy', 1, statsContext?.tags);
        return (0, functions_1.createAudience)(request, payload);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map