"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dynamicFields = void 0;
const actions_core_1 = require("@segment/actions-core");
exports.dynamicFields = {
    canvas_id: async (request, { settings }) => {
        const canvasListItems = [];
        if (!settings.endpoint) {
            throw new actions_core_1.APIError('Braze REST API endpoint is required.', 400);
        }
        try {
            let page = 0;
            let hasMore = true;
            const MAX_PAGES = 100;
            while (hasMore) {
                const response = await request(`${settings.endpoint}/canvas/list`, {
                    method: 'GET',
                    searchParams: {
                        page: page
                    }
                });
                const canvases = response?.data?.canvases || [];
                if (canvases.length === 0 || page >= MAX_PAGES) {
                    hasMore = false;
                }
                else {
                    canvases.forEach((canvas) => {
                        canvasListItems.push({
                            label: `${canvas.name}`,
                            value: canvas.id
                        });
                    });
                    page++;
                }
            }
            canvasListItems.sort((a, b) => a.label.localeCompare(b.label));
        }
        catch (error) {
            throw new actions_core_1.APIError(`Failed to fetch canvas list: ${error instanceof Error ? error.message : String(error)}`, 400);
        }
        return {
            choices: canvasListItems
        };
    }
};
//# sourceMappingURL=dynamic-field-functions.js.map