"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TikTokAudiences = void 0;
const constants_1 = require("../constants");
const versioning_info_1 = require("../versioning-info");
class TikTokAudiences {
    constructor(request, selectedAdvertiserID) {
        this.fetchAudiences = async (selected_advertiser_id) => {
            if (!selected_advertiser_id || !selected_advertiser_id.length) {
                return {
                    choices: [],
                    error: {
                        message: 'Please select Advertiser ID first to get list of Audience IDs.',
                        code: 'FIELD_NOT_SELECTED'
                    }
                };
            }
            try {
                const response = [];
                let page_number = 1;
                let total_page = 1;
                while (page_number <= total_page) {
                    const result = await this.request(`${constants_1.BASE_URL}${versioning_info_1.TIKTOK_AUDIENCES_API_VERSION}/dmp/custom_audience/list/`, {
                        method: 'GET',
                        searchParams: {
                            advertiser_id: selected_advertiser_id,
                            page: page_number,
                            page_size: '100'
                        }
                    });
                    if (result.data.code !== 0) {
                        throw {
                            message: result.data.message,
                            code: result.data.code
                        };
                    }
                    total_page = result.data?.data?.page_info?.total_page || 1;
                    page_number++;
                    result.data.data.list.forEach((item) => {
                        response.push(item);
                    });
                }
                const choices = response?.map((item) => {
                    return {
                        label: item.name,
                        value: item.audience_id
                    };
                });
                return {
                    choices: choices
                };
            }
            catch (err) {
                return {
                    choices: [],
                    error: {
                        message: err.message ?? 'An error occurred while fetching audiences.',
                        code: err.code.toString() ?? 'FETCH_AUDIENCE_ERROR'
                    }
                };
            }
        };
        this.fetchAdvertisers = async (advertiser_ids) => {
            if (!advertiser_ids.length) {
                return {
                    choices: [],
                    error: {
                        message: 'Sign in via OAuth on the settings page to load advertisers.',
                        code: 'NOT_LOGGED_IN'
                    }
                };
            }
            try {
                const result = await this.request(`${constants_1.BASE_URL}${versioning_info_1.TIKTOK_AUDIENCES_API_VERSION}/advertiser/info/`, {
                    method: 'GET',
                    searchParams: {
                        advertiser_ids: JSON.stringify(advertiser_ids)
                    }
                });
                if (result.data.code !== 0) {
                    throw {
                        message: result.data.message,
                        code: result.data.code
                    };
                }
                const choices = result.data.data.list.map((item) => {
                    return {
                        label: item.name,
                        value: item.advertiser_id
                    };
                });
                return {
                    choices: choices
                };
            }
            catch (err) {
                const choices = advertiser_ids.map((id) => {
                    return {
                        label: id,
                        value: id
                    };
                });
                return {
                    choices: choices,
                    error: {
                        message: err.response.data.message ?? 'An error occurred while fetching advertisers.',
                        code: err.response.data.code.toString() ?? 'FETCH_ADVERTISERS_ERROR'
                    }
                };
            }
        };
        this.request = request;
        this.selectedAdvertiserID = selectedAdvertiserID;
    }
    async getUserInfo() {
        return this.request(`${constants_1.BASE_URL}${versioning_info_1.TIKTOK_AUDIENCES_API_VERSION}/user/info/`, {
            method: 'GET'
        });
    }
    async createAudience(payload) {
        return this.request(`${constants_1.BASE_URL}${versioning_info_1.TIKTOK_AUDIENCES_API_VERSION}/segment/audience/`, {
            method: 'POST',
            json: {
                custom_audience_name: payload.custom_audience_name,
                advertiser_id: this.selectedAdvertiserID,
                action: 'create'
            }
        });
    }
    async batchUpdate(elements) {
        return this.request(`${constants_1.BASE_URL}${versioning_info_1.TIKTOK_AUDIENCES_API_VERSION}/segment/mapping/`, {
            method: 'POST',
            json: elements,
            throwHttpErrors: false
        });
    }
}
exports.TikTokAudiences = TikTokAudiences;
//# sourceMappingURL=index.js.map