"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const client_1 = require("../client");
const functions_1 = require("../functions");
const action = {
    title: 'Purchase',
    defaultSubscription: 'type = "track" and event = "Order Completed"',
    description: 'Send purchase events to Topsort when a consumer places an order.',
    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'
            }
        },
        items: {
            label: 'Items',
            description: 'Item information list related to the event.',
            type: 'object',
            required: true,
            multiple: true,
            properties: {
                productId: {
                    label: 'Product ID',
                    description: 'The marketplace ID of the product being purchased.',
                    type: 'string',
                    required: true
                },
                unitPrice: {
                    label: 'Unit Price',
                    description: 'The price of a single item in the marketplace currency.',
                    type: 'number',
                    required: false
                },
                quantity: {
                    label: 'Quantity',
                    description: 'Count of products purchased.',
                    type: 'integer',
                    required: false
                },
                vendorId: {
                    label: 'Vendor ID',
                    description: 'The vendor ID of the product being purchased.',
                    type: 'string',
                    required: false
                }
            },
            default: {
                '@arrayPath': [
                    '$.properties.products',
                    {
                        productId: { '@path': '$.product_id' },
                        unitPrice: { '@path': '$.price' },
                        quantity: { '@path': '$.quantity' },
                        vendorId: {
                            '@if': {
                                exists: { '@path': '$.vendorId' },
                                then: { '@path': '$.vendorId' },
                                else: { '@path': '$.brand' }
                            }
                        }
                    }
                ]
            }
        },
        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);
        if (settings.skipZeroPricePurchases) {
            const filteredItems = payload.items.filter((item) => item.unitPrice != null && item.unitPrice > 0);
            if (filteredItems.length === 0) {
                return;
            }
            payload.items = filteredItems;
        }
        payload.deviceType = (0, functions_1.NormalizeDeviceType)(payload.deviceType);
        return client.sendEvent({
            purchases: [payload]
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map