import { normalizeCodexBackendMode } from '../providerSettings/definitions/codex.js';
import { readSessionMetadataRuntimeDescriptor } from './agentRuntimeDescriptor.js';
function asRecord(value) {
    if (!value || typeof value !== 'object' || Array.isArray(value))
        return null;
    return value;
}
function hasGenericCodexState(metadata) {
    return [
        'sessionModesV1',
        'sessionModelsV1',
        'sessionConfigOptionsV1',
        'acpSessionModesV1',
        'acpSessionModelsV1',
        'acpConfigOptionsV1',
    ].some((key) => {
        const value = asRecord(metadata?.[key]);
        return value?.provider === 'codex';
    });
}
function readCodexRuntimeDescriptorV1BackendMode(value) {
    const descriptor = asRecord(value);
    if (!descriptor || descriptor.v !== 1)
        return null;
    return normalizeCodexBackendMode(descriptor.backendMode);
}
export function resolvePersistedCodexRuntimeIdentity(metadata) {
    const metadataRecord = asRecord(metadata);
    if (!metadataRecord)
        return null;
    const genericDescriptor = readSessionMetadataRuntimeDescriptor(metadataRecord, 'codex');
    const genericMode = genericDescriptor?.backendMode ?? null;
    if (genericMode) {
        return { backendMode: genericMode };
    }
    const descriptorMode = readCodexRuntimeDescriptorV1BackendMode(metadataRecord.codexRuntimeDescriptorV1);
    if (descriptorMode) {
        return { backendMode: descriptorMode };
    }
    const affinityMode = normalizeCodexBackendMode(asRecord(metadataRecord.affinity)?.backendMode);
    if (affinityMode) {
        return { backendMode: affinityMode };
    }
    const persistedMode = normalizeCodexBackendMode(metadataRecord.codexBackendMode);
    if (persistedMode) {
        return { backendMode: persistedMode };
    }
    const directSession = asRecord(metadataRecord.directSessionV1);
    const nestedMode = normalizeCodexBackendMode(directSession?.codexBackendMode);
    if (nestedMode) {
        return { backendMode: nestedMode };
    }
    const codexSessionId = typeof metadataRecord.codexSessionId === 'string' ? metadataRecord.codexSessionId.trim() : '';
    if (codexSessionId && hasGenericCodexState(metadataRecord)) {
        return { backendMode: 'appServer' };
    }
    return null;
}
export function resolvePersistedCodexVendorSessionId(metadata) {
    const metadataRecord = asRecord(metadata);
    if (!metadataRecord)
        return null;
    const genericDescriptor = readSessionMetadataRuntimeDescriptor(metadataRecord, 'codex');
    const genericVendorSessionId = genericDescriptor?.vendorSessionId ?? '';
    if (genericVendorSessionId) {
        return genericVendorSessionId;
    }
    const legacyVendorSessionId = typeof metadataRecord.codexSessionId === 'string' ? metadataRecord.codexSessionId.trim() : '';
    return legacyVendorSessionId || null;
}
export function buildCodexSpawnRuntimeAffinityCompatFields(runtimeIdentity) {
    const backendMode = runtimeIdentity?.backendMode ?? null;
    if (!backendMode) {
        return {};
    }
    return {
        codexBackendMode: backendMode,
    };
}
//# sourceMappingURL=codexRuntimeIdentity.js.map