import{jsx as D}from"react/jsx-runtime";import{Renderer as G,Triangle as E,Color as b,Program as O,Mesh as B}from"ogl";import{useRef as _,useEffect as z}from"react";const I=`
attribute vec2 uv;
attribute vec2 position;

varying vec2 vUv;

void main() {
    vUv = uv;
    gl_Position = vec4(position, 0, 1);
}
`,U=a=>`
precision highp float;

uniform float uTime;
uniform float uSwirl;
uniform float uSpeed;
uniform float uScale;
uniform float uOffsetX;
uniform float uOffsetY;
uniform float uRotation;
uniform float uWaveAmp;
uniform float uWaveFreq;
uniform float uWaveSpeed;
uniform float uGrain;
uniform vec3 uColorA;
uniform vec3 uColorB;
uniform vec3 uColorC;
uniform vec3 uResolution;

varying vec2 vUv;

// 2D noise-like wave
float wave(vec2 uv, float freq, float speed, float time) {
    return sin(uv.x * freq + time * speed) * cos(uv.y * freq + time * speed);
}

// Simple random noise
float random(vec2 st) {
    return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123);
}

// Color Dodge blending
vec3 colorDodge(vec3 base, vec3 blend) {
    return min(base / (1.0 - blend + 0.0001), 1.0);
}

void main() {
    float mr = min(uResolution.x, uResolution.y);
    vec2 uv = (vUv.xy * 2.0 - 1.0) * uResolution.xy / mr;

    // Scale and offset
    uv = uv * uScale + vec2(uOffsetX, uOffsetY);

    // Rotation
    float cosR = cos(uRotation);
    float sinR = sin(uRotation);
    uv = vec2(
        uv.x * cosR - uv.y * sinR,
        uv.x * sinR + uv.y * cosR
    );

    // Water waves
    uv.x += wave(uv, uWaveFreq, uWaveSpeed, uTime) * uWaveAmp;
    uv.y += wave(uv + 10.0, uWaveFreq * 1.5, uWaveSpeed * 0.8, uTime) * uWaveAmp * 0.5;

    // Swirl
    float angle = atan(uv.y, uv.x);
    float radius = length(uv);
    angle += uSwirl * radius;
    uv = vec2(cos(angle), sin(angle)) * radius;

    // Distortion loop
    float d = -uTime * 0.5 * uSpeed;
    float a = 0.0;
    for (float i = 0.0; i < ${a.toFixed(1)}; ++i) {
        a += cos(i - d - a * uv.x);
        d += sin(uv.y * i + a);
    }
    d += uTime * 0.5 * uSpeed;

    // Base color mixing
    float mix1 = (sin(d) + 1.0) * 0.5;
    float mix2 = (cos(a) + 1.0) * 0.5;
    vec3 col = mix(uColorA, uColorB, mix1);
    col = mix(col, uColorC, mix2);

    // Grain using Color Dodge
    float grain = (random(gl_FragCoord.xy + uTime) - 0.5) * uGrain;
    vec3 grainCol = vec3(0.5 + grain); // grayish grain
    col = colorDodge(col, grainCol);

    gl_FragColor = vec4(col, 1.0);
}
`,X=a=>{const o=a.replace("#",""),n=parseInt(o.substring(0,2),16)/255,t=parseInt(o.substring(2,4),16)/255,i=parseInt(o.substring(4,6),16)/255;return[n,t,i]};function M(a){const o=_(null),{colors:n=["#3b2a8d","#aaa7d7","#f75092"],distortion:t=5,swirl:i=.5,speed:c=1,scale:f=1,offsetX:m=0,offsetY:d=0,rotation:g=90,waveAmp:p=.1,waveFreq:w=10,waveSpeed:x=.2,grain:h=.06,...y}=a;return z(()=>{if(!o.current)return;const r=o.current,u=new G,e=u.gl;e.clearColor(0,0,0,1);function s(){u.setSize(r.offsetWidth,r.offsetHeight)}window.addEventListener("resize",s,!1),s();const W=new E(e),A=n.slice(0,3).map(X),C={uTime:{value:0},uSwirl:{value:i},uSpeed:{value:c},uScale:{value:f},uOffsetX:{value:m},uOffsetY:{value:d},uRotation:{value:g},uWaveAmp:{value:p},uWaveFreq:{value:w},uWaveSpeed:{value:x},uResolution:{value:new b(e.canvas.width,e.canvas.height,e.canvas.width/e.canvas.height)},uGrain:{value:h}},F=["A","B","C"];A.forEach((v,T)=>{C[`uColor${F[T]}`]={value:new b(...v)}});const R=new O(e,{vertex:I,fragment:U(t),uniforms:C}),q=new B(e,{geometry:W,program:R});let l;function S(v){l=requestAnimationFrame(S),R.uniforms.uTime.value=v*.001,u.render({scene:q})}return l=requestAnimationFrame(S),r.appendChild(e.canvas),()=>{cancelAnimationFrame(l),window.removeEventListener("resize",s),r.removeChild(e.canvas),e.getExtension("WEBGL_lose_context")?.loseContext()}},[n,t,i,c,f,m,d,g,p,w,x,h]),D("div",{ref:o,style:{width:"100%",height:"100%",position:"absolute",overflow:"hidden"},...y})}export{M as G};
