import { readAgentRuntimeDescriptorV1ForProvider } from '@happier-dev/protocol';
import { normalizeCodexBackendMode } from '../providerSettings/definitions/codex.js';
import { buildCodexRuntimeDescriptorProviderExtra, readCodexRuntimeDescriptorProviderExtra, } from './codexRuntimeDescriptorExtra.js';
import { normalizeOpenCodeBackendMode, normalizeOpenCodeServerBaseUrl, normalizeOpenCodeServerBaseUrlExplicit, } from '../providerSettings/definitions/opencode.js';
import { readOpenCodeRuntimeDescriptorProviderExtra, } from './opencodeRuntimeDescriptorExtra.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 buildCodexAgentRuntimeDescriptor(params) {
    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,
        providerId: 'codex',
        provider: {
            backendMode: params.backendMode,
            ...(vendorSessionId ? { vendorSessionId } : {}),
            ...(home ? { home } : {}),
            ...(connectedServiceId ? { connectedServiceId } : {}),
            ...(connectedServiceProfileId ? { connectedServiceProfileId } : {}),
            ...(homePath ? { homePath } : {}),
            providerExtra: {
                owner: 'codex',
                schemaId: 'codex.agentRuntimeDescriptorExtra',
                ...buildCodexRuntimeDescriptorProviderExtra({
                    backendMode: params.backendMode,
                    vendorSessionId,
                    home,
                    connectedServiceId,
                    connectedServiceProfileId,
                    homePath,
                }),
            },
        },
    };
}
export function buildOpenCodeAgentRuntimeDescriptor(params) {
    const vendorSessionId = normalizeTrimmedString(params.vendorSessionId);
    const rawServerBaseUrl = normalizeTrimmedString(params.serverBaseUrl);
    const serverBaseUrlExplicit = normalizeOpenCodeServerBaseUrlExplicit(params.serverBaseUrlExplicit);
    const serverBaseUrl = serverBaseUrlExplicit ? rawServerBaseUrl : null;
    return {
        v: 1,
        providerId: 'opencode',
        provider: {
            backendMode: params.backendMode,
            ...(vendorSessionId ? { vendorSessionId } : {}),
            ...(serverBaseUrl ? { serverBaseUrl } : {}),
            ...(serverBaseUrl && serverBaseUrlExplicit ? { serverBaseUrlExplicit: true } : {}),
            providerExtra: {
                owner: 'opencode',
                schemaId: 'opencode.agentRuntimeDescriptorExtra',
                v: 1,
                runtimeHandle: {
                    backendMode: params.backendMode,
                    ...(vendorSessionId ? { vendorSessionId } : {}),
                    ...(serverBaseUrl ? { serverBaseUrl } : {}),
                    ...(serverBaseUrl && serverBaseUrlExplicit ? { serverBaseUrlExplicit: true } : {}),
                },
            },
        },
    };
}
function normalizeCodexConnectedServiceFields(params) {
    if (params.home === 'connectedService') {
        return {
            connectedServiceId: params.connectedServiceId,
            connectedServiceProfileId: params.connectedServiceProfileId,
            homePath: params.homePath,
        };
    }
    return {
        connectedServiceId: null,
        connectedServiceProfileId: null,
        homePath: params.homePath,
    };
}
export function readSessionMetadataRuntimeDescriptor(metadata, providerId) {
    const metadataRecord = asRecord(metadata);
    if (!metadataRecord)
        return null;
    switch (providerId) {
        case 'codex': {
            const rawDescriptor = asRecord(metadataRecord.agentRuntimeDescriptorV1);
            const rawProvider = rawDescriptor?.providerId === 'codex' ? asRecord(rawDescriptor.provider) : null;
            const descriptor = readAgentRuntimeDescriptorV1ForProvider(metadataRecord.agentRuntimeDescriptorV1, 'codex');
            const providerExtra = readCodexRuntimeDescriptorProviderExtra(rawProvider?.providerExtra);
            const provider = descriptor?.provider ?? rawProvider;
            if (!provider)
                return null;
            const home = providerExtra?.home ?? normalizeCodexHome(provider.home);
            const codexConnectedServiceFields = normalizeCodexConnectedServiceFields({
                home,
                connectedServiceId: providerExtra?.connectedServiceId ?? normalizeTrimmedString(provider.connectedServiceId),
                connectedServiceProfileId: providerExtra?.connectedServiceProfileId ?? normalizeTrimmedString(provider.connectedServiceProfileId),
                homePath: providerExtra?.homePath ?? normalizeTrimmedString(provider.homePath),
            });
            return {
                providerId: 'codex',
                backendMode: providerExtra?.backendMode ?? normalizeCodexBackendMode(provider.backendMode),
                vendorSessionId: providerExtra?.vendorSessionId ?? normalizeTrimmedString(provider.vendorSessionId),
                home,
                connectedServiceId: codexConnectedServiceFields.connectedServiceId,
                connectedServiceProfileId: codexConnectedServiceFields.connectedServiceProfileId,
                homePath: codexConnectedServiceFields.homePath,
            };
        }
        case 'opencode': {
            const rawDescriptor = asRecord(metadataRecord.agentRuntimeDescriptorV1);
            const rawProvider = rawDescriptor?.providerId === 'opencode' ? asRecord(rawDescriptor.provider) : null;
            const descriptor = readAgentRuntimeDescriptorV1ForProvider(metadataRecord.agentRuntimeDescriptorV1, 'opencode');
            const providerExtra = readOpenCodeRuntimeDescriptorProviderExtra(rawProvider?.providerExtra);
            const provider = descriptor?.provider ?? rawProvider;
            if (!provider)
                return null;
            return {
                providerId: 'opencode',
                backendMode: providerExtra?.backendMode ?? normalizeOpenCodeBackendMode(provider.backendMode),
                vendorSessionId: providerExtra?.vendorSessionId ?? normalizeTrimmedString(provider.vendorSessionId),
                serverBaseUrl: providerExtra?.serverBaseUrl ?? normalizeOpenCodeServerBaseUrl(provider.serverBaseUrl),
                serverBaseUrlExplicit: providerExtra?.serverBaseUrlExplicit ?? normalizeOpenCodeServerBaseUrlExplicit(provider.serverBaseUrlExplicit),
            };
        }
        case 'pi': {
            const descriptor = readAgentRuntimeDescriptorV1ForProvider(metadataRecord.agentRuntimeDescriptorV1, 'pi');
            if (!descriptor)
                return null;
            return {
                providerId: 'pi',
                vendorSessionId: normalizeTrimmedString(descriptor.provider.vendorSessionId),
            };
        }
    }
}
//# sourceMappingURL=agentRuntimeDescriptor.js.map