"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dynamicFields = void 0;
const properties_1 = require("../../properties");
const constants_1 = require("../constants");
const validation_functions_1 = require("./validation-functions");
exports.dynamicFields = {
    event_name: async (request) => {
        return await dynamicReadEventNames(request);
    },
    record_details: {
        object_type: async (request) => {
            return await dynamicReadObjectTypes(request);
        }
    },
    properties: {
        __keys__: async (request, { payload }) => {
            const eventName = payload?.event_name;
            if (!eventName) {
                throw new Error("Select from 'Event Name' first");
            }
            return await dynamicReadProperties(request, eventName);
        }
    }
};
async function dynamicReadEventNames(request) {
    try {
        const response = await request(`${properties_1.HUBSPOT_BASE_URL}/events/v3/event-definitions/?includeProperties=false`, {
            method: 'GET',
            skipResponseCloning: true
        });
        return {
            choices: response.data.results
                .filter((event) => !event.archived && (event.labels?.singular || event.labels?.plural))
                .map((event) => {
                return {
                    label: event.labels?.singular ?? event.labels?.plural ?? '',
                    value: event.fullyQualifiedName
                };
            })
                .sort((a, b) => a.label.toLowerCase().localeCompare(b.label.toLowerCase()))
        };
    }
    catch (err) {
        const code = err?.response?.status ? String(err.response.status) : '500';
        return {
            choices: [],
            error: {
                message: err?.response?.data?.message ?? 'Unknown error: dynamicReadEventNames',
                code: code
            }
        };
    }
}
async function dynamicReadObjectTypes(request) {
    const defaultChoices = constants_1.SUPPORTED_HUBSPOT_OBJECT_TYPES;
    try {
        const response = await request(`${properties_1.HUBSPOT_BASE_URL}/crm/v3/schemas?archived=false`, {
            method: 'GET',
            skipResponseCloning: true
        });
        const choices = response.data.results.map((schema) => ({
            label: `${schema.labels.plural} (Custom)`,
            value: schema.fullyQualifiedName
        }));
        return {
            choices: [...choices, ...defaultChoices].sort((a, b) => a.label.toLowerCase().localeCompare(b.label.toLowerCase()))
        };
    }
    catch (err) {
        const code = err?.response?.status ? String(err.response.status) : '500';
        return {
            choices: [],
            error: {
                message: err?.response?.data?.message ?? 'Unknown error: dynamicReadObjectTypes',
                code: code
            }
        };
    }
}
async function dynamicReadProperties(request, eventName) {
    try {
        const response = await request(`${properties_1.HUBSPOT_BASE_URL}/events/v3/event-definitions/?includeProperties=true`, {
            method: 'GET',
            skipResponseCloning: true
        });
        const cleanedEventName = (0, validation_functions_1.cleanEventName)(eventName);
        return {
            choices: (() => {
                const choices = response.data.results
                    .filter((event) => (event.fullyQualifiedName === cleanedEventName || event.name === cleanedEventName) && !event.archived)
                    .map((event) => {
                    if (!event.properties || event.properties.length === 0) {
                        return {
                            label: `No properties found for event ${eventName}`,
                            value: ''
                        };
                    }
                    return event.properties
                        .filter((property) => !property.archived)
                        .sort((a, b) => a.label.toLowerCase().localeCompare(b.label.toLowerCase()))
                        .map((property) => {
                        return {
                            label: `${property.label} - ${property.type}`,
                            value: property.name
                        };
                    });
                })
                    .flat();
                if (choices.length === 0) {
                    return constants_1.DEFAULT_CUSTOM_EVENT_PROPERTIES;
                }
                return choices;
            })()
        };
    }
    catch (err) {
        const code = err?.response?.status ? String(err.response.status) : '500';
        return {
            choices: [],
            error: {
                message: err?.response?.data?.message ?? 'Unknown error: dynamicReadProperties',
                code: code
            }
        };
    }
}
//# sourceMappingURL=dynamic-field-functions.js.map