"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = validate;
exports.validateSubscribeUser = validateSubscribeUser;
exports.formatEcommEventJSON = formatEcommEventJSON;
exports.formatCustomEventJSON = formatCustomEventJSON;
exports.formatUpsertUserAttributesJSON = formatUpsertUserAttributesJSON;
exports.formatSubscribeUserJSON = formatSubscribeUserJSON;
const actions_core_1 = require("@segment/actions-core");
function validate(payload) {
    const { userIdentifiers: { phone, email, clientUserId, ...customIdentifiers } } = payload;
    if (!email && !phone && !clientUserId && Object.keys(customIdentifiers).length === 0) {
        throw new actions_core_1.PayloadValidationError('At least one user identifier is required.');
    }
}
function validateSubscribeUser(payload) {
    const { signUpSourceId, locale } = payload;
    if ([signUpSourceId, locale].filter(Boolean).length !== 1) {
        throw new actions_core_1.PayloadValidationError('Exactly one of signUpSourceId or locale should be provided.');
    }
}
function formatUser(userIdentifiers) {
    const { phone, email, clientUserId, ...customIdentifiers } = userIdentifiers;
    return {
        ...(phone ? { phone } : {}),
        ...(email ? { email } : {}),
        ...(clientUserId || Object.keys(customIdentifiers || {}).length > 0
            ? {
                externalIdentifiers: {
                    ...(clientUserId ? { clientUserId } : {}),
                    ...(Object.keys(customIdentifiers || {}).length > 0 ? { customIdentifiers } : {})
                }
            }
            : {})
    };
}
function formatItems(items) {
    return items.map(({ value, currency, ...rest }) => ({
        ...rest,
        price: currency !== undefined && currency !== null && currency !== '' ? [{ value, currency }] : [{ value }]
    }));
}
function formatLocale(locale) {
    const parts = locale.split('-');
    if (parts.length !== 2 || !parts[0] || !parts[1]) {
        throw new actions_core_1.PayloadValidationError('Invalid locale format. Expected format: "language-country" e.g. "en-US".');
    }
    const [language, country] = parts;
    return { language, country };
}
function formatEcommEventJSON(payload) {
    return {
        items: formatItems(payload.items),
        occurredAt: payload.occurredAt,
        user: formatUser(payload.userIdentifiers)
    };
}
function formatCustomEventJSON(payload) {
    const { externalEventId, type, properties, occurredAt, userIdentifiers } = payload;
    return {
        type,
        properties,
        externalEventId,
        occurredAt,
        user: formatUser(userIdentifiers)
    };
}
function formatUpsertUserAttributesJSON(payload) {
    const { properties, userIdentifiers } = payload;
    if (Object.values(properties ?? {}).some((value) => typeof value === 'object' || Array.isArray(value))) {
        throw new actions_core_1.PayloadValidationError('Properties cannot contain objects or arrays.');
    }
    return {
        properties,
        user: formatUser(userIdentifiers)
    };
}
function formatSubscribeUserJSON(payload) {
    const { externalEventId, occurredAt, userIdentifiers, subscriptionType, signUpSourceId, singleOptIn, locale } = payload;
    return {
        externalEventId,
        occurredAt,
        subscriptionType: subscriptionType,
        ...(locale ? { locale: formatLocale(locale) } : {}),
        ...(signUpSourceId ? { signUpSourceId } : {}),
        singleOptIn,
        user: formatUser(userIdentifiers)
    };
}
//# sourceMappingURL=functions.js.map