# Copyright 2024 Josua Krause
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A moving average implementation."""
# from redipy.api import RedisClientAPI
# from redipy.backend.backend import ExecFunction
# from redipy.symbolic.seq import FnContext


# class RMovingAverage:
#     def __init__(self, rt: RedisClientAPI) -> None:
#         self._rt = rt
#         self._update = self._update_script()

#     def _update_script(self) -> ExecFunction:
#         ctx = FnContext()


#         rsize = RedisVar(ctx.add_key("size"))
#         base = ctx.add_local(ctx.add_key("frame"))
#         field = ctx.add_arg("field")
#         pos = ctx.add_local(ToNum(rsize.get_value(default=0)))
#         res = ctx.add_local(None)
#         cur = ctx.add_local(None)
#         rframe = RedisHash(cur)

#         loop = ctx.while_(res.eq_(None).and_(pos.ge_(0)))
#         loop.add(cur.assign(Strs(base, ":", ToIntStr(pos))))
#         loop.add(res.assign(rframe.hget(field)))
#         loop.add(pos.assign(pos - 1))

#         ctx.set_return_value(res)
#         return self._rt.register_script(ctx)
