"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyCurrency = verifyCurrency;
exports.verifyParams = verifyParams;
exports.verifyUserProps = verifyUserProps;
exports.convertTimestamp = convertTimestamp;
const actions_core_1 = require("@segment/actions-core");
const constants_1 = require("./constants");
function verifyCurrency(currency) {
    if (!constants_1.CURRENCY_ISO_CODES.includes(currency.toUpperCase())) {
        throw new actions_core_1.IntegrationError(`${currency} is not a valid currency code.`, 'Incorrect value format', 400);
    }
}
function verifyParams(params) {
    if (!params) {
        return;
    }
    Object.entries(params).forEach(([key, value]) => {
        if (value instanceof Array) {
            throw new actions_core_1.IntegrationError(`Param [${key}] has unsupported value of type [Array]. GA4 does not accept null, array, or object values for event parameters and item parameters.`, 'Invalid value', 400);
        }
        else if (value == null) {
            throw new actions_core_1.IntegrationError(`Param [${key}] has unsupported value of type [NULL]. GA4 does not accept null, array, or object values for event parameters and item parameters.`, 'Invalid value', 400);
        }
        else if (typeof value == 'object') {
            throw new actions_core_1.IntegrationError(`Param [${key}] has unsupported value of type [${typeof value}]. GA4 does not accept null, array, or object values for event parameters and item parameters.`, 'Invalid value', 400);
        }
    });
}
function verifyUserProps(userProperties) {
    if (!userProperties) {
        return;
    }
    Object.entries(userProperties).forEach(([key, value]) => {
        if (value instanceof Array) {
            throw new actions_core_1.IntegrationError(`Param [${key}] has unsupported value of type [Array]. GA4 does not accept array or object values for user properties.`, 'Invalid value', 400);
        }
        else if (value != null && typeof value == 'object') {
            throw new actions_core_1.IntegrationError(`Param [${key}] has unsupported value of type [${typeof value}]. GA4 does not accept array or object values for user properties.`, 'Invalid value', 400);
        }
    });
}
function convertTimestamp(timestamp) {
    if (!timestamp) {
        return undefined;
    }
    if (!isNaN(+timestamp)) {
        return +timestamp;
    }
    return Date.parse(timestamp) * 1000;
}
//# sourceMappingURL=ga4-functions.js.map