import { BloombergPanel, TerminalConnectService } from '../bloomberg/bloomberg.types';
import { ApiError } from '../errors/errors';
/**
 * Action to be executed in the Terminal.
 *
 * This can be a {@link BloombergTerminalFunction | Terminal function}, a {@link BloombergGroupUpdate | Launchpad group update} or `null` to do nothing.
 *
 * @group Types
 */
export type BloombergAction = BloombergTerminalFunction | BloombergGroupUpdate | null | undefined;
/**
 * Action that will update a Launchpad group’s security.
 *
 * @group Types
 */
export type BloombergGroupUpdate = {
    /** The name of the Launchpad group to update (e.g. “Group-A”). */
    group: string;
    /** The security to update the group with (e.g. “MSFT US Equity”). */
    security: string;
};
/**
 * Configuration determining which incoming {@link https://developers.openfin.co/of-docs/docs/interoperability-overview#context | context types} and {@link https://developers.openfin.co/of-docs/docs/interoperability-overview#intent | intents} will be handled by the API and their resultant actions in the Terminal.
 *
 * @group Types
 */
export type BloombergActionsConfig = {
    /** Context types that will be handled along with their corresponding actions. */
    contexts?: ContextActionMap;
    /** Intents that will be handled along with their corresponding actions. */
    intents?: IntentActionMap;
};
/**
 * A connection to the Bloomberg Terminal.
 *
 * @group Types
 */
export type BloombergConnection = {
    /** Terminates the connection and cleans up utilized resources. */
    disconnect: () => Promise<void>;
    /**
     * Executes a GraphQL request to the Bloomberg Terminal Connect API.
     *
     * Please refer to Bloomberg’s Terminal Connect API documentation for more information on executing Terminal Connect API requests.
     *
     * @typeParam T - A type that describes the requested data. This API includes a number of {@link TerminalConnectApiTypes | data types} for use with this function.
     *
     * @param query - The GraphQL query payload to execute.
     * @param service - The Terminal Connect API service endpoint to direct the request to. Defaults to the local service endpoint.
     *
     * @returns The data object of type {@link T} contained in the API response.
     */
    executeApiRequest: <T = unknown>(query: string, service?: TerminalConnectService) => Promise<T>;
};
/**
 * Configuration options for a Bloomberg Connection.
 *
 * @group Types
 */
export type BloombergConnectionConfig = {
    /** Provide an actions config to extend or override the default actions used by the API. */
    actions?: BloombergActionsConfig;
    /**
     * Provide a list of Launchpad group names (e.g. `['Group-A','Group-B']`) and an {@link https://fdc3.finos.org/docs/context/ref/Instrument | fdc3.instrument} context will be broadcast when one of the group’s security changes.
     *
     * By default (if no value is provided), the API listens to all Launchpad groups. To stop the API from broadcasting context on group updates, set this to `null`.
     */
    groups?: string[] | null;
    /** Set to `true` to disable all interop functionality for the connection. */
    interopDisabled?: boolean;
    /**
     * Function that will be executed whenever {@link https://developers.openfin.co/of-docs/docs/interoperability-overview#context | context } changes.
     *
     * @param context - The incoming context object that has triggered execution of the function.
     */
    onContextChanged?: (context: unknown) => void;
    /**
     * Function that will be executed whenever an API error occurs after the connection has been created.
     *
     * @param error - Error that was detected.
     */
    onError?: (error: ApiError) => void;
};
/**
 * Action that will execute a Bloomberg function in the specified Terminal panel.
 *
 * @group Types
 */
export type BloombergTerminalFunction = {
    /** Mnemonic code of the Terminal function to execute. */
    mnemonic: string;
    /** Securities to be used with the function. */
    securities?: string[];
    /** Tail to be used with the function. */
    tail?: string;
    /** The Terminal panel ID or named tab that will execute the function. */
    target: BloombergPanel | string;
};
/**
 * Function that accepts a context object as a parameter and returns an action to be executed in the Terminal.
 *
 * @param context - The incoming {@link https://developers.openfin.co/of-docs/docs/interoperability-overview#context | context } object that will be passed to the handler so that it can determine the appropriate action.
 *
 * @returns The action that will be executed in the Terminal, which can be a {@link BloombergTerminalFunction | Terminal function} or a {@link BloombergGroupUpdate | Launchpad group update}. Alternatively, returning `null` will do nothing.
 *
 * @group Types
 */
export type ContextActionHandler = (context: unknown) => BloombergAction | Promise<BloombergAction>;
/**
 * Action map associating context types with their corresponding actions.
 *
 * @example
 * ```typescript
 * import { BloombergConnectionConfig } from '@openfin/bloomberg';
 * import * as fdc3 from '@finos/fdc3';
 *
 * const options: BloombergConnectionConfig = {
 *  actions: {
 *    contexts: [
 *      [
 *        fdc3.ContextTypes.Instrument,
 *        (context) => ({ mnemonic: 'DES', securities: [(context as fdc3.Instrument).id.BBG], target: 0 }),
 *      ],
 *    ],
 *  },
 *};
 * ```
 *
 * @group Types
 */
export type ContextActionMap = [string, ContextActionHandler][];
/**
 * Action map associating intents with their corresponding context types and actions.
 *
 * @example
 * ```typescript
 * import { BloombergConnectionConfig } from '@openfin/bloomberg';
 * import * as fdc3 from '@finos/fdc3';
 *
 * const options: BloombergConnectionConfig = {
 *  actions: {
 *    intents: [
 *      [
 *        fdc3.Intents.ViewInstrument,
 *        [
 *          [
 *            fdc3.ContextTypes.Instrument,
 *            (context) => ({ mnemonic: 'DES', securities: [(context as fdc3.Instrument).id.BBG], target: 0 })
 *          ]
 *        ],
 *      ],
 *    ],
 *  },
 *};
 * ```
 *
 * @group Types
 */
export type IntentActionMap = [string, ContextActionMap | undefined][];
