import { normalizeOpenCodeBackendMode, normalizeOpenCodeServerBaseUrl, normalizeOpenCodeServerBaseUrlExplicit, } from '../providerSettings/definitions/opencode.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 normalizeOptionalOpenCodeBackendMode(value) {
    if (value === 'server' || value === 'acp')
        return value;
    if (typeof value !== 'string')
        return null;
    const trimmed = value.trim().toLowerCase();
    return trimmed === 'server' || trimmed === 'acp' ? trimmed : null;
}
export function buildOpenCodeRuntimeDescriptorProviderExtra(params) {
    const backendMode = normalizeOpenCodeBackendMode(params.backendMode);
    const vendorSessionId = normalizeTrimmedString(params.vendorSessionId);
    const serverBaseUrlExplicit = normalizeOpenCodeServerBaseUrlExplicit(params.serverBaseUrlExplicit);
    const serverBaseUrl = serverBaseUrlExplicit ? normalizeOpenCodeServerBaseUrl(params.serverBaseUrl) : null;
    return {
        v: 1,
        runtimeHandle: {
            ...(backendMode ? { backendMode } : {}),
            ...(vendorSessionId ? { vendorSessionId } : {}),
            ...(serverBaseUrl ? { serverBaseUrl } : {}),
            ...(serverBaseUrl && serverBaseUrlExplicit ? { serverBaseUrlExplicit: true } : {}),
        },
    };
}
export function readOpenCodeRuntimeDescriptorProviderExtra(value) {
    const extra = asRecord(value);
    if (!extra || extra.v !== 1)
        return null;
    const runtimeHandle = asRecord(extra.runtimeHandle);
    if (!runtimeHandle)
        return null;
    const normalizedRuntimeHandle = {
        backendMode: normalizeOptionalOpenCodeBackendMode(runtimeHandle.backendMode),
        vendorSessionId: normalizeTrimmedString(runtimeHandle.vendorSessionId),
        serverBaseUrl: normalizeOpenCodeServerBaseUrl(runtimeHandle.serverBaseUrl),
        serverBaseUrlExplicit: normalizeOpenCodeServerBaseUrlExplicit(runtimeHandle.serverBaseUrlExplicit),
    };
    if (!normalizedRuntimeHandle.backendMode &&
        !normalizedRuntimeHandle.vendorSessionId &&
        !normalizedRuntimeHandle.serverBaseUrl) {
        return null;
    }
    return normalizedRuntimeHandle;
}
//# sourceMappingURL=opencodeRuntimeDescriptorExtra.js.map