"""FikoYT LTX Interpolate — frame multiplier (2x / 4x / 8x) linear blend.

Basit linear interpolation. Daha gelişmiş RIFE/FILM modeli için harici node kullan.
"""
try:
    import torch
except ImportError:
    torch = None


def _interp_pair(a, b, n: int):
    """a, b: [H, W, C] → n yeni frame [n, H, W, C] (a → b arası, a/b dahil değil)."""
    frames = []
    for i in range(1, n + 1):
        t = i / (n + 1)
        f = a * (1.0 - t) + b * t
        frames.append(f.unsqueeze(0))
    return torch.cat(frames, dim=0)


class FikoYTLTXInterpolate:
    """IMAGE batch (N frames) → IMAGE batch (N * multiplier - (multiplier-1))."""

    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "frames": ("IMAGE",),
                "multiplier": ("INT", {"default": 2, "min": 2, "max": 8, "step": 1}),
            }
        }

    RETURN_TYPES = ("IMAGE",)
    RETURN_NAMES = ("frames",)
    FUNCTION = "interpolate"
    CATEGORY = "FikoYT/video"

    def interpolate(self, frames, multiplier):
        if frames is None or frames.shape[0] < 2 or multiplier < 2:
            return (frames,)
        n_insert = int(multiplier) - 1
        out = [frames[0:1]]
        for i in range(frames.shape[0] - 1):
            a = frames[i]
            b = frames[i + 1]
            mid = _interp_pair(a, b, n_insert)
            out.append(mid)
            out.append(frames[i + 1:i + 2])
        return (torch.cat(out, dim=0),)


NODE_CLASS_MAPPINGS = {"FikoYTLTXInterpolate": FikoYTLTXInterpolate}
NODE_DISPLAY_NAME_MAPPINGS = {"FikoYTLTXInterpolate": "FikoYT • LTX Interpolate"}
