"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const recombeeApiClient_1 = require("../recombeeApiClient");
const commonFields_1 = require("../commonFields");
const action = {
    title: 'Set View Portion from Watch Time',
    description: 'Sets the viewed portion of a given item (e.g. a video or article) by the given user. **Use this action when you have the watch time of the item (e.g. in seconds) instead of the portion watched.**',
    fields: {
        userId: {
            label: 'User ID',
            description: 'The ID of the user who viewed a portion of the item.',
            type: 'string',
            required: true,
            default: {
                '@if': {
                    exists: { '@path': '$.userId' },
                    then: { '@path': '$.userId' },
                    else: { '@path': '$.anonymousId' }
                }
            }
        },
        itemId: {
            label: 'Item ID',
            description: 'The viewed item.',
            type: 'string',
            required: true,
            default: {
                '@if': {
                    exists: { '@path': '$.properties.product_id' },
                    then: { '@path': '$.properties.product_id' },
                    else: { '@path': '$.properties.asset_id' }
                }
            }
        },
        timestamp: {
            label: 'Timestamp',
            description: 'The UTC timestamp of when the view portion occurred.',
            type: 'string',
            required: false,
            default: { '@path': '$.timestamp' }
        },
        portion: {
            label: 'Portion',
            description: 'The portion of the item that the user viewed.',
            type: 'object',
            required: true,
            properties: {
                totalLength: {
                    label: 'Total Length',
                    description: 'The total length of the item that the user can view (for example, in seconds or minutes).',
                    type: 'number',
                    minimum: 0,
                    required: true
                },
                watchTime: {
                    label: 'Watch Time',
                    description: "The user's watched time of the item (measured in the same units as Total Length).",
                    type: 'number',
                    minimum: 0,
                    required: true
                }
            },
            default: {
                totalLength: { '@path': '$.properties.total_length' },
                watchTime: { '@path': '$.properties.position' }
            }
        },
        sessionId: {
            label: 'Session ID',
            description: 'The ID of the session in which the user viewed the item.',
            type: 'string',
            required: false,
            default: { '@path': '$.properties.session' }
        },
        ...(0, commonFields_1.interactionFields)('view portion')
    },
    perform: async (request, data) => {
        const client = new recombeeApiClient_1.RecombeeApiClient(data.settings, request);
        await client.send(payloadToViewPortion(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(payloadToViewPortion)));
    }
};
function payloadToViewPortion(payload) {
    if (payload.portion.totalLength === 0) {
        throw new actions_core_1.PayloadValidationError('The total length of the item cannot be zero.');
    }
    const { portion: portionData, ...rest } = payload;
    if (portionData.watchTime > portionData.totalLength) {
        throw new actions_core_1.PayloadValidationError('The watch time of the item cannot be greater than the total length of the item.');
    }
    return new recombeeApiClient_1.SetViewPortion({ portion: portionData.watchTime / portionData.totalLength, ...rest });
}
exports.default = action;
//# sourceMappingURL=index.js.map