const structuredCloneFallback = (value) => {
    if (typeof globalThis.structuredClone === 'function') {
        try {
            return globalThis.structuredClone(value);
        }
        catch {
            // swallow clone failures – payload will fall back to shallow copy below
        }
    }
    try {
        return JSON.parse(JSON.stringify(value));
    }
    catch {
        return undefined;
    }
};
const sanitizePrimitive = (value) => {
    if (value === null || value === undefined) {
        return undefined;
    }
    if (typeof value === 'string') {
        const trimmed = value.trim();
        return trimmed.length > 0 ? trimmed : undefined;
    }
    if (typeof value === 'number' || typeof value === 'boolean') {
        return String(value);
    }
    if (value instanceof Date) {
        return value.toISOString();
    }
    try {
        return JSON.stringify(value);
    }
    catch {
        return undefined;
    }
};
const sanitizeConnection = (connection) => {
    if (!connection || typeof connection !== 'object') {
        return undefined;
    }
    const entries = {};
    for (const [rawKey, rawValue] of Object.entries(connection)) {
        if (!rawKey)
            continue;
        const key = rawKey.trim();
        if (!key)
            continue;
        const sanitized = sanitizePrimitive(rawValue);
        if (sanitized !== undefined) {
            entries[key] = sanitized;
        }
    }
    return Object.keys(entries).length > 0 ? entries : undefined;
};
const sanitizeClientHeaders = (headers) => {
    if (!headers || typeof headers !== 'object') {
        return undefined;
    }
    const entries = {};
    for (const [rawKey, rawValue] of Object.entries(headers)) {
        if (!rawKey)
            continue;
        const key = rawKey.trim();
        if (!key)
            continue;
        const sanitized = sanitizePrimitive(rawValue);
        if (sanitized !== undefined) {
            entries[key] = sanitized;
        }
    }
    return Object.keys(entries).length > 0 ? entries : undefined;
};
const sanitizeSettings = (settings) => {
    if (!settings || typeof settings !== 'object') {
        return undefined;
    }
    const cloned = structuredCloneFallback(settings);
    if (!cloned || typeof cloned !== 'object') {
        return undefined;
    }
    return cloned;
};
export const sanitizeStoredModel = (model) => {
    const cloned = structuredCloneFallback(model);
    const base = cloned && typeof cloned === 'object'
        ? cloned
        : { ...model };
    const sanitized = {
        ...base,
        connection: sanitizeConnection(base.connection) ?? {},
        clientHeaders: sanitizeClientHeaders(base.clientHeaders),
        settings: sanitizeSettings(base.settings)
    };
    if (!sanitized.label || typeof sanitized.label !== 'string') {
        sanitized.label = model.label;
    }
    if (!sanitized.model || typeof sanitized.model !== 'string') {
        sanitized.model = model.model;
    }
    return sanitized;
};
export const coerceStoredModels = (models) => {
    if (!Array.isArray(models)) {
        return [];
    }
    return models
        .map((model) => {
        if (!model || typeof model !== 'object') {
            return null;
        }
        const sanitized = sanitizeStoredModel(model);
        return sanitized;
    })
        .filter((model) => Boolean(model));
};
