import { normalizeCodexBackendMode } from '../providerSettings/definitions/codex.js';
function asRecord(value) {
    if (!value || typeof value !== 'object' || Array.isArray(value))
        return null;
    return value;
}
function normalizeTrimmedString(value) {
    if (typeof value !== 'string')
        return null;
    const trimmed = value.trim();
    return trimmed || null;
}
function normalizeCodexHome(value) {
    return value === 'user' || value === 'connectedService' ? value : null;
}
export function buildCodexRuntimeDescriptorProviderExtra(params) {
    const backendMode = normalizeCodexBackendMode(params.backendMode);
    const vendorSessionId = normalizeTrimmedString(params.vendorSessionId);
    const home = normalizeCodexHome(params.home);
    const connectedServiceId = home === 'connectedService' ? normalizeTrimmedString(params.connectedServiceId) : null;
    const connectedServiceProfileId = home === 'connectedService'
        ? normalizeTrimmedString(params.connectedServiceProfileId)
        : null;
    const homePath = normalizeTrimmedString(params.homePath);
    return {
        v: 1,
        runtimeAffinity: {
            ...(backendMode ? { backendMode } : {}),
            ...(vendorSessionId ? { vendorSessionId } : {}),
            ...(home ? { home } : {}),
            ...(connectedServiceId ? { connectedServiceId } : {}),
            ...(connectedServiceProfileId ? { connectedServiceProfileId } : {}),
            ...(homePath ? { homePath } : {}),
        },
    };
}
export function readCodexRuntimeDescriptorProviderExtra(value) {
    const extra = asRecord(value);
    if (!extra || extra.v !== 1)
        return null;
    const runtimeAffinity = asRecord(extra.runtimeAffinity);
    if (!runtimeAffinity)
        return null;
    const home = normalizeCodexHome(runtimeAffinity.home);
    const normalizedRuntimeAffinity = {
        backendMode: normalizeCodexBackendMode(runtimeAffinity.backendMode),
        vendorSessionId: normalizeTrimmedString(runtimeAffinity.vendorSessionId),
        home,
        connectedServiceId: home === 'connectedService' ? normalizeTrimmedString(runtimeAffinity.connectedServiceId) : null,
        connectedServiceProfileId: home === 'connectedService'
            ? normalizeTrimmedString(runtimeAffinity.connectedServiceProfileId)
            : null,
        homePath: normalizeTrimmedString(runtimeAffinity.homePath),
    };
    if (!normalizedRuntimeAffinity.backendMode
        && !normalizedRuntimeAffinity.vendorSessionId
        && !normalizedRuntimeAffinity.home
        && !normalizedRuntimeAffinity.homePath) {
        return null;
    }
    return normalizedRuntimeAffinity;
}
//# sourceMappingURL=codexRuntimeDescriptorExtra.js.map