"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.productClickPresets = exports.productClickedEvents = void 0;
const actions_core_1 = require("@segment/actions-core");
const algolia_insight_api_1 = require("../algolia-insight-api");
exports.productClickedEvents = {
    title: 'Product Clicked Events',
    description: 'When a product is clicked within an Algolia Search, Recommend or Predict result',
    fields: {
        objectID: {
            label: 'Product ID',
            description: 'Populates the ObjectIds field in the Algolia Insights API. Product ID of the clicked item.',
            type: 'string',
            required: true,
            default: {
                '@path': '$.properties.product_id'
            }
        },
        index: {
            label: 'Index',
            description: 'Name of the targeted search index.',
            type: 'string',
            required: true,
            default: {
                '@path': '$.properties.search_index'
            }
        },
        queryID: {
            label: 'Query ID',
            description: 'Query ID of the list on which the item was clicked.',
            type: 'string',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.properties.query_id' },
                    then: { '@path': '$.properties.query_id' },
                    else: { '@path': '$.integrations.Algolia Insights (Actions).query_id' }
                }
            }
        },
        position: {
            label: 'Position',
            description: 'Position of the click in the list of Algolia search results. Positions should start from 1, not 0.',
            type: 'integer',
            required: false,
            default: {
                '@path': '$.properties.position'
            }
        },
        userToken: {
            type: 'string',
            required: true,
            description: 'The ID associated with the user. If a user is authenticated, this should be set to the same value as the Authenticated User Token',
            label: 'User Token',
            default: {
                '@if': {
                    exists: { '@path': '$.anonymousId' },
                    then: { '@path': '$.anonymousId' },
                    else: { '@path': '$.userId' }
                }
            }
        },
        authenticatedUserToken: {
            type: 'string',
            description: 'The authenticated ID associated with the user.',
            label: 'Authenticated User Token',
            default: { '@path': '$.userId' }
        },
        timestamp: {
            type: 'string',
            required: false,
            description: 'The timestamp of the event.',
            label: 'Timestamp',
            default: { '@path': '$.timestamp' }
        },
        extraProperties: {
            label: 'Extra Properties',
            required: false,
            description: 'Additional fields for this event. This field may be useful for Algolia Insights fields which are not mapped in Segment.',
            type: 'object',
            default: {
                '@path': '$.properties'
            }
        },
        eventName: {
            label: 'Event Name',
            description: "The name of the event to be send to Algolia. Defaults to 'Product Clicked'",
            type: 'string',
            required: false,
            default: 'Product Clicked'
        },
        eventType: {
            label: 'Event Type',
            description: "The type of event to send to Algolia. Defaults to 'click'",
            type: 'string',
            required: false,
            default: 'click',
            choices: [
                { label: 'view', value: 'view' },
                { label: 'conversion', value: 'conversion' },
                { label: 'click', value: 'click' }
            ]
        }
    },
    defaultSubscription: 'type = "track" and event = "Product Clicked"',
    perform: (request, data) => {
        const insightEvent = {
            ...data.payload.extraProperties,
            eventName: data.payload.eventName ?? 'Product Clicked',
            eventType: data.payload.eventType ?? 'click',
            index: data.payload.index,
            queryID: data.payload.queryID,
            objectIDs: [data.payload.objectID],
            userToken: data.payload.userToken,
            authenticatedUserToken: data.payload.authenticatedUserToken,
            positions: data.payload.position != null ? [data.payload.position] : undefined,
            timestamp: data.payload.timestamp ? new Date(data.payload.timestamp).valueOf() : undefined
        };
        const insightPayload = { events: [insightEvent] };
        return request(algolia_insight_api_1.AlgoliaBehaviourURL, {
            method: 'post',
            json: insightPayload
        });
    }
};
exports.productClickPresets = {
    name: 'Send product clicked events to Algolia',
    subscribe: exports.productClickedEvents.defaultSubscription,
    partnerAction: 'productClickedEvents',
    mapping: (0, actions_core_1.defaultValues)(exports.productClickedEvents.fields),
    type: 'automatic'
};
//# sourceMappingURL=index.js.map