"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const recombeeApiClient_1 = require("../recombeeApiClient");
const commonFields_1 = require("../commonFields");
const action = {
    title: 'Add Cart Addition',
    description: 'Adds a cart addition of the given item made by the given user.',
    defaultSubscription: 'type = "track" and event = "Product Added"',
    fields: {
        userId: {
            label: 'User ID',
            description: 'The ID of the user who added the item to the cart.',
            type: 'string',
            required: true,
            default: {
                '@if': {
                    exists: { '@path': '$.userId' },
                    then: { '@path': '$.userId' },
                    else: { '@path': '$.anonymousId' }
                }
            }
        },
        item: {
            label: 'Item',
            description: 'The item that was added to the cart.',
            type: 'object',
            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 added to the cart.',
                    type: 'number',
                    required: false,
                    default: 1
                },
                price: {
                    label: 'Price (per item)',
                    description: 'The price of the added item. If `amount` is greater than 1, the price of one item should be given.',
                    type: 'number',
                    required: false
                }
            },
            default: {
                itemId: {
                    '@if': {
                        exists: { '@path': '$.properties.product_id' },
                        then: { '@path': '$.properties.product_id' },
                        else: { '@path': '$.properties.sku' }
                    }
                },
                amount: { '@path': '$.properties.quantity' },
                price: { '@path': '$.properties.price' }
            }
        },
        timestamp: {
            label: 'Timestamp',
            description: 'The UTC timestamp of when the cart addition occurred.',
            type: 'string',
            required: false,
            default: { '@path': '$.timestamp' }
        },
        ...(0, commonFields_1.interactionFields)('cart addition')
    },
    perform: async (request, data) => {
        const client = new recombeeApiClient_1.RecombeeApiClient(data.settings, request);
        await client.send(payloadToCartAddition(data.payload));
    },
    performBatch: async (request, data) => {
        const client = new recombeeApiClient_1.RecombeeApiClient(data.settings, request);
        await client.send(new recombeeApiClient_1.Batch(data.payload.map(payloadToCartAddition)));
    }
};
function payloadToCartAddition({ item, ...rest }) {
    return new recombeeApiClient_1.AddCartAddition({ ...item, ...rest });
}
exports.default = action;
//# sourceMappingURL=index.js.map