import { jsx as _jsx } from "react/jsx-runtime";
import React from "react";
import styled from "styled-components";
import { widget as Widget } from "../../third-party/charting_library";
import { UDFCompatibleDatafeed } from "../../third-party/datafeeds/udf";
import DefaultDataFeed from "./datafeed";
const Container = styled.div `
  height: ${(props) => props?.height ?? 500}px;
  width: ${(props) => props?.width ?? 600}px;
  transition: width 0.2s ease-in-out;

  iframe {
    height: 100% !important;
    width: 100%;
  }
`;
const defaultSymbolCreator = (token) => `Bitfinex:${token.toUpperCase()}/USD`;
const TradingViewWidget = React.forwardRef(({ width, height, url, token, datafeed = DefaultDataFeed, symbolCreator = defaultSymbolCreator, saveLoadAdapter, cssURL, }, ref) => {
    const [widget, setWidget] = React.useState();
    const container = React.useRef(null);
    const prev = React.useRef({ token });
    React.useEffect(() => {
        if (container.current) {
            const widget = new Widget({
                symbol: symbolCreator(prev.current.token),
                interval: "1D",
                locale: "en",
                fullscreen: true,
                container: container.current,
                datafeed,
                library_path: url ?? "",
                save_load_adapter: saveLoadAdapter,
                custom_css_url: cssURL ?? "",
            });
            setWidget(widget);
        }
    }, [datafeed, symbolCreator, saveLoadAdapter]);
    React.useEffect(() => {
        if (widget && token !== prev.current.token) {
            try {
                widget.setSymbol(symbolCreator(token), "1D", () => { });
            }
            catch (e) {
                if (e instanceof Error) {
                    console.error(e.message);
                }
            }
        }
    }, [widget, token, symbolCreator]);
    React.useEffect(() => {
        if (ref && widget) {
            if (typeof ref === "function") {
                ref(widget);
            }
            else {
                ref.current = widget;
            }
        }
    }, [widget]);
    React.useEffect(() => {
        prev.current.token = token;
    }, [token]);
    React.useEffect(() => {
        console.log("CHANGED");
    });
    return (_jsx(Container, { width: width, height: height, className: "tradingview-external-widget", ref: container }));
});
export { UDFCompatibleDatafeed };
export default TradingViewWidget;
//# sourceMappingURL=index.js.map