"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPublicUuid = createPublicUuid;
exports.createPlainToken = createPlainToken;
exports.hashToken = hashToken;
exports.renderTemplate = renderTemplate;
exports.summarizePayload = summarizePayload;
exports.sanitizeSensitiveData = sanitizeSensitiveData;
exports.normalizeJsonObject = normalizeJsonObject;
const crypto_1 = require("crypto");
const Handlebars = __importStar(require("handlebars"));
function createPublicUuid() {
    return (0, crypto_1.randomUUID)();
}
function createPlainToken() {
    return (0, crypto_1.randomBytes)(32).toString('hex');
}
function hashToken(token) {
    return (0, crypto_1.createHash)('sha256').update(token).digest('hex');
}
function renderTemplate(source, context) {
    if (source === undefined || source === null) {
        return source;
    }
    if (typeof source === 'string') {
        return Handlebars.compile(source)(context);
    }
    if (Array.isArray(source)) {
        return source.map((item) => renderTemplate(item, context));
    }
    if (typeof source === 'object') {
        return Object.fromEntries(Object.entries(source).map(([key, value]) => [
            key,
            renderTemplate(value, context),
        ]));
    }
    return source;
}
function summarizePayload(value) {
    if (value === undefined || value === null) {
        return null;
    }
    try {
        const serialized = JSON.stringify(value);
        if (serialized.length <= 4000) {
            return value;
        }
        return {
            truncated: true,
            preview: serialized.slice(0, 4000),
        };
    }
    catch (_a) {
        return {
            unsupported: true,
        };
    }
}
const SENSITIVE_KEY_PATTERN = /(token|authorization|apikey|api_key|password|secret|credential|passwd)/i;
function maskString(value) {
    if (!value) {
        return value;
    }
    if (/^Bearer\s+/i.test(value)) {
        return 'Bearer [REDACTED]';
    }
    if (value.length <= 8) {
        return '[REDACTED]';
    }
    return `${value.slice(0, 3)}***${value.slice(-2)}`;
}
function sanitizeSensitiveData(value, keyPath = '') {
    if (value === null || value === undefined) {
        return value;
    }
    if (Array.isArray(value)) {
        return value.map((item, index) => sanitizeSensitiveData(item, `${keyPath}[${index}]`));
    }
    if (typeof value === 'object') {
        return Object.fromEntries(Object.entries(value).map(([key, child]) => {
            const nextPath = keyPath ? `${keyPath}.${key}` : key;
            if (SENSITIVE_KEY_PATTERN.test(key)) {
                if (typeof child === 'string') {
                    return [key, maskString(child)];
                }
                return [key, '[REDACTED]'];
            }
            return [key, sanitizeSensitiveData(child, nextPath)];
        }));
    }
    if (typeof value === 'string' && SENSITIVE_KEY_PATTERN.test(keyPath)) {
        return maskString(value);
    }
    return value;
}
function normalizeJsonObject(value) {
    if (!value) {
        return null;
    }
    if (typeof value === 'string') {
        const trimmed = value.trim();
        if (!trimmed) {
            return null;
        }
        return JSON.parse(trimmed);
    }
    return value;
}
//# sourceMappingURL=webhook-utils.js.map