import { Table } from "apache-arrow";
import * as Effect from "effect/Effect";
import * as ManagedRuntime from "effect/ManagedRuntime";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import * as ArrowFlight from "./api/ArrowFlight.js";
import * as Arrow from "./Arrow.js";
export * as StudioModel from "./studio/Model.js";
/**
 * Admin client for managing service
 */
export * as Admin from "./api/Admin.js";
/**
 * Arrow Flight protocol implementation
 */
export * as ArrowFlight from "./api/ArrowFlight.js";
/**
 * Error types and utilities
 */
export * as ApiError from "./api/Error.js";
/**
 * Apache Arrow utilities and helpers
 */
export * as Arrow from "./Arrow.js";
/**
 * Data models and type definitions
 */
export * as Model from "./Model.js";
// TODO: Temporary re-export for backwards compatibility
export * from "./config.js";
/**
 * Create a query client
 *
 * Creates a client that connects to Amp using the provided transport.
 *
 * The client uses the Arrow Flight protocol for efficient data transfer and supports
 * both streaming and batch queries.
 *
 * @param transport - Transport for communicating with the Amp server
 * @returns Streaming and batch query client instance
 *
 * @example
 * ```typescript
 * import { createClient } from "@edgeandnode/amp"
 * import { createConnectTransport } from "@connectrpc/connect-node"
 *
 * const transport = createConnectTransport({
 *   baseUrl: "http://localhost:1602",
 *   httpVersion: "2",
 *   interceptors: [createAuthInterceptor("your-api-token")]
 * })
 *
 * const client = createClient(transport)
 * for await (const batch of client.stream("SELECT * FROM eth_rpc.logs")) {
 *   console.log(`Received ${batch.data.numRows} rows`)
 * }
 * ```
 */
export const createClient = (transport) => {
    const layer = ArrowFlight.layer(transport);
    const runtime = ManagedRuntime.make(layer);
    return {
        stream: (query, resume) => Effect.gen(function* () {
            const flight = yield* ArrowFlight.ArrowFlight;
            return flight.stream(query, resume).pipe(Stream.mapEffect(Effect.fn(function* (batch) {
                const schema = Schema.Array(Arrow.generateSchema(batch.data.schema));
                const table = new Table(batch.data);
                const data = yield* Schema.validate(schema)([...table]);
                return data;
            })), Stream.toAsyncIterable);
        }).pipe(runtime.runSync),
        query: (query) => Effect.gen(function* () {
            const flight = yield* ArrowFlight.ArrowFlight;
            return flight.query(query).pipe(Stream.mapEffect(Effect.fn(function* (batch) {
                const schema = Schema.Array(Arrow.generateSchema(batch.schema));
                const table = new Table(batch);
                const data = yield* Schema.validate(schema)([...table]);
                return data;
            })), Stream.toAsyncIterable);
        }).pipe(runtime.runSync),
    };
};
/**
 * Create an authentication interceptor
 *
 * Creates an interceptor that adds a Bearer token to all outgoing requests.
 *
 * Use this interceptor when connecting to an authenticated Amp server.
 *
 * @param token - Token to use for authentication
 * @returns Interceptor that adds Authorization header
 *
 * @example
 * ```typescript
 * import { createClient, createAuthInterceptor } from "@edgeandnode/amp"
 * import { createConnectTransport } from "@connectrpc/connect-node"
 *
 * const transport = createConnectTransport({
 *   baseUrl: "https://amp.example.com",
 *   httpVersion: "2",
 *   interceptors: [createAuthInterceptor("your-api-token")]
 * })
 *
 * const client = createClient(transport)
 * for await (const batch of client.query("SELECT * FROM eth_rpc.blocks LIMIT 10")) {
 *   console.log(batch)
 * }
 * ```
 */
export const createAuthInterceptor = (token) => {
    return (next) => (request) => {
        request.header.set("Authorization", `Bearer ${token}`);
        return next(request);
    };
};
//# sourceMappingURL=index.js.map