/*
 * Microsoft Application Insights XHR dependencies plugin, 3.4.3
 * Copyright (c) Microsoft and contributors. All rights reserved.
 *
 * Microsoft Application Insights Team
 * https://github.com/microsoft/ApplicationInsights-JS#readme
 *
 * ---------------------------------------------------------------------------
 * This is a single combined (rollup) declaration file for the package,
 * if you require a namespace wrapped version it is also available.
 * - Namespaced version: types/applicationinsights-dependencies-js.namespaced.d.ts
 * ---------------------------------------------------------------------------
 */

import { BaseTelemetryPlugin } from '@microsoft/applicationinsights-core-js';
import { DistributedTracingModes } from '@microsoft/applicationinsights-core-js';
import { eDistributedTracingModes } from '@microsoft/applicationinsights-core-js';
import { IAppInsightsCore } from '@microsoft/applicationinsights-core-js';
import { IConfig } from '@microsoft/applicationinsights-core-js';
import { IConfiguration } from '@microsoft/applicationinsights-core-js';
import { ICorrelationConfig } from '@microsoft/applicationinsights-core-js';
import { IDependencyTelemetry } from '@microsoft/applicationinsights-core-js';
import { IDistributedTraceContext } from '@microsoft/applicationinsights-core-js';
import { IPlugin } from '@microsoft/applicationinsights-core-js';
import { IProcessTelemetryContext } from '@microsoft/applicationinsights-core-js';
import { ITelemetryItem } from '@microsoft/applicationinsights-core-js';
import { ITelemetryPluginChain } from '@microsoft/applicationinsights-core-js';
import { IW3cTraceState } from '@microsoft/applicationinsights-core-js';

export declare class AjaxPlugin extends BaseTelemetryPlugin implements IAjaxMonitorPlugin {
    static identifier: string;
    identifier: string;
    priority: number;
    constructor();
    initialize(config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?: ITelemetryPluginChain): void;
    processTelemetry(item: ITelemetryItem, itemCtx?: IProcessTelemetryContext): void;
    /**
     * Logs dependency call
     * @param dependencyData - dependency data object
     */
    trackDependencyData(dependency: IDependencyTelemetry, properties?: {
        [key: string]: any;
    }): void;
    /**
     * Resets the ajax attempts counter. This is typically called on page view to allow a fresh set of ajax calls to be tracked.
     */
    resetAjaxAttempts(): void;
    includeCorrelationHeaders(ajaxData: IAjaxRecordData, input?: Request | string, init?: RequestInit, xhr?: XMLHttpRequestInstrumented): any;
    /**
     * Add an ajax listener which is called just prior to the request being sent and before the correlation headers are added, to allow you
     * to access the headers and modify the values used to generate the distributed tracing correlation headers.
     * @param dependencyListener - The Telemetry Initializer function
     * @returns - A IDependencyListenerHandler to enable the initializer to be removed
     */
    addDependencyListener(dependencyListener: DependencyListenerFunction): IDependencyListenerHandler;
    /**
     * Add an dependency telemetry initializer callback function to allow populating additional properties or drop the request.
     * It is called after the dependency call has completed and any available performance details are available. A dependency
     * initializer is similar to the TelemetryInitializer function but it allows you to block the reporting of the dependency
     * request so that it doesn't count against the `maxAjaxCallsPerView`.
     * @param dependencyInitializer - The Dependency Telemetry Initializer function
     * @returns - A IDependencyInitializerHandler to enable the initializer to be removed
     */
    addDependencyInitializer(dependencyInitializer: DependencyInitializerFunction): IDependencyInitializerHandler;
    /**
     * Protected function to allow sub classes the chance to add additional properties to the dependency event
     * before it's sent. This function calls track, so sub-classes must call this function after they have
     * populated their properties.
     * @param dependencyData - dependency data object
     */
    protected trackDependencyDataInternal(dependency: IDependencyTelemetry, properties?: {
        [key: string]: any;
    }, systemProperties?: {
        [key: string]: any;
    }): void;
}

/**
 * The initializer function that will be called, if it returns false the event will be dropped and not reported
 * or counted against the `maxAjaxCallsPerView`.
 */
export declare type DependencyInitializerFunction = (item: IDependencyInitializerDetails) => boolean | void;

/**
 * The function that will get called when the ajax request is about to occur.
 */
export declare type DependencyListenerFunction = (dependencyDetails: IDependencyListenerDetails) => boolean | void;

export declare const DfltAjaxCorrelationHeaderExDomains: string[];

export { DistributedTracingModes }

declare const _DYN_INCLUDE_CORRELATION_0 = "includeCorrelationHeaders";

export { eDistributedTracingModes }

/**
 * Interface for the Ajax Monitor Plugin that extends IPlugin and includes ajax specific functionality.
 * This interface is used for proper typing when retrieving the plugin via getPlugin().
 */
export declare interface IAjaxMonitorPlugin extends IPlugin, IDependenciesPlugin, IInstrumentationRequirements, IDependencyListenerContainer {
    /**
     * Resets the ajax attempts counter. This is typically called on page view to allow a fresh set of ajax calls to be tracked.
     */
    resetAjaxAttempts(): void;
}

/**
 * Interface for ajax data passed to includeCorrelationHeaders function.
 * Contains the public properties and methods needed for correlation header processing.
 */
export declare interface IAjaxRecordData {
    /**
     * Gets the absolute URL for the request
     * @returns The absolute URL string or null
     */
    getAbsoluteUrl(): string | null;
    /**
     * Gets the sanitized path name for the request URL
     * @returns The sanitized path name string or null
     */
    getPathName(): string | null;
    /**
     * The distributed trace context for the request containing trace ID, span ID, and trace flags
     */
    readonly traceCtx: IDistributedTraceContext;
    /**
     * Object containing request headers that have been set for this request
     */
    requestHeaders: {
        [key: string]: string;
    };
    /**
     * Indicates whether the request was aborted (0 = not aborted, 1 = aborted)
     */
    aborted: number;
    /**
     * Optional context object that can be set by dependency listeners
     */
    context?: {
        [key: string]: any;
    };
}

/**
 * Internal interface that extends the public IAjaxRecordData with additional properties and methods
 * used internally by the AJAX monitoring implementation.
 *
 * @internal
 */
declare interface IAjaxRecordInternal extends IAjaxRecordData {
    /**
     * Indicates if the ajax call has completed
     */
    completed: boolean;
    /**
     * Size of the request headers in bytes
     */
    requestHeadersSize: number;
    /**
     * Duration of receiving the response in milliseconds
     */
    responseReceivingDuration: number;
    /**
     * Duration of the callback execution in milliseconds
     */
    callbackDuration: number;
    /**
     * Total duration of the ajax call in milliseconds
     */
    ajaxTotalDuration: number;
    /**
     * URL of the page that initiated the request
     */
    pageUrl: string;
    /**
     * The URL of the request
     */
    requestUrl: string;
    /**
     * Size of the request in bytes
     */
    requestSize: number;
    /**
     * HTTP method used for the request
     */
    method: string;
    /**
     * Performance mark associated with this request
     */
    perfMark: PerformanceMark;
    /**
     * Performance timing data from the Resource Timing API
     */
    perfTiming: PerformanceResourceTiming;
    /**
     * Number of attempts to find performance data
     */
    perfAttempts?: number;
    /**
     * Indicates if the request was made asynchronously
     */
    async?: boolean;
    /**
     * Should the Error Status text be included in the response
     */
    errorStatusText?: boolean;
    /**
     * HTTP status code of the response
     */
    status: string | number;
    /**
     * Timestamp when the request was sent
     */
    requestSentTime: number;
    /**
     * Timestamp when the first byte was received
     */
    responseStartedTime: number;
    /**
     * Timestamp when the last byte was received
     */
    responseFinishedTime: number;
    /**
     * Timestamp when the onreadystatechange callback finished
     */
    callbackFinishedTime: number;
    /**
     * Timestamp when the ajax call ended
     */
    endTime: number;
    /**
     * State tracking object for XHR monitoring
     */
    xhrMonitoringState: IXHRMonitoringState;
    /**
     * Indicates if a JavaScript exception occurred in xhr.onreadystatechange code (1 if occurred, 0 otherwise)
     */
    clientFailure: number;
    /**
     * Creates a telemetry item for tracking this ajax request
     * @param ajaxType - Type of the ajax request
     * @param enableRequestHeaderTracking - Whether to include request headers in telemetry
     * @param getResponse - Function to get response data
     * @returns Dependency telemetry item or null
     */
    CreateTrackItem(ajaxType: string, enableRequestHeaderTracking: boolean, getResponse: () => IAjaxRecordResponse): IDependencyTelemetry;
    /**
     * Gets Part A properties for telemetry
     * @returns Object containing Part A properties or null
     */
    getPartAProps(): {
        [key: string]: any;
    };
}

declare interface IAjaxRecordResponse {
    statusText: string;
    headerMap: Object;
    correlationContext: string;
    type?: string;
    responseText?: string;
    response?: Object;
}

export { ICorrelationConfig }

export declare interface IDependenciesPlugin extends IDependencyListenerContainer {
    /**
     * Logs dependency call
     * @param dependencyData - dependency data object
     */
    trackDependencyData(dependency: IDependencyTelemetry): void;
}

export declare interface IDependencyHandler {
    remove(): void;
}

export declare interface IDependencyInitializerDetails {
    /**
     * The DependencyTelemetry event that will be passed to the `trackDependencyDataInternal` function.
     */
    item: IDependencyTelemetry;
    /**
     * Additional properties to be added to the event
     */
    properties?: {
        [key: string]: any;
    };
    /**
     * Additional system properties to be added to the event.
     */
    sysProperties?: {
        [key: string]: any;
    };
    /**
     * The context that the application can assigned via the dependency listener(s)
     */
    context?: {
        [key: string]: any;
    };
    /**
     * [Optional] A flag that indicates whether the client request was manually aborted by the `abort()`
     */
    aborted?: boolean;
}

export declare interface IDependencyInitializerHandler extends IDependencyHandler {
}

declare interface IDependencyListenerContainer {
    /**
     * Add an ajax listener which is called just prior to the request being sent and before the correlation headers are added.
     * This allows you to access the headers and modify the values used to generate the distributed tracing correlation headers (added in v2.8.4),
     * or to drop the correlation (added in v3.3.7).
     * @param dependencyListener - The Telemetry Initializer function
     * @returns - A IDependencyListenerHandler to enable the initializer to be removed
     */
    addDependencyListener(dependencyListener: DependencyListenerFunction): IDependencyListenerHandler;
}

export declare interface IDependencyListenerDetails {
    /**
     * The current core instance
     */
    core: IAppInsightsCore;
    /**
     * Provided only if the dependency request is an XHR call
     */
    xhr?: XMLHttpRequest;
    /**
     * Provided only if the dependency request is a fetch call, this is the input argument being used,
     * re-assigning this value has not affect on the value used for the request, however, when this is a Request
     * object changing the value of the Request will be used for the outbound request.
     */
    input?: Request | string;
    /**
     * Provided only if the dependency request is a fetch call, this is the init argument being used,
     * re-assigning this value does not change the value used for the request, however, changing properties
     * of this object will be used.
     */
    init?: RequestInit;
    /**
     * Returns the unique identifier for a trace. All requests / spans from the same trace share the same traceId.
     * Must be read from incoming headers or generated according to the W3C TraceContext specification,
     * in a hex representation of 16-byte array. A.k.a. trace-id, TraceID or Distributed TraceID
     */
    traceId?: string;
    /**
     * Self-generated 8-bytes identifier of the incoming request. Must be a hex representation of 8-byte array.
     * Also know as the parentId, used to link requests together
     */
    spanId?: string;
    /**
     * An integer representation of the W3C TraceContext trace-flags.
     * https://www.w3.org/TR/trace-context/#trace-flags
     */
    traceFlags?: number;
    /**
     * The W3C TraceState object that contains the trace state information, this is mutable and changes made to this
     * instance will be reflected in the distributed trace context. You cannot overwrite the traceState, but you can
     * modify the values within the traceState.
     */
    readonly traceState?: IW3cTraceState;
    /**
     * [Optional] Context that the application can assign that will also be passed to any dependency initializer
     */
    context?: {
        [key: string]: any;
    };
    /**
     * [Optional] A flag that indicates whether the client request was manually aborted by the `abort()`,
     * as listeners are called just before the request is sent it is unlikely that an application would have
     * called `abort` before `send` this is also available in the dependency initializer.
     */
    aborted?: boolean;
}

export declare interface IDependencyListenerHandler extends IDependencyHandler {
}

export declare interface IInstrumentationRequirements extends IDependenciesPlugin {
    [_DYN_INCLUDE_CORRELATION_0]: (ajaxData: IAjaxRecordData, input?: Request | string, init?: RequestInit, xhr?: XMLHttpRequestInstrumented) => any;
}

/**
 * Interface defining the XHR monitoring state properties
 */
declare interface IXHRMonitoringState {
    openDone: boolean;
    setRequestHeaderDone: boolean;
    sendDone: boolean;
    abortDone: boolean;
    stateChangeAttached: boolean;
}

export declare interface XMLHttpRequestData {
    /**
     * The "Shared" XHR headers, avoids causing multiple instances
     */
    xh?: Array<{
        n: string;
        v: string;
    }>;
    /**
     * The individual tracking data for each AI instance
     */
    i: {
        [key: string]: IAjaxRecordInternal;
    };
}

export declare interface XMLHttpRequestInstrumented extends XMLHttpRequest {
    _ajaxData: XMLHttpRequestData;
}

export { }
