"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatConsent = void 0;
exports.verifyCurrency = verifyCurrency;
exports.verifyParams = verifyParams;
exports.verifyUserProps = verifyUserProps;
exports.convertTimestamp = convertTimestamp;
exports.getMobileStreamParams = getMobileStreamParams;
exports.getWebStreamParams = getWebStreamParams;
exports.sendData = sendData;
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.`, actions_core_1.ErrorCodes.INVALID_CURRENCY_CODE, 400);
    }
}
function verifyParams(params) {
    if (!params) {
        return;
    }
    Object.entries(params).forEach(([key, value]) => {
        if (key == 'items' && value instanceof Array)
            return;
        if (value instanceof Array) {
            throw new actions_core_1.PayloadValidationError(`Param [${key}] has unsupported value of type [Array]. GA4 does not accept null, array, or object values for event parameters and item parameters.`);
        }
        else if (value == null) {
            throw new actions_core_1.PayloadValidationError(`Param [${key}] has unsupported value of type [NULL]. GA4 does not accept null, array, or object values for event parameters and item parameters.`);
        }
        else if (typeof value == 'object') {
            throw new actions_core_1.PayloadValidationError(`Param [${key}] has unsupported value of type [${typeof value}]. GA4 does not accept null, array, or object values for event parameters and item parameters.`);
        }
    });
}
function verifyUserProps(userProperties) {
    if (!userProperties) {
        return;
    }
    Object.entries(userProperties).forEach(([key, value]) => {
        if (value instanceof Array) {
            throw new actions_core_1.PayloadValidationError(`Param [${key}] has unsupported value of type [Array]. GA4 does not accept array or object values for user properties.`);
        }
        else if (value != null && typeof value == 'object') {
            throw new actions_core_1.PayloadValidationError(`Param [${key}] has unsupported value of type [${typeof value}]. GA4 does not accept array or object values for user properties.`);
        }
    });
}
function convertTimestamp(timestamp) {
    if (!timestamp) {
        return undefined;
    }
    if (!isNaN(+timestamp)) {
        return +timestamp;
    }
    return Date.parse(timestamp) * 1000;
}
function getMobileStreamParams(api_secret, firebase_app_id, app_instance_id) {
    if (!firebase_app_id) {
        throw new actions_core_1.PayloadValidationError(`Firebase App ID is required for mobile app streams`);
    }
    if (!app_instance_id) {
        throw new actions_core_1.PayloadValidationError(`Firebase App Instance ID is required for mobile app streams`);
    }
    return {
        search_params: `api_secret=${api_secret}&firebase_app_id=${firebase_app_id}`,
        identifier: {
            app_instance_id
        }
    };
}
function getWebStreamParams(api_secret, measurement_id, client_id) {
    if (!measurement_id) {
        throw new actions_core_1.PayloadValidationError(`Measurement ID is required for web streams`);
    }
    if (!client_id) {
        throw new actions_core_1.PayloadValidationError(`Client ID is required for web streams`);
    }
    return {
        search_params: `api_secret=${api_secret}&measurement_id=${measurement_id}`,
        identifier: {
            client_id
        }
    };
}
async function sendData(request, search_params, payload) {
    return request(`https://www.google-analytics.com/mp/collect?${search_params}`, {
        method: 'POST',
        json: payload
    });
}
const formatConsent = (consent) => {
    if (!consent.ad_user_data_consent && !consent.ad_personalization_consent) {
        return undefined;
    }
    return {
        consent: {
            ad_user_data: consent.ad_user_data_consent,
            ad_personalization: consent.ad_personalization_consent
        }
    };
};
exports.formatConsent = formatConsent;
//# sourceMappingURL=ga4-functions.js.map