import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
const UNBREAKABLE_BLOCKS = [
    "minecraft:bedrock",
    "minecraft:barrier",
    "minecraft:command_block",
    "minecraft:end_portal_frame",
    "minecraft:end_portal",
    "minecraft:end_gateway",
    "minecraft:jigsaw",
    "minecraft:portal",
    "minecraft:border",
    "minecraft:structure_block",
    "minecraft:structure_void",
    "minecraft:bubble_column",
    "minecraft:light_block",
    "minecraft:command_block",
    "minecraft:chain_command_block",
    "minecraft:repeating_command_block",
    "minecraft:lava",
    "minecraft:flowing_lava",
];
export class MineBlockComponent extends utils.ActorComponent {
    isBlockInDirection(direction, origin = this.actor.location, maxDistance = 1) {
        try {
            origin = new utils.Vector3({ x: origin.x, y: Math.ceil(origin.y), z: origin.z });
            const hit = this.actor.dimension.getBlockFromRay(origin, direction, { includeLiquidBlocks: false, maxDistance });
            if (hit) {
                return true;
            }
            else {
                return false;
            }
        }
        catch (error) {
            return false;
        }
    }
    breakBlockInDirection(direction, origin = this.actor.location, maxDistance = 1) {
        var _a, _b, _c, _d;
        try {
            origin = new utils.Vector3({ x: origin.x, y: Math.ceil(origin.y), z: origin.z });
            const hit = this.actor.dimension.getBlockFromRay(origin, direction, { includeLiquidBlocks: false, maxDistance });
            if (hit) {
                return this.breakBlock(hit.block);
            }
            else {
                (_b = (_a = this.options).onFailure) === null || _b === void 0 ? void 0 : _b.call(_a, "No block hit");
                return false;
            }
        }
        catch (error) {
            (_d = (_c = this.options).onFailure) === null || _d === void 0 ? void 0 : _d.call(_c, error);
            return false;
        }
    }
    breakBlock(block) {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        try {
            if (UNBREAKABLE_BLOCKS.includes(block.typeId)) {
                (_b = (_a = this.options).onUnbreakable) === null || _b === void 0 ? void 0 : _b.call(_a, block);
                return false;
            }
            else {
                const item = block.getItemStack(1, true);
                if (item)
                    (_d = (_c = this.options).onItemCollected) === null || _d === void 0 ? void 0 : _d.call(_c, item, block);
                const currentRule = mc.world.gameRules.doTileDrops;
                this.actor.runCommand(`gamerule dotiledrops ${Boolean(this.options.useTileDrops)}`);
                this.actor.runCommand(`setblock ${new utils.Vector3(block.location)} air [] destroy`);
                this.actor.runCommand(`gamerule dotiledrops ${currentRule}`);
                (_f = (_e = this.options).onSuccess) === null || _f === void 0 ? void 0 : _f.call(_e, block);
                return true;
            }
        }
        catch (error) {
            (_h = (_g = this.options).onFailure) === null || _h === void 0 ? void 0 : _h.call(_g, error);
            return false;
        }
    }
}
