/* eslint-disable */
// AUTO-GENERATED by scripts/generate-realm-sdk.mjs. DO NOT EDIT.
import { REALM_OPERATION_MAP, REALM_SERVICE_METHODS } from './operation-map.js';
function hasValue(value) {
    return value !== undefined && value !== null;
}
function encodePathValue(value) {
    return encodeURIComponent(String(value));
}
function isRecord(value) {
    return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
}
function isServiceCallOptions(value) {
    if (!isRecord(value)) {
        return false;
    }
    const keys = Object.keys(value);
    if (keys.length === 0) {
        return false;
    }
    if (!keys.every((key) => key === 'headers' || key === 'timeoutMs' || key === 'signal')) {
        return false;
    }
    if ('timeoutMs' in value) {
        const timeoutMs = value.timeoutMs;
        if (timeoutMs !== undefined && !(typeof timeoutMs === 'number' && Number.isFinite(timeoutMs) && timeoutMs > 0)) {
            return false;
        }
    }
    if ('headers' in value) {
        const headers = value.headers;
        if (headers !== undefined && !isRecord(headers)) {
            return false;
        }
    }
    if ('signal' in value) {
        const signal = value.signal;
        if (signal !== undefined && signal !== null && typeof signal !== 'object') {
            return false;
        }
    }
    return true;
}
function bindOperationInput(definition, args) {
    const values = [...args];
    const trailing = values[values.length - 1];
    const options = isServiceCallOptions(trailing) ? trailing : undefined;
    if (options) {
        values.pop();
    }
    let index = 0;
    const query = {};
    const headerParams = {};
    let path = definition.path;
    for (const parameter of definition.parameters) {
        const value = values[index];
        index += 1;
        if (parameter.in === 'path') {
            if (!hasValue(value)) {
                if (parameter.required) {
                    throw new Error(`missing required path param: ${parameter.name}`);
                }
                continue;
            }
            const placeholder = `{${parameter.name}}`;
            path = path.includes(placeholder)
                ? path.replaceAll(placeholder, encodePathValue(value))
                : path;
            continue;
        }
        if (parameter.in === 'query') {
            if (!hasValue(value)) {
                if (parameter.required) {
                    throw new Error(`missing required query param: ${parameter.name}`);
                }
                continue;
            }
            query[parameter.name] = value;
            continue;
        }
        if (parameter.in === 'header') {
            if (!hasValue(value)) {
                if (parameter.required) {
                    throw new Error(`missing required header param: ${parameter.name}`);
                }
                continue;
            }
            headerParams[parameter.name] = String(value);
            continue;
        }
    }
    const body = definition.hasBody ? values[index] : undefined;
    if (definition.hasBody) {
        if (definition.bodyRequired && !hasValue(body)) {
            throw new Error('missing required request body');
        }
        index += 1;
    }
    const headers = {
        ...headerParams,
        ...(options?.headers || {}),
    };
    return {
        method: definition.method,
        path,
        query: Object.keys(query).length > 0 ? query : undefined,
        body,
        headers: Object.keys(headers).length > 0 ? headers : undefined,
        timeoutMs: options?.timeoutMs,
        signal: options?.signal,
    };
}
export function createRealmServiceRegistry(execute) {
    const registry = {};
    for (const serviceName of Object.keys(REALM_SERVICE_METHODS)) {
        const methodMap = REALM_SERVICE_METHODS[serviceName];
        const handle = {};
        for (const methodName of Object.keys(methodMap)) {
            const operationKey = methodMap[methodName];
            const definition = REALM_OPERATION_MAP[operationKey];
            handle[String(methodName)] = async (...args) => execute(bindOperationInput(definition, args));
        }
        registry[String(serviceName)] = handle;
    }
    return registry;
}
//# sourceMappingURL=service-registry.js.map