/**
 * Sanitizes record property values to improve readability while preserving removed URLs.
 */
function sanitizePrimitive(value) {
    if (value === null || value === undefined) {
        return { value, removedUrls: [] };
    }
    if (typeof value !== "string") {
        return { value, removedUrls: [] };
    }
    let cleaned = value.trim();
    const removedUrls = [];
    cleaned = cleaned.replace(/\[([^\]]+)\]\((https?:\/\/(?:www\.)?notion\.so\/[^\s)]+)\)/gi, (_, label, url) => {
        removedUrls.push(url);
        return label;
    });
    cleaned = cleaned.replace(/\s*\((https?:\/\/(?:www\.)?notion\.so\/[^\s)]+)\)\s*/gi, (_, url) => {
        removedUrls.push(url);
        return " ";
    });
    cleaned = cleaned.replace(/\s+/g, " ").trim();
    if ((cleaned.startsWith('"') && cleaned.endsWith('"')) ||
        (cleaned.startsWith("'") && cleaned.endsWith("'"))) {
        cleaned = cleaned.slice(1, -1).trim();
    }
    if (!cleaned.length) {
        return { value: undefined, removedUrls };
    }
    return { value: cleaned, removedUrls };
}
export function sanitizePropertyValue(value) {
    return sanitizePrimitive(value).value;
}
function mergeUrlValue(target, key, urls, plural) {
    if (!urls.length)
        return;
    const needsPlural = plural || urls.length > 1;
    const urlKey = `${key}${needsPlural ? "_urls" : "_url"}`;
    const incoming = needsPlural ? urls : urls[0];
    if (target[urlKey] === undefined) {
        target[urlKey] = incoming;
        return;
    }
    const ensureArray = (input) => Array.isArray(input) ? input : [input];
    const merged = new Set(ensureArray(target[urlKey]));
    ensureArray(incoming).forEach((url) => merged.add(url));
    target[urlKey] = Array.from(merged);
}
export function sanitizeRecordProperties(properties) {
    const sanitized = {};
    for (const [key, value] of Object.entries(properties)) {
        if (Array.isArray(value)) {
            const sanitizedItems = [];
            const collectedUrls = [];
            value.forEach((item) => {
                if (typeof item === "string") {
                    const result = sanitizePrimitive(item);
                    if (result.value !== undefined) {
                        sanitizedItems.push(result.value);
                    }
                    collectedUrls.push(...result.removedUrls);
                }
                else if (Array.isArray(item)) {
                    sanitizedItems.push(item);
                }
                else if (typeof item === "object" && item !== null && !(item instanceof Date)) {
                    sanitizedItems.push(sanitizeRecordProperties(item));
                }
                else if (item !== undefined) {
                    sanitizedItems.push(item);
                }
            });
            sanitized[key] = sanitizedItems;
            mergeUrlValue(sanitized, key, collectedUrls, true);
        }
        else if (typeof value === "object" && value !== null && !(value instanceof Date)) {
            sanitized[key] = sanitizeRecordProperties(value);
        }
        else {
            const result = sanitizePrimitive(value);
            if (result.value !== undefined) {
                sanitized[key] = result.value;
            }
            mergeUrlValue(sanitized, key, result.removedUrls, false);
        }
    }
    return sanitized;
}
//# sourceMappingURL=property_sanitizer.js.map