"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const recombeeApiClient_1 = require("../recombeeApiClient");
const commonFields_1 = require("../commonFields");
const action = {
    title: 'Add Purchase',
    description: 'Adds a purchase of the given item(s) made by the given user.',
    fields: {
        userId: {
            label: 'User ID',
            description: 'The ID of the user who purchased the item(s).',
            type: 'string',
            required: true,
            default: {
                '@if': {
                    exists: { '@path': '$.userId' },
                    then: { '@path': '$.userId' },
                    else: { '@path': '$.anonymousId' }
                }
            }
        },
        items: {
            label: 'Items',
            description: 'The items that were purchased.',
            type: 'object',
            multiple: true,
            required: true,
            properties: {
                itemId: {
                    label: 'Item ID',
                    description: 'ID of the item.',
                    type: 'string',
                    required: true
                },
                amount: {
                    label: 'Amount',
                    description: 'The amount (number) of the item purchased.',
                    type: 'number',
                    required: false,
                    default: 1
                },
                price: {
                    label: 'Price (per item)',
                    description: 'The price of the purchased item. If `amount` is greater than 1, the price of one item should be given.',
                    type: 'number',
                    required: false
                },
                profit: {
                    label: 'Profit (per item)',
                    description: 'The profit of the purchased item. If `amount` is greater than 1, the profit per one item should be given.',
                    type: 'number',
                    required: false
                }
            },
            default: {
                '@arrayPath': [
                    '$.properties.products',
                    {
                        itemId: {
                            '@if': {
                                exists: { '@path': '$.product_id' },
                                then: { '@path': '$.product_id' },
                                else: { '@path': '$.sku' }
                            }
                        },
                        amount: {
                            '@path': '$.quantity'
                        },
                        price: {
                            '@path': '$.price'
                        },
                        profit: {
                            '@path': '$.profit'
                        }
                    }
                ]
            }
        },
        timestamp: {
            label: 'Timestamp',
            description: 'The UTC timestamp of when the purchase occurred.',
            type: 'string',
            required: false,
            default: { '@path': '$.timestamp' }
        },
        ...(0, commonFields_1.interactionFields)('purchase')
    },
    perform: async (request, data) => {
        const client = new recombeeApiClient_1.RecombeeApiClient(data.settings, request);
        await client.send(new recombeeApiClient_1.Batch(payloadToPurchases(data.payload)));
    },
    performBatch: async (request, data) => {
        const client = new recombeeApiClient_1.RecombeeApiClient(data.settings, request);
        await client.send(new recombeeApiClient_1.Batch(data.payload.flatMap(payloadToPurchases)));
    }
};
function payloadToPurchases({ items, ...rest }) {
    return items.map((item) => new recombeeApiClient_1.AddPurchase({ ...item, ...rest }));
}
exports.default = action;
//# sourceMappingURL=index.js.map