import { BlockInventoryComponent, ItemStack } from "@minecraft/server";
import Item_Manager from "../../Item/Item_Manager";
export default class Block_Inventory_Manager {
    static set(block, inventory) {
        const container = this.container(block);
        if (container == undefined)
            return undefined;
        this.clear(block);
        for (let i = 0; i < inventory.length; i++) {
            this.set_Item(block, i, inventory[i]);
        }
    }
    static set_Item(block, slot, item) {
        const container = this.container(block);
        if (container == undefined)
            return undefined;
        if (item == undefined) {
            container.setItem(slot);
        }
        else if (item instanceof ItemStack) {
            container.setItem(slot, item);
        }
        else {
            container.setItem(slot, Item_Manager.new(item));
        }
    }
    static get(block) {
        const container = this.container(block);
        if (container == undefined)
            return undefined;
        const inventory = new Array();
        for (let i = 0; i < container.size; i++) {
            inventory.push(container.getItem(i));
        }
        return;
    }
    static get_Item(block, slot) {
        const container = this.container(block);
        if (container == undefined)
            return undefined;
        return container.getItem(slot);
    }
    static add_Item(block, item) {
        const container = this.container(block);
        if (container == undefined)
            return undefined;
        if (item instanceof ItemStack) {
            container.addItem(item);
        }
        else {
            container.addItem(Item_Manager.new(item));
        }
    }
    static clear(block) {
        this.container(block).clearAll();
    }
    static clear_Item(block, item_ID) {
        const inventory = this.get(block);
        for (let i = 0; i < inventory.length; ++i) {
            if (inventory[i] == undefined)
                continue;
            if (inventory[i].typeId != item_ID)
                continue;
            this.set_Item(block, i);
        }
    }
    static reduce(block, item_ID, amount) {
        const inventory = this.get(block);
        if (this.count_Item(block, item_ID) < amount)
            return false;
        let count = amount;
        for (let i = 0; i < inventory.length; ++i) {
            if (inventory[i] == undefined)
                continue;
            if (inventory[i].typeId != item_ID)
                continue;
            const item = inventory[i];
            if (item.amount > count) {
                item.amount = item.amount - count;
                this.set_Item(block, i, item);
                break;
            }
            else {
                count = count - item.amount;
                this.set_Item(block, i);
            }
        }
        return true;
    }
    static count_Item(block, item_ID) {
        const inventory = this.get(block);
        let amount = 0;
        for (let i = 0; i < inventory.length; ++i) {
            if (inventory[i] == undefined)
                continue;
            if (inventory[i].typeId != item_ID)
                continue;
            amount = amount + inventory[i].amount;
        }
        return amount;
    }
    static container(block) {
        const component = block.getComponent(BlockInventoryComponent.componentId);
        if (component == undefined)
            return undefined;
        return component.container;
    }
}
