import { a as p } from "../../index-DSGwv2Yx.js";
class m {
  variableNames = ["cache", "item"];
  outputShape;
  userCode;
  customUniforms = [{ name: "cacheT", type: "int" }];
  constructor(t, a, n, o, c) {
    const s = Math.min(n + 1, c);
    this.outputShape = [t, a, s, o], this.userCode = `
        void main() {
            ivec4 coords = getOutputCoords(); // [b, h, t, d]
            int b = coords.x;
            int h = coords.y;
            int t = coords.z;
            int d = coords.w;

            int itemT = 1;
            int maxSize = ${c};
            int totalT = cacheT + itemT;
            int start = totalT >= maxSize ? 1 : 0;

            int srcT = t + start;
            float val = 0.0;
            if (srcT < cacheT) {
                val = getCache(b, h, srcT, d);
            } else if (srcT == cacheT) {
                val = getItem(b, h, 0, d);
            } else {
                val = 0.0;}
            setOutput(val);
        }
        `;
  }
}
function d(e) {
  const { cache: t, item: a } = e.inputs, { maxSize: n, pastLen: o } = e.attrs, c = e.backend, s = t.shape[0], r = t.shape[2], i = t.shape[1], h = new m(s, i, r, a.shape[3], n);
  return c.runWebGLProgram(h, [t, a], "float32", [[o]]);
}
const l = {
  kernelName: "AppendCache",
  backendName: "webgl",
  kernelFunc: d
};
p(l);
