(function(){
  const { useEffect, useId, useRef, useState } = React;

  function GlassSurfaceStock({
    children,
    width = 200,
    height = 80,
    borderRadius = 20,
    borderWidth = 0.07,
    brightness = 50,
    opacity = 0.93,
    blur = 11,
    displace = 0,
    backgroundOpacity = 0,
    saturation = 1,
    distortionScale = -180,
    redOffset = 0,
    greenOffset = 10,
    blueOffset = 20,
    xChannel = 'R',
    yChannel = 'G',
    mixBlendMode = 'difference',
    className = '',
    style = {}
  }){
    const uniqueId = useId().replace(/:/g, '-');
    const filterId = 'glass-filter-' + uniqueId;
    const redGradId = 'red-grad-' + uniqueId;
    const blueGradId = 'blue-grad-' + uniqueId;

    const containerRef = useRef(null);
    const feImageRef = useRef(null);
    const redChannelRef = useRef(null);
    const greenChannelRef = useRef(null);
    const blueChannelRef = useRef(null);
    const gaussianBlurRef = useRef(null);
    const [svgSupported, setSvgSupported] = useState(false);

    function supportsSVGFilters(){
      if (typeof window === 'undefined' || typeof document === 'undefined') return false;
      const isChromium = /Chrome|Chromium|HeadlessChrome|CriOS|Edg/.test(navigator.userAgent);
      const isWebkit = /Safari/.test(navigator.userAgent) && !isChromium;
      const isFirefox = /Firefox/.test(navigator.userAgent);
      return !isWebkit && !isFirefox;
    }

    function generateDisplacementMap(){
      const rect = containerRef.current && containerRef.current.getBoundingClientRect();
      const actualWidth = (rect && rect.width) || (typeof width === 'number' ? width : 400);
      const actualHeight = (rect && rect.height) || (typeof height === 'number' ? height : 200);
      const edgeSize = Math.min(actualWidth, actualHeight) * (borderWidth * 0.5);
      const innerWidth = Math.max(0, actualWidth - edgeSize * 2);
      const innerHeight = Math.max(0, actualHeight - edgeSize * 2);
      const svgContent =
        '<svg viewBox="0 0 ' + actualWidth + ' ' + actualHeight + '" xmlns="http://www.w3.org/2000/svg">' +
          '<defs>' +
            '<linearGradient id="' + redGradId + '" x1="100%" y1="0%" x2="0%" y2="0%">' +
              '<stop offset="0%" stop-color="#0000"/>' +
              '<stop offset="100%" stop-color="red"/>' +
            '</linearGradient>' +
            '<linearGradient id="' + blueGradId + '" x1="0%" y1="0%" x2="0%" y2="100%">' +
              '<stop offset="0%" stop-color="#0000"/>' +
              '<stop offset="100%" stop-color="blue"/>' +
            '</linearGradient>' +
          '</defs>' +
          '<rect x="0" y="0" width="' + actualWidth + '" height="' + actualHeight + '" fill="black"></rect>' +
          '<rect x="0" y="0" width="' + actualWidth + '" height="' + actualHeight + '" rx="' + borderRadius + '" fill="url(#' + redGradId + ')" />' +
          '<rect x="0" y="0" width="' + actualWidth + '" height="' + actualHeight + '" rx="' + borderRadius + '" fill="url(#' + blueGradId + ')" style="mix-blend-mode: ' + mixBlendMode + '" />' +
          '<rect x="' + edgeSize + '" y="' + edgeSize + '" width="' + innerWidth + '" height="' + innerHeight + '" rx="' + borderRadius + '" fill="hsl(0 0% ' + brightness + '% / ' + opacity + ')" style="filter:blur(' + blur + 'px)" />' +
        '</svg>';
      return 'data:image/svg+xml,' + encodeURIComponent(svgContent);
    }

    function updateDisplacementMap(){
      if (feImageRef.current) feImageRef.current.setAttribute('href', generateDisplacementMap());
    }

    useEffect(function(){
      updateDisplacementMap();
      [
        { ref: redChannelRef, offset: redOffset },
        { ref: greenChannelRef, offset: greenOffset },
        { ref: blueChannelRef, offset: blueOffset }
      ].forEach(function(item){
        if (!item.ref.current) return;
        item.ref.current.setAttribute('scale', String(distortionScale + item.offset));
        item.ref.current.setAttribute('xChannelSelector', xChannel);
        item.ref.current.setAttribute('yChannelSelector', yChannel);
      });
      if (gaussianBlurRef.current) gaussianBlurRef.current.setAttribute('stdDeviation', String(displace));
    }, [width, height, borderRadius, borderWidth, brightness, opacity, blur, displace, distortionScale, redOffset, greenOffset, blueOffset, xChannel, yChannel, mixBlendMode]);

    useEffect(function(){
      if (!containerRef.current || typeof ResizeObserver === 'undefined') return;
      const resizeObserver = new ResizeObserver(function(){ setTimeout(updateDisplacementMap, 0); });
      resizeObserver.observe(containerRef.current);
      return function(){ resizeObserver.disconnect(); };
    }, []);

    useEffect(function(){ setTimeout(updateDisplacementMap, 0); }, [width, height]);
    useEffect(function(){ setSvgSupported(supportsSVGFilters()); }, []);

    const containerStyle = Object.assign({}, style, {
      width: typeof width === 'number' ? width + 'px' : width,
      height: typeof height === 'number' ? height + 'px' : height,
      borderRadius: borderRadius + 'px',
      '--glass-frost': backgroundOpacity,
      '--glass-saturation': saturation,
      '--filter-id': 'url(#' + filterId + ')'
    });

    return (
      <div
        ref={containerRef}
        className={'glass-surface ' + (svgSupported ? 'glass-surface--svg' : 'glass-surface--fallback') + ' ' + className}
        style={containerStyle}
      >
        <svg className="glass-surface__filter" xmlns="http://www.w3.org/2000/svg">
          <defs>
            <filter id={filterId} colorInterpolationFilters="sRGB" x="0%" y="0%" width="100%" height="100%">
              <feImage ref={feImageRef} x="0" y="0" width="100%" height="100%" preserveAspectRatio="none" result="map" />
              <feDisplacementMap ref={redChannelRef} in="SourceGraphic" in2="map" result="dispRed" />
              <feColorMatrix in="dispRed" type="matrix" values={'1 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 1 0'} result="red" />
              <feDisplacementMap ref={greenChannelRef} in="SourceGraphic" in2="map" result="dispGreen" />
              <feColorMatrix in="dispGreen" type="matrix" values={'0 0 0 0 0\n0 1 0 0 0\n0 0 0 0 0\n0 0 0 1 0'} result="green" />
              <feDisplacementMap ref={blueChannelRef} in="SourceGraphic" in2="map" result="dispBlue" />
              <feColorMatrix in="dispBlue" type="matrix" values={'0 0 0 0 0\n0 0 0 0 0\n0 0 1 0 0\n0 0 0 1 0'} result="blue" />
              <feBlend in="red" in2="green" mode="screen" result="rg" />
              <feBlend in="rg" in2="blue" mode="screen" result="output" />
              <feGaussianBlur ref={gaussianBlurRef} in="output" stdDeviation="0.7" />
            </filter>
          </defs>
        </svg>
        <div className="glass-surface__content">{children}</div>
      </div>
    );
  }

  window.GlassSurfaceStock = GlassSurfaceStock;
  window.GlassSurface = GlassSurfaceStock;
})();
