import {IOGateway} from '@interopio/gateway';
import {FilePublisherConfig} from '@interopio/gateway/metrics/publisher/file';

export default GatewayServer.Factory;

export namespace GatewayServer {
    export const Factory: (options: ServerConfig) => Promise<Server>;
    export type SslConfig = {key?: string, cert?: string, ca?: string};
    export type OriginFilters = {
        non_matched?: IOGateway.Filtering.Action
        missing?: IOGateway.Filtering.Action
        block?: IOGateway.Filtering.Matcher[]
        allow?: IOGateway.Filtering.Matcher[]
        /**
         * @deprecated
         * @see block
         */
        blacklist?: IOGateway.Filtering.Matcher[]
        /**
         * @deprecated
         * @see allow
         */
        whitelist?: IOGateway.Filtering.Matcher[]
    }
    export import LoggerConfig = IOGateway.Logging.LogConfig;
    export type ServerConfig = {
        /**
         * The port to bind for network communication.
         * Accepts a single valuer or a range.
         * If a range is specified, will bind to the first available port in the range.
         *
         * Defaults to 0 - a random port.
         */
        port?: number | string
        /**
         * The network address.
         */
        host?: string
        ssl?: SslConfig,

        memory?: {
            memory_limit?: number
            dump_location?: string,
            dump_prefix?: string,
            report_interval?: number,
            max_backups?: number,
        },

        gateway?: IOGateway.GatewayConfig & {
            route?: string
            ping?: number
            origins?: OriginFilters
            limits?: {
                max_connections?: number
            },
        }
        mesh?: {
            timeout?: number // defaults to 60000
            ping?: number // defaults to 30000
        }
        metrics?: {
            file: FilePublisherConfig
        }
    }
    export interface Server {
        readonly gateway: IOGateway.Gateway
        close(): Promise<void>
    }
}
