import { jsx as _jsx } from "react/jsx-runtime";
import React, { useEffect, forwardRef, useRef } from "react";
export const Widget = /*#__PURE__*/ forwardRef(({ scriptHTML, scriptSRC, containerId, type }, ref)=>{
    // const containerRef: { current: HTMLDivElement | null } = createRef();
    const containerRef = useRef(null);
    useEffect(()=>{
        let refValue;
        if (containerRef.current) {
            const script = document.createElement("script");
            script.setAttribute("data-nscript", "afterInteractive");
            script.src = scriptSRC;
            script.async = true;
            script.type = "text/javascript";
            if (type === "Widget" || type === "MediumWidget") {
                script.onload = ()=>{
                    if (typeof TradingView !== undefined) {
                        // script.innerHTML = JSON.stringify(
                        type === "Widget" ? ref = new TradingView.widget(scriptHTML) : type === "MediumWidget" ? ref = new TradingView.MediumWidget(scriptHTML) : undefined;
                    // );
                    }
                };
            } else {
                script.innerHTML = JSON.stringify(scriptHTML);
            }
            containerRef.current.appendChild(script);
            refValue = containerRef.current;
        }
        return ()=>{
            if (refValue) {
                while(refValue.firstChild){
                    refValue.removeChild(refValue.firstChild);
                }
            }
        };
    }, [
        containerRef,
        scriptHTML,
        type,
        scriptSRC
    ]);
    return /*#__PURE__*/ _jsx("div", {
        className: "orderly-w-full orderly-h-full",
        ref: containerRef,
        id: containerId
    });
});

//# sourceMappingURL=widget.js.map