import * as http from 'http';
import { IncomingHttpHeaders } from 'http';
import { HighlightOptions } from 'highlight.run';
import { Attributes } from '@opentelemetry/api';
import { ResourceAttributes } from '@opentelemetry/resources';
import { Span, BufferConfig } from '@opentelemetry/sdk-trace-base';
import { BatchSpanProcessorBase } from '@opentelemetry/sdk-trace-base/build/src/export/BatchSpanProcessorBase';
import { NodeSDK } from '@opentelemetry/sdk-node';

interface NodeOptions extends HighlightOptions {
    /**
     * ID used to associate payloads with a Highlight project.
     */
    projectID: string;
    /**
     * The endpoint string to send opentelemetry data to.
     * @default https://otel.highlight.io:4318
     */
    otlpEndpoint?: string;
    /**
     * This app's service name.
     */
    serviceName?: string;
    /**
     * This app's version ideally set to the latest deployed git SHA.
     */
    serviceVersion?: string;
    /**
     * Enables node fs instrumentation @default false
     * see .
     * {@link https://opentelemetry.io/docs/instrumentation/js/libraries/#registration}
     */
    enableFsInstrumentation?: boolean;
}

declare const HIGHLIGHT_REQUEST_HEADER = "x-highlight-request";
interface HighlightInterface {
    init: (options: NodeOptions) => void;
    stop: () => Promise<void>;
    isInitialized: () => boolean;
    parseHeaders: (headers: IncomingHttpHeaders) => {
        secureSessionId: string;
        requestId: string;
    } | undefined;
    consumeError: (error: Error, secureSessionId?: string, requestId?: string, metadata?: Attributes) => void;
    recordMetric: (secureSessionId: string, name: string, value: number, requestId?: string, tags?: {
        name: string;
        value: string;
    }[]) => void;
    flush: () => Promise<void>;
    log: (message: any, level: string, secureSessionId?: string | undefined, requestId?: string | undefined, metadata?: Attributes) => void;
    consumeAndFlush: (error: Error, secureSessionId?: string, requestId?: string, metadata?: Attributes) => Promise<void>;
    setAttributes: (attributes: ResourceAttributes) => void;
    _debug: (...data: any[]) => void;
}
declare const H: HighlightInterface;

declare class CustomSpanProcessor extends BatchSpanProcessorBase<BufferConfig> {
    private _listeners;
    constructor(config: any, options: any);
    onShutdown(): void;
    forceFlush(): Promise<void>;
    registerListener(listener: () => void): () => boolean;
}
declare class Highlight {
    readonly FLUSH_TIMEOUT = 10;
    _intervalFunction: ReturnType<typeof setInterval>;
    _projectID: string;
    _debug: boolean;
    otel: NodeSDK;
    private tracer;
    private processor;
    constructor(options: NodeOptions);
    get activeSpanProcessor(): CustomSpanProcessor;
    get finishedSpans(): Span[];
    stop(): Promise<void>;
    _log(...data: any[]): void;
    recordMetric(secureSessionId: string, name: string, value: number, requestId?: string, tags?: {
        name: string;
        value: string;
    }[]): void;
    log(date: Date, msg: string, level: string, stack: object, secureSessionId?: string, requestId?: string, metadata?: Attributes): void;
    consumeCustomError(error: Error, secureSessionId: string | undefined, requestId: string | undefined, metadata?: Attributes): void;
    flush(): Promise<void>;
    waitForFlush(): Promise<void>;
    setAttributes(attributes: Attributes): void;
}

/** JSDoc */
interface MiddlewareError extends Error {
    status?: number | string;
    statusCode?: number | string;
    status_code?: number | string;
    output?: {
        statusCode?: number | string;
    };
}
/**
 * Express compatible error handler.
 * Exposed as `Handlers.errorHandler`.
 * `metadata` accepts structured tags that should be attached to every error.
 */
declare function errorHandler(options: NodeOptions, metadata?: Attributes): (error: MiddlewareError, req: http.IncomingMessage, res: http.ServerResponse, next: (error: MiddlewareError) => void) => void;
/**
 * A TRPC compatible error handler for logging errors to Highlight.
 * `metadata` accepts structured tags that should be attached to every error.
 */
declare function trpcOnError({ error, req, }: {
    error: Error;
    req: {
        headers?: http.IncomingHttpHeaders;
    };
}, options: NodeOptions, metadata?: Attributes): Promise<void>;
/**
 * A wrapper for logging errors to Highlight for Firebase HTTP functions.
 * `metadata` accepts structured tags that should be attached to every error.
 */
declare type FirebaseHttpFunctionHandler = (req: any, resp: any) => void | Promise<void>;
declare function firebaseHttpFunctionHandler(origHandler: FirebaseHttpFunctionHandler, options: NodeOptions, metadata?: Attributes): FirebaseHttpFunctionHandler;
/**
 * A wrapper for logging errors to Highlight for Firebase callable functions.
 * `metadata` accepts structured tags that should be attached to every error.
 */
declare type FirebaseCallableFunctionHandler = (data: any, context: any) => any;
declare function firebaseCallableFunctionHandler(origHandler: FirebaseCallableFunctionHandler, options: NodeOptions, metadata?: Attributes): FirebaseCallableFunctionHandler;
/**
 * A wrapper for logging errors to Highlight for AWS Lambda and other serverless functions.
 * `metadata` accepts structured tags that should be attached to every error.
 */
declare type ServerlessCallableFunctionHandler = (event?: any) => any;
declare function serverlessFunction(origHandler: ServerlessCallableFunctionHandler, options: NodeOptions, metadata?: Attributes): ServerlessCallableFunctionHandler;

declare const handlers_errorHandler: typeof errorHandler;
declare const handlers_trpcOnError: typeof trpcOnError;
declare const handlers_firebaseHttpFunctionHandler: typeof firebaseHttpFunctionHandler;
declare const handlers_firebaseCallableFunctionHandler: typeof firebaseCallableFunctionHandler;
declare const handlers_serverlessFunction: typeof serverlessFunction;
declare namespace handlers {
  export {
    handlers_errorHandler as errorHandler,
    handlers_trpcOnError as trpcOnError,
    handlers_firebaseHttpFunctionHandler as firebaseHttpFunctionHandler,
    handlers_firebaseCallableFunctionHandler as firebaseCallableFunctionHandler,
    handlers_serverlessFunction as serverlessFunction,
  };
}

export { H, HIGHLIGHT_REQUEST_HEADER, handlers as Handlers, Highlight, NodeOptions };
