import React, { useEffect, useState } from "react";
import { RelayDataType, RelayTransmissionType } from "../../../platform/services/bloombergBridge/lib/types";
import { Button } from "../common/Button";
import { BloombergLink } from "./BloombergLink";
import "./bloombergFDC3.css";
import { FEAGlobals } from "../../../common";
const BLOOMBERG_ERROR_MESSAGE = "Error received when looking for Bloomberg Bridge Service link prefences";
const PREFERENCE_FIELD_FOR_LINKS = "finsemble.custom.bloomberg.links";
const getLinks = async () => {
    var _a;
    const { err, data: prefs } = await FEAGlobals.FSBL.Clients.ConfigClient.getPreferences();
    if (err) {
        FEAGlobals.FSBL.Clients.Logger.error(BLOOMBERG_ERROR_MESSAGE, err);
        return [];
    }
    return (_a = prefs === null || prefs === void 0 ? void 0 : prefs[PREFERENCE_FIELD_FOR_LINKS]) !== null && _a !== void 0 ? _a : [];
};
const updateLinks = async (links) => {
    FEAGlobals.FSBL.Clients.ConfigClient.setPreference({
        field: PREFERENCE_FIELD_FOR_LINKS,
        value: links,
    });
};
export const BloombergFDC3 = () => {
    const [links, setLinks] = useState([]);
    const linksWereUpdated = (err, { value }) => {
        if (err) {
            FEAGlobals.FSBL.Clients.Logger.error(BLOOMBERG_ERROR_MESSAGE, err);
        }
        setLinks(value);
    };
    useEffect(() => {
        (async () => {
            setLinks(await getLinks());
            FEAGlobals.FSBL.Clients.ConfigClient.addListener({ field: PREFERENCE_FIELD_FOR_LINKS }, linksWereUpdated);
        })();
        return () => {
            FEAGlobals.FSBL.Clients.ConfigClient.removeListener({ field: PREFERENCE_FIELD_FOR_LINKS }, linksWereUpdated);
        };
    }, []);
    return (React.createElement("div", null,
        React.createElement("div", null,
            React.createElement("h3", null, "Bloomberg FDC3 Integration (Experimental)"),
            React.createElement("p", null, "Note: Bloomberg to FDC3 interoperability is currently an experimental feature."),
            React.createElement(Button, { fashion: "primary", text: "Add New Connection", onClick: () => {
                    updateLinks([
                        {
                            bidirectional: false,
                            source: {
                                id: "",
                                type: RelayTransmissionType.FDC3Intent,
                                data: RelayDataType.FDC3Instrument,
                            },
                            target: {
                                id: "",
                                type: RelayTransmissionType.LaunchPadGroup,
                                data: RelayDataType.BloombergSecurity,
                            },
                        },
                        ...links,
                    ]);
                } })),
        React.createElement("div", { className: "bloomberg-links" }, links.map((link, index) => (React.createElement(BloombergLink, { key: index, link: link, onChange: (newConfig) => {
                const newLinkList = links.slice(0);
                newLinkList[index] = newConfig;
                updateLinks(newLinkList);
            }, onDelete: () => {
                const newLinkList = links.filter((lnk, i) => index !== i);
                updateLinks(newLinkList);
            } }))))));
};
//# sourceMappingURL=BloombergFDC3.js.map