"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const action = {
    title: 'Page',
    description: 'Register page view in Podscribe',
    defaultSubscription: 'type = "page"',
    fields: {
        anonymousId: {
            type: 'string',
            allowNull: true,
            description: 'The anonymous ID associated with the user',
            label: 'Anonymous ID',
            default: { '@path': '$.anonymousId' }
        },
        timestamp: {
            type: 'string',
            format: 'date-time',
            required: true,
            description: 'The timestamp of the event',
            label: 'Timestamp',
            default: { '@path': '$.timestamp' }
        },
        referrer: {
            type: 'string',
            allowNull: true,
            description: 'The page referrer',
            label: 'Page Referrer',
            default: {
                '@if': {
                    exists: { '@path': '$.context.page.referrer' },
                    then: { '@path': '$.context.page.referrer' },
                    else: { '@path': '$.properties.referrer' }
                }
            }
        },
        url: {
            type: 'string',
            format: 'uri',
            allowNull: true,
            description: 'The page URL',
            label: 'Page URL',
            default: {
                '@if': {
                    exists: { '@path': '$.context.page.url' },
                    then: { '@path': '$.context.page.url' },
                    else: { '@path': '$.properties.url' }
                }
            }
        },
        ip: {
            label: 'User IP address',
            type: 'string',
            required: true,
            description: 'The IP address of the device sending the event.',
            default: {
                '@path': '$.context.ip'
            }
        },
        library: {
            label: 'Segment Library',
            type: 'object',
            description: 'The library sending the event.',
            default: {
                '@path': '$.context.library'
            }
        },
        userAgent: {
            label: 'User Agent',
            type: 'string',
            description: 'The user agent of the device sending the event.',
            default: {
                '@path': '$.context.userAgent'
            }
        }
    },
    perform: (request, { settings, payload }) => {
        const params = (0, utils_1.serializeParams)({
            action: 'view',
            advertiser: settings.advertiser,
            user_id: settings.userId,
            timestamp: payload.timestamp,
            device_id: payload.anonymousId,
            referrer: payload.referrer,
            url: payload.url,
            ip: payload.ip,
            user_agent: payload.userAgent,
            library: payload?.library ? JSON.stringify(payload.library) : null
        });
        return request(`https://verifi.podscribe.com/tag?${params}`);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map