var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _InteropAdmin_uuid;
import RouterClient from "../routerClient";
import Logger from "../logger";
import { INTENT_RESOLVER_CHANNEL, SERVER_CHANNEL, } from "./types";
import { ResolveError } from "@finos/fdc3";
export class InteropAdmin {
    constructor() {
        _InteropAdmin_uuid.set(this, window.name);
    }
    /**
     * Provides the resolver with app definitions (used to build intent map plus other functionality)
     * @param apps array of appD config definitions
     */
    async addAppDefinitions(apps) {
        const message = {
            type: "addAppDefinitions",
            meta: {
                uuid: __classPrivateFieldGet(this, _InteropAdmin_uuid, "f"),
            },
            payload: {
                apps,
            },
        };
        await RouterClient.query(SERVER_CHANNEL, message);
    }
    /**
     * Removes from the resolver a set of app definitions
     * @param apps array of app names to remove
     */
    async removeAppDefinitions(apps) {
        const message = {
            type: "removeAppDefinitions",
            meta: {
                uuid: __classPrivateFieldGet(this, _InteropAdmin_uuid, "f"),
            },
            payload: {
                apps,
            },
        };
        await RouterClient.query(SERVER_CHANNEL, message);
    }
    /**
     * Provides to InteropService addition window-service data needed for FDC3 registration.
     *
     * @param authenticationToken authentication token dynamically allocated by window service; required for authenticate the app
     * @param windowDescriptor app's window descriptor, which can carry Interop window data

     * @returns {Promise<void>}
     * @memberof InteropAdmin
     */
    async setInteropRegistrationData(windowDescriptor) {
        const message = {
            type: "setInteropRegistrationData",
            meta: {
                uuid: __classPrivateFieldGet(this, _InteropAdmin_uuid, "f"),
            },
            payload: {
                windowDescriptor,
            },
        };
        await RouterClient.query(SERVER_CHANNEL, message);
    }
    async registerUIResolverListener(handler) {
        RouterClient.addResponder(INTENT_RESOLVER_CHANNEL, async (error, queryMessage) => {
            let resolverResolution = null;
            let errorResolution = null;
            if (!error) {
                const choices = queryMessage === null || queryMessage === void 0 ? void 0 : queryMessage.data;
                try {
                    resolverResolution = await handler(choices);
                }
                catch (err) {
                    Logger.log(`registerUIResolverListener IntentResolution rejected ${err}`);
                    errorResolution = { error: ResolveError.ResolverUnavailable };
                }
                queryMessage === null || queryMessage === void 0 ? void 0 : queryMessage.sendQueryResponse(null, errorResolution || resolverResolution);
            }
            else {
                Promise.reject(`registerUIResolverListener failed. It can only be invoked once, system wide (router error: ${error})`);
            }
        });
        // let InteropService know that an intent resolver is available
        const message = {
            type: "registerUIResolver",
            meta: {
                uuid: __classPrivateFieldGet(this, _InteropAdmin_uuid, "f"),
            },
        };
        await RouterClient.query(SERVER_CHANNEL, message);
    }
}
_InteropAdmin_uuid = new WeakMap();
export default InteropAdmin;
//# sourceMappingURL=InteropAdmin.js.map