"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.smartHash = exports.parseDateSafe = exports.parseNumberSafe = exports.emptyStringToUndefined = exports.splitListValueToArray = exports.emptyObjectToUndefined = exports.box = exports.raiseMisconfiguredRequiredFieldErrorIfNullOrUndefined = exports.raiseMisconfiguredRequiredFieldErrorIf = exports.isNullOrUndefined = void 0;
const actions_core_1 = require("@segment/actions-core");
const hashing_utils_1 = require("../../../lib/hashing-utils");
const isNullOrUndefined = (v) => v == null;
exports.isNullOrUndefined = isNullOrUndefined;
const raiseMisconfiguredRequiredFieldErrorIf = (condition, message) => {
    if (condition) {
        throw new actions_core_1.IntegrationError(message, 'Misconfigured required field', 400);
    }
};
exports.raiseMisconfiguredRequiredFieldErrorIf = raiseMisconfiguredRequiredFieldErrorIf;
const raiseMisconfiguredRequiredFieldErrorIfNullOrUndefined = (v, message) => (0, exports.raiseMisconfiguredRequiredFieldErrorIf)((0, exports.isNullOrUndefined)(v), message);
exports.raiseMisconfiguredRequiredFieldErrorIfNullOrUndefined = raiseMisconfiguredRequiredFieldErrorIfNullOrUndefined;
const box = (v) => (v ?? '').length > 0 ? [v] : undefined;
exports.box = box;
const emptyObjectToUndefined = (v) => {
    const properties = Object.getOwnPropertyNames(v);
    if (properties.length === 0) {
        return undefined;
    }
    for (const prop of properties) {
        if (v[prop] !== undefined) {
            return v;
        }
    }
    return undefined;
};
exports.emptyObjectToUndefined = emptyObjectToUndefined;
const splitListValueToArray = (input) => {
    const separator = input.includes(';') ? ';' : ',';
    const result = input
        .split(separator)
        .map((x) => x.trim())
        .filter((x) => x != '');
    return result.length > 0 ? result : undefined;
};
exports.splitListValueToArray = splitListValueToArray;
const emptyStringToUndefined = (v) => {
    const trimmed = v?.trim();
    return (trimmed ?? '').length > 0 ? trimmed : undefined;
};
exports.emptyStringToUndefined = emptyStringToUndefined;
const parseNumberSafe = (v) => {
    if (Number.isSafeInteger(v)) {
        return v;
    }
    else if (v != null) {
        const parsed = Number.parseInt(String(v) ?? '');
        return Number.isSafeInteger(parsed) ? parsed : undefined;
    }
    return undefined;
};
exports.parseNumberSafe = parseNumberSafe;
const parseDateSafe = (v) => {
    const parsed = Date.parse(v ?? '');
    return Number.isSafeInteger(parsed) ? parsed : undefined;
};
exports.parseDateSafe = parseDateSafe;
const smartHash = (value, cleaningFunction) => {
    if (value === undefined)
        return;
    return (0, hashing_utils_1.processHashing)(value, 'sha256', 'hex', cleaningFunction);
};
exports.smartHash = smartHash;
//# sourceMappingURL=utils.js.map