import { EquipmentSlot, ItemStack } from "@minecraft/server";
import { system, world, Entity, Player } from "@minecraft/server";
export const Overworld = world.getDimension('overworld');
export const afterEvents = world.afterEvents;
export const beforeEvents = world.beforeEvents;
export const LigthLevel = {
    "minecraft:torch": 12,
    "minecraft:end_rod": 13,
    "minecraft:shroomlight": 11,
    "minecraft:soul_torch": 13,
    "minecraft:glowstone": 12,
    "minecraft:lantern": 11,
    "minecraft:soul_lantern": 12,
    "minecraft:redstone_block": 4,
    "minecraft:redstone_torch": 6,
    "minecraft:glowstone_dust": 7,
    "minecraft:lit_pumpkin": 10,
    "minecraft:sea_lantern": 12,
    "minecraft:pearlescent_froglight": 10,
    "minecraft:verdant_froglight": 10,
    "minecraft:ochre_froglight": 10,
    "minecraft:crying_obsidian": 2,
    "minecraft:campfire": 12,
    "minecraft:soul_campfire": 13,
    "minecraft:blaze_rod": 6,
    "minecraft:enchanting_table": 9,
    "minecraft:blaze_powder": 6,
    "minecraft:ender_chest": 9,
    "minecraft:glow_ink_sac": 6,
    "minecraft:glow_berries": 6,
    "minecraft:blaze": 6,
    "minecraft:glow_squid": 6,
    "minecraft:magma_cube": 3
};
export function sleep(ticks = 1) {
    return new Promise((resolve) => system.runTimeout(resolve, ticks));
}
export function repeat(times, callback) {
    for (let i = 0; i < times; i++) {
        callback();
    }
}
export function parseID(id) {
    return capitalizeEachWord(id.split(':').pop().replace(/_/g, ' '));
}
export function capitalizeEachWord(str) {
    let words = str.split(" ");
    for (let i = 0; i < words.length; i++) {
        words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
    }
    return words.join(" ");
}
export class Vector {
    x;
    y;
    z;
    static up = { x: 0, y: 1, z: 0 };
    static down = { x: 0, y: -1, z: 0 };
    static forward = { x: 0, y: 0, z: 1 };
    static back = { x: 0, y: 0, z: -1 };
    static left = { x: -1, y: 0, z: 0 };
    static right = { x: 1, y: 0, z: 0 };
    static zero = { x: 0, y: 0, z: 0 };
    constructor(x, y, z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    static floor(vector) {
        return {
            x: Math.floor(vector.x),
            y: Math.floor(vector.y),
            z: Math.floor(vector.z)
        };
    }
    static equals(vectorA, vectorB) {
        return JSON.stringify(Vector.floor(vectorA)) === JSON.stringify(Vector.floor(vectorB));
    }
    static length(vector) {
        return Math.sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
    }
    static multiply(vectorA, value) {
        return {
            x: vectorA.x * (typeof value === 'number' ? value : value.x ?? 1),
            y: vectorA.y * (typeof value === 'number' ? value : value.y ?? 1),
            z: vectorA.z * (typeof value === 'number' ? value : value.z ?? 1)
        };
    }
    static add(vectorA, vectorB) {
        return {
            x: vectorA.x + (vectorB.x ?? 0),
            y: vectorA.y + (vectorB.y ?? 0),
            z: vectorA.z + (vectorB.z ?? 0)
        };
    }
    static subtract(vectorA, vectorB) {
        return {
            x: vectorA.x - (vectorB.x ?? 0),
            y: vectorA.y - (vectorB.y ?? 0),
            z: vectorA.z - (vectorB.z ?? 0)
        };
    }
    static distance(vectorA, vectorB) {
        return Math.sqrt(Math.pow(vectorA.x - vectorB.x, 2)
            + Math.pow(vectorA.y - vectorB.y, 2)
            + Math.pow(vectorA.z - vectorB.z, 2));
    }
    static normalize(vector) {
        const len = Vector.length(vector);
        return new Vector(vector.x / len, vector.y / len, vector.z / len);
    }
}
// @ts-ignore
Entity.prototype.isPlayer = function () {
    return this.typeId === "minecraft:player";
};
Object.defineProperties(Entity.prototype, {
    health: {
        get: function () {
            return this.getComponent('health');
        }
    },
    inventory: {
        get: function () {
            const inventory = this.getComponent('inventory');
            if (inventory)
                return inventory;
            throw new Error('MC error: Please do /reload');
        }
    },
    equippable: {
        get: function () {
            const equippable = this.getComponent('equippable');
            if (equippable)
                return equippable;
            throw new Error('MC error: Please do /reload');
        }
    },
});
Player.prototype.getMainhand = function () {
    return this.equippable.getEquipment(EquipmentSlot.Mainhand);
};
Object.defineProperties(ItemStack.prototype, {
    durability: {
        get: function () {
            return this.getComponent('durability');
        }
    },
    enchantable: {
        get: function () {
            return this.getComponent('enchantable');
        }
    },
});
