"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const avo_1 = require("./avo");
const processEvents = async (request, settings, payload) => {
    const events = payload.map((value) => (0, avo_1.extractSchemaFromEvent)(value, settings.appVersionPropertyName));
    const endpoint = 'https://api.avo.app/inspector/segment/v1/track';
    return request(endpoint, {
        method: 'post',
        headers: {
            accept: 'application/json',
            'content-type': 'application/json',
            'api-key': settings.apiKey,
            env: settings.env
        },
        body: JSON.stringify(events)
    });
};
const sendSchemaAction = {
    title: 'Track Schema From Event',
    description: 'Sends event schema to the Avo Inspector API',
    defaultSubscription: 'type = "track"',
    fields: {
        event: {
            label: 'Event Name',
            type: 'string',
            description: 'Name of the event being sent',
            required: true,
            default: {
                '@path': '$.event'
            }
        },
        properties: {
            label: 'Properties',
            type: 'object',
            description: 'Properties of the event being sent',
            required: true,
            default: {
                '@path': '$.properties'
            }
        },
        messageId: {
            label: 'Message ID',
            type: 'string',
            description: 'Message ID of the event being sent',
            required: true,
            default: {
                '@path': '$.messageId'
            }
        },
        createdAt: {
            label: 'Created At',
            type: 'string',
            description: 'Timestamp of when the event was sent',
            required: true,
            default: {
                '@path': '$.timestamp'
            }
        },
        appVersion: {
            label: 'App Version',
            type: 'string',
            description: 'Version of the app that sent the event',
            required: false,
            default: {
                '@path': '$.context.app.version'
            }
        },
        appName: {
            label: 'App Name',
            type: 'string',
            description: 'Name of the app that sent the event',
            required: false,
            default: {
                '@path': '$.context.app.name'
            }
        },
        pageUrl: {
            label: 'Page URL',
            type: 'string',
            description: 'URL of the page that sent the event',
            required: false,
            default: {
                '@path': '$.context.page.url'
            }
        }
    },
    perform: async (request, { payload, settings }) => {
        return processEvents(request, settings, [payload]);
    },
    performBatch: async (request, { payload, settings }) => {
        return processEvents(request, settings, payload);
    }
};
exports.default = sendSchemaAction;
//# sourceMappingURL=index.js.map