"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dynamicCustomFields = dynamicCustomFields;
const constants_1 = require("../constants");
function createErrorResponse(message, code) {
    return {
        choices: [],
        error: { message: message ?? 'Unknown error', code: code ?? '404' }
    };
}
async function dynamicCustomFields(request, payload, fieldType) {
    try {
        const response = await request(constants_1.GET_CUSTOM_FIELDS_URL, {
            method: 'GET',
            skipResponseCloning: true
        });
        const selected = new Set(Object.keys({ ...payload.custom_text_fields, ...payload.custom_number_fields, ...payload.custom_date_fields }));
        const remainingFields = response.data.custom_fields.filter(field => field.field_type === fieldType).filter(field => !selected.has(field.name));
        if (remainingFields.length === 0) {
            return createErrorResponse(`No custom ${fieldType} fields available`);
        }
        return {
            choices: remainingFields.map(field => ({
                label: field.name,
                value: field.name
            }))
        };
    }
    catch (err) {
        const error = err;
        return createErrorResponse(error.response.data.errors.map((e) => e.message).join(';') ?? 'Unknown error: dynamicCustomFields', String(error.response.status));
    }
}
//# sourceMappingURL=dynamic-fields.js.map