// Trustindex widget loader — injects the Trustindex script once and lets it
// self-initialize. Pass `loaderUrl` (the script src from your Trustindex embed
// code) and optionally `widgetId` if you want to target a specific widget.
function TrustindexEmbed({ loaderUrl, widgetId, minHeight }) {
  const ref = React.useRef(null);

  React.useEffect(() => {
    if (!loaderUrl || !ref.current) return;
    const existing = document.querySelector(`script[data-trustindex-src="${loaderUrl}"]`);
    if (!existing) {
      const s = document.createElement('script');
      s.src = loaderUrl;
      s.async = true;
      s.defer = true;
      s.setAttribute('data-trustindex-src', loaderUrl);
      ref.current.appendChild(s);
    }
  }, [loaderUrl]);

  return (
    <div
      ref={ref}
      style={{ minHeight: minHeight || 320, width: '100%' }}
      {...(widgetId ? { 'data-trustindex-widget-id': widgetId } : {})}
    />
  );
}
Object.assign(window, { TrustindexEmbed });
