import { Component } from "../core/world";
import { component } from "../utils/decorators";

@component
export class Sprite extends Component<{
    frames: string[],
    currentFrame: number,
    animationDelay: number,
    animationCounter: number,
}> {
    constructor(frame: string);
    constructor(frames: string[], animationDelay?: number);
    constructor(frames: string | string[], animationDelay: number = Infinity) {
        super({
            frames: Array.isArray(frames) ? frames : [frames],
            animationDelay,
            animationCounter: 0,
            currentFrame: 0,
        });
    }

    override async onAdd() {
        if (Number.isFinite(this.state.animationDelay)) {
            const { SpriteSystem } = await import('../systems/render/sprite'); // dynamic import to break cyclic dependency
            if (!this.world.hasSystem(SpriteSystem)) {
                this.world.addSystem(new SpriteSystem());
            }
        }
    }

    get image(): string {
        return this.state.frames[this.state.currentFrame];
    }
}

@component export class Hidden extends Component<{}> {
    constructor() {
        super({});
    }
}
