/*!
 * Copyright 2017 - 2020 by ChartIQ, Inc.
 * All rights reserved.
 */
import { RouterClient } from "../clients/routerClient";
import { Logger } from "../clients/logger";
import { System } from "../common/system";
import { FSBLDependencyManagerSingleton as FSBLDependencyManager } from "../common/dependencyManager";
/**
 * <h2>Base Client</h2>
 * The Base Client is inherited by every client to provide common functionality to the clients. Once all dependencies are met, either client or service, the client's `onReady` method is fired.
 *
 * We're currently halfway through migrating our clients from extending a normal function prototype to an ES6 class.
 * "_BaseClient" represents the new class, while "BaseClient" is the original function. When the migration is complete,
 * we will remove the old function and rename "_BaseClient" to "BaseClient".

 * @param {Object} params
 * @param {Function} params.onReady - A function to be called after the client has initialized.
 * @param {String} params.name - The name of the client
    @example
    import { _BaseClient as BaseClient } from "./baseClient";
    var NewClient = function (params) {
        BaseClient.call(this, params);
        var self = this;

        return this;
    };

    var clientInstance = new NewClient({
        onReady: function (cb) {
            Logger.system.log("NewClient Online");
            cb();
        },
        name:"NewClient"
    });
    clientInstance.initialize();
    module.exports = clientInstance;
    @private
 */
export class _BaseClient {
    constructor(params) {
        /** The callback called when this service is ready. */
        this.onReadyCallback = (cb) => {
            cb();
        };
        // Flag is reset when the client is ready.
        this.isReady = false;
        this.startupDependencies = {
            services: [],
            clients: [],
        };
        /** Queue of functions registered via calls to `onReady()`, invoked once the client goes online. */
        this.clientReadyQueue = [];
        /** Gets the current window name. */
        this.windowName = "";
        /**
         * @private
         */
        this.processClientReadyQueue = () => {
            for (const cb of this.clientReadyQueue) {
                cb();
            }
            this.clientReadyQueue = [];
        };
        /**
         * @private
         */
        this.onReady = (cb) => {
            if (this.isReady)
                cb();
            else
                this.clientReadyQueue.push(cb);
        };
        /** Check to see if the client can come online. We check this against the required services and clients */
        /**
         * @private
         *
         */
        this.setClientOnline = () => {
            var _a;
            this.isReady = true;
            const onReadyMessage = `STARTUP:CLIENT:ONLINE:${(_a = this.finWindow) === null || _a === void 0 ? void 0 : _a.name}:${this.name}`;
            this.logger.system.log(onReadyMessage);
            const readyCB = () => {
                this.logger.system.debug(onReadyMessage);
                FSBLDependencyManager.setClientOnline(this.name);
                this.processClientReadyQueue();
            };
            this.onReadyCallback(readyCB);
        };
        /**
         * @private
         */
        this.initialize = (cb) => {
            var _a;
            this.logger.system.log("baseClient initialize", this.name);
            this.finWindow = System.Window.getCurrent();
            this.windowName = (_a = this.finWindow) === null || _a === void 0 ? void 0 : _a.name;
            this.logger.system.debug("baseClient Init Router Ready", this.name, "DEPENDENCIES", this.startupDependencies);
            FSBLDependencyManager.startup.waitFor(this.startupDependencies, () => {
                this.logger.system.log("baseClient initialize waitfor done", this.name);
                cb === null || cb === void 0 ? void 0 : cb();
                this.setClientOnline();
            }, `${this.windowName}(${this.name})`);
        };
        Logger.system.deprecated("BaseClient has been deprecated. There is no longer a need to create dependencies on Finsemble's built-in clients.");
        this.name = params.name;
        if (params.onReady)
            this.onReadyCallback = params.onReady;
        this.startupDependencies = params.startupDependencies || {
            services: [],
            clients: [],
        };
        this.logger = Logger;
        this.routerClient = RouterClient;
    }
    /**
     * @private
     */
    onClose(cb) {
        cb === null || cb === void 0 ? void 0 : cb();
    }
}
//# sourceMappingURL=baseClient.js.map