import { R as C, N as c, aj as R, v as f } from "./index-DSGwv2Yx.js";
import { u as g, g as I, a as x, b as F, c as $, d as u, e as m, i as l } from "./gpgpu_math-3bCb5ooU.js";
class v {
  constructor(t, i) {
    this.variableNames = ["A"], this.packedInputs = !0, this.packedOutput = !0, this.customUniforms = [{ name: "inputShape", type: "ivec3" }], this.outputShape = t, this.enableShapeUniforms = g(this.outputShape.length);
    let a = "";
    for (let e = 0; e < 4; e++) {
      let o = "thisRC = rc;";
      e % 2 === 1 && (o += "thisRC.z += 1;"), e > 1 && (o += "thisRC.y += 1;"), a += `
        ${o}
        ${e > 0 ? "if(thisRC.y < rows && thisRC.z < cols){" : ""}
          int flatIndex = getFlatIndex(thisRC);

          ivec3 inputRC = inputCoordsFromReshapedOutCoords(flatIndex);
          vec2 inputRCInnerDims = vec2(float(inputRC.y),float(inputRC.z));

          result[${e}] =
            getChannel(getA(inputRC.x, inputRC.y, inputRC.z), inputRCInnerDims);
        ${e > 0 ? "}" : ""}
      `;
    }
    this.userCode = `
      ${S(i, this.enableShapeUniforms)}
      ${this.enableShapeUniforms ? I() : x(t)}

      void main() {
        ivec3 rc = getOutputCoords();

        vec4 result = vec4(0.);

        ivec3 thisRC;
        int rows = ${this.enableShapeUniforms ? "outShape[1]" : t[1]};
        int cols = ${this.enableShapeUniforms ? "outShape[2]" : t[2]};

        ${a}

        setOutput(result);
      }
    `;
  }
}
function S(s, t) {
  return `
    ivec3 inputCoordsFromReshapedOutCoords(int index) {
      ${t ? F(["r", "c", "d"], "inputShape") : $(["r", "c", "d"], s)}
      return ivec3(r, c, d);
    }
  `;
}
function b(s, t, i) {
  const a = [
    u(s.shape),
    ...m(s.shape)
  ], e = {
    dtype: s.dtype,
    shape: a,
    dataId: s.dataId
  }, o = [
    u(t),
    ...m(t)
  ], r = new v(o, a), p = !0, n = [a], h = i.runWebGLProgram(r, [e], s.dtype, n, p);
  return { dataId: h.dataId, shape: t, dtype: h.dtype };
}
function y(s) {
  const { inputs: t, backend: i, attrs: a } = s, { x: e } = t, { shape: o } = a, r = i, p = c(e.shape), n = R(o, p), h = c(n);
  f(p === h, () => `The new shape (${n}) has ${h} elements and the old shape (${e.shape}) has ${p} elements. The new shape and old shape must have the same number of elements.`);
  const d = r.texData.get(e.dataId);
  return d.isPacked && !l(e.shape, n) && !(d.texture !== null && l(d.shape, n)) ? b(e, n, r) : (r.incRef(e.dataId), { dataId: e.dataId, shape: n, dtype: e.dtype });
}
const U = {
  kernelName: C,
  backendName: "webgl",
  kernelFunc: y
};
export {
  v as R,
  U as a,
  y as r
};
