import { createNimiError } from './runtime/errors.js';
import { ReasonCode } from './types/index.js';
import { createLocalFirstPartyRuntimePlatformClient, createPlatformClient, } from './platform-client.js';
function unavailable(input) {
    return {
        status: input.status,
        mode: input.mode,
        reasonCode: input.reasonCode,
        actionHint: input.actionHint,
        message: input.message,
    };
}
function normalizeText(value) {
    return String(value || '').trim();
}
function assertSupportedMode(mode) {
    if (mode === 'local-first-party' || mode === 'third-party-nimi-app' || mode === 'dev-standalone') {
        return;
    }
    throw createNimiError({
        message: `unsupported Nimi App auth mode: ${mode || '(empty)'}`,
        reasonCode: ReasonCode.SDK_AUTH_MODE_INVALID,
        actionHint: 'use_supported_nimi_app_auth_mode',
        source: 'sdk',
    });
}
function toSharedPlatformInput(input) {
    return {
        appId: input.appId,
        realmBaseUrl: input.realmBaseUrl,
        runtimeTransport: input.runtimeTransport,
        runtimeDefaults: input.runtimeDefaults,
        runtimeOptions: input.runtimeOptions,
        realmFetchImpl: input.realmFetchImpl,
    };
}
export async function createNimiAppRuntimePlatformClient(input) {
    const mode = normalizeText(input.mode);
    assertSupportedMode(mode);
    if (mode === 'local-first-party') {
        try {
            const client = await createLocalFirstPartyRuntimePlatformClient(toSharedPlatformInput(input));
            return {
                status: 'ready',
                mode,
                client,
                auth: {
                    state: 'ready',
                    source: 'runtime-local-first-party',
                },
            };
        }
        catch (error) {
            const reasonCode = typeof error === 'object' && error !== null && 'reasonCode' in error
                ? String(error.reasonCode || ReasonCode.RUNTIME_UNAVAILABLE)
                : ReasonCode.RUNTIME_UNAVAILABLE;
            return unavailable({
                status: 'action-required',
                mode,
                reasonCode,
                actionHint: 'complete_runtime_local_first_party_account_setup',
                message: error instanceof Error ? error.message : 'local first-party Runtime account setup is required',
            });
        }
    }
    if (mode === 'third-party-nimi-app') {
        return unavailable({
            status: 'unavailable',
            mode,
            reasonCode: ReasonCode.SDK_RUNTIME_METHOD_UNAVAILABLE,
            actionHint: 'wait_for_runtime_nimi_app_session_projection',
            message: 'third-party Nimi App Runtime session projection is not exposed by this SDK/runtime pair',
        });
    }
    const developerSession = input.mode === 'dev-standalone' ? input.developerSession : null;
    if (!developerSession
        || developerSession.source !== 'runtime-developer-session'
        || !normalizeText(developerSession.sessionId)
        || !normalizeText(developerSession.sessionToken)) {
        return unavailable({
            status: 'action-required',
            mode,
            reasonCode: ReasonCode.PRINCIPAL_UNAUTHORIZED,
            actionHint: 'provide_runtime_developer_session',
            message: 'dev-standalone mode requires an explicit Runtime developer app session',
        });
    }
    if (input.runtimeTransport === null) {
        return unavailable({
            status: 'unavailable',
            mode,
            reasonCode: ReasonCode.SDK_RUNTIME_METHOD_UNAVAILABLE,
            actionHint: 'configure_runtime_transport',
            message: 'dev-standalone mode requires Runtime transport support',
        });
    }
    const client = await createPlatformClient({
        ...toSharedPlatformInput(input),
        runtimeDefaults: {
            ...input.runtimeDefaults,
            callerKind: 'third-party-app',
        },
        runtimeAppSession: {
            sessionId: developerSession.sessionId,
            sessionToken: developerSession.sessionToken,
        },
        allowAnonymousRealm: true,
    });
    return {
        status: 'ready',
        mode,
        client,
        auth: {
            state: 'ready',
            source: 'runtime-developer-session',
        },
    };
}
//# sourceMappingURL=nimi-app-runtime-platform-client.js.map