import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
export const HORSE_TYPES = ["minecraft:horse", "minecraft:donkey", "minecraft:mule", "minecraft:skeleton_horse", "minecraft:zombie_horse"];
export class HorseActor extends utils.Actor {
    set Age(age) {
        if (this.isValid())
            this.setDynamicProperty("age", age);
    }
    get Age() {
        var _a;
        if (!this.isValid())
            return 0;
        return (_a = this.getDynamicProperty("age")) !== null && _a !== void 0 ? _a : 0;
    }
    constructor(entity) {
        super(entity);
        HorseActor.HorseActorMap.set(entity.id, this);
        utils.waitForLoopEvent(20, () => {
            this.Age += 1;
            return !this.isValid();
        });
    }
}
HorseActor.HorseActorMap = new Map();
(() => {
    mc.world.afterEvents.playerInteractWithEntity.subscribe(({ player, target, itemStack }) => {
        if (HORSE_TYPES.includes(target.typeId)) {
            const actor = HorseActor.getActor(target.id, HorseActor);
            const ageable = actor === null || actor === void 0 ? void 0 : actor.getComponent("minecraft:ageable");
            if (actor && ageable) {
                const feedItems = ageable.getFeedItems();
                feedItems.forEach(feedItem => {
                    if (feedItem.item === (itemStack === null || itemStack === void 0 ? void 0 : itemStack.typeId)) {
                        actor.Age += ageable.duration * feedItem.growth;
                    }
                });
            }
        }
    });
})();
HORSE_TYPES.forEach(type => {
    utils.Actor.makeActors({ type }, HorseActor);
});
