import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
export class HopperComponent extends utils.ActorComponent {
    onComponentAdded() {
        utils.waitForLoopEvent(this.options.pullFrequencySeconds * 20, this.pull.bind(this), this.actor.id + this.id);
    }
    onComponentRemoved() {
        utils.abortEvents(this.actor.id + this.id);
    }
    pull() {
        if (!this.actor.isValid())
            return true;
        if (!this.options.pullCondition())
            return;
        const pulls = this.options.pullCallbacks();
        const containers = this.getAttachedHoppers();
        containers.forEach(container => {
            if (container) {
                for (let i = 0; i < container.size; i++) {
                    const item = container.getItem(i);
                    if (item && pulls.has(item.typeId)) {
                        const shouldPull = pulls.get(item.typeId)(item);
                        if (shouldPull) {
                            if (item.amount > 1) {
                                item.amount -= 1;
                                container.setItem(i, item);
                            }
                            else {
                                container.setItem(i);
                            }
                            return;
                        }
                    }
                }
            }
        });
    }
    getAttachedHoppers() {
        var _a, _b, _c, _d, _e;
        const hoppers = [];
        const originBlock = this.actor.dimension.getBlock(this.actor.location);
        if ((_a = originBlock === null || originBlock === void 0 ? void 0 : originBlock.above()) === null || _a === void 0 ? void 0 : _a.matches("minecraft:hopper", { facing_direction: 0, toggle_bit: 0 })) {
            hoppers.push(originBlock.above());
        }
        if ((_b = originBlock === null || originBlock === void 0 ? void 0 : originBlock.north()) === null || _b === void 0 ? void 0 : _b.matches("minecraft:hopper", { facing_direction: 3, toggle_bit: 0 })) {
            hoppers.push(originBlock.north());
        }
        if ((_c = originBlock === null || originBlock === void 0 ? void 0 : originBlock.east()) === null || _c === void 0 ? void 0 : _c.matches("minecraft:hopper", { facing_direction: 4, toggle_bit: 0 })) {
            hoppers.push(originBlock.east());
        }
        if ((_d = originBlock === null || originBlock === void 0 ? void 0 : originBlock.south()) === null || _d === void 0 ? void 0 : _d.matches("minecraft:hopper", { facing_direction: 2, toggle_bit: 0 })) {
            hoppers.push(originBlock.south());
        }
        if ((_e = originBlock === null || originBlock === void 0 ? void 0 : originBlock.west()) === null || _e === void 0 ? void 0 : _e.matches("minecraft:hopper", { facing_direction: 5, toggle_bit: 0 })) {
            hoppers.push(originBlock.west());
        }
        return hoppers.map(hopper => {
            const inventory = hopper.getComponent(mc.BlockInventoryComponent.componentId);
            if (inventory)
                return inventory.container;
        });
    }
}
