"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const client_1 = require("../client");
const functions_1 = require("../functions");
const action = {
    title: 'ImpressionsList',
    defaultSubscription: 'type = "track" and event = "Product List Viewed"',
    description: 'Send impression events to Topsort when a consumer has viewed a list of promotables.',
    fields: {
        id: {
            label: 'Event ID',
            description: 'Unique ID generated by the client to suppress duplicate events. The length should not exceed 128 characters.',
            type: 'string',
            required: true,
            default: {
                '@path': '$.messageId'
            }
        },
        occurredAt: {
            label: 'Occurred At',
            description: 'Timestamp that the event happened at.',
            type: 'datetime',
            required: true,
            default: {
                '@path': '$.timestamp'
            }
        },
        opaqueUserId: {
            label: 'Opaque User ID',
            description: 'Identifier for tracking users regardless of sign-in status. The length should not exceed 128 characters.',
            type: 'string',
            required: true,
            default: {
                '@path': '$.anonymousId'
            }
        },
        products: {
            label: 'Products',
            description: 'The list of products viewed. Each product is a promotable entity.',
            type: 'object',
            multiple: true,
            required: true,
            properties: {
                resolvedBidId: {
                    label: 'Resolved Bid ID',
                    description: 'Identifier of an instance of a resolved auction for a determined product. The length should not exceed 128 characters.',
                    type: 'string',
                    required: true
                },
                additionalAttribution: {
                    label: 'Additional Attribution',
                    description: 'Additional attribution information.',
                    type: 'object',
                    required: false
                }
            },
            default: {
                '@arrayPath': [
                    '$.properties.products',
                    {
                        resolvedBidId: { '@path': 'resolvedBidId' },
                        additionalAttribution: { '@path': 'additionalAttribution' }
                    }
                ]
            }
        },
        deviceType: {
            label: 'Device Type',
            description: 'The device the user is on.',
            type: 'string',
            required: false,
            default: {
                '@path': '$.context.device.type'
            }
        },
        channel: {
            label: 'Channel',
            description: 'The channel where the event occurred.',
            type: 'string',
            required: false,
            default: {
                '@path': '$.properties.channel'
            }
        }
    },
    perform: (request, { payload, settings }) => {
        const client = new client_1.TopsortAPIClient(request, settings);
        payload.deviceType = (0, functions_1.NormalizeDeviceType)(payload.deviceType);
        const impressions = payload.products?.map((impression) => ({
            id: payload.id,
            occurredAt: payload.occurredAt,
            opaqueUserId: payload.opaqueUserId,
            resolvedBidId: impression.resolvedBidId,
            deviceType: payload.deviceType,
            channel: payload.channel,
            ...(impression.additionalAttribution && {
                additionalAttribution: impression.additionalAttribution
            })
        }));
        return client.sendEvent({
            impressions
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map