import { world, system, BlockComponentTypes, MolangVariableMap } from '@minecraft/server';
import { c as clearItem, b as bees, m as mc } from './utils-d4218765.js';

const beesWax = bees("bees_wax");
const conversionLookup = {
    [mc("copper_block")]: mc("waxed_copper"),
    [mc("exposed_copper")]: mc("waxed_exposed_copper"),
    [mc("weathered_copper")]: mc("waxed_weathered_copper"),
    [mc("oxidized_copper")]: mc("waxed_oxidized_copper"),
    [mc("copper_bulb")]: mc("waxed_copper_bulb"),
    [mc("exposed_copper_bulb")]: mc("waxed_exposed_copper_bulb"),
    [mc("weathered_copper_bulb")]: mc("waxed_weathered_copper_bulb"),
    [mc("oxidized_copper_bulb")]: mc("waxed_oxidized_copper_bulb"),
    [mc("copper_grate")]: mc("waxed_copper_grate"),
    [mc("exposed_copper_grate")]: mc("waxed_exposed_copper_grate"),
    [mc("weathered_copper_grate")]: mc("waxed_weathered_copper_grate"),
    [mc("oxidized_copper_grate")]: mc("waxed_oxidized_copper_grate"),
    [mc("chiseled_copper")]: mc("waxed_chiseled_copper"),
    [mc("exposed_chiseled_copper")]: mc("waxed_exposed_chiseled_copper"),
    [mc("weathered_chiseled_copper")]: mc("waxed_weathered_chiseled_copper"),
    [mc("oxidized_chiseled_copper")]: mc("waxed_oxidized_chiseled_copper"),
    [mc("cut_copper")]: mc("waxed_cut_copper"),
    [mc("exposed_cut_copper")]: mc("waxed_exposed_cut_copper"),
    [mc("weathered_cut_copper")]: mc("waxed_weathered_cut_copper"),
    [mc("oxidized_cut_copper")]: mc("waxed_oxidized_cut_copper"),
    [mc("cut_copper_slab")]: mc("waxed_cut_copper_slab"),
    [mc("exposed_cut_copper_slab")]: mc("waxed_exposed_cut_copper_slab"),
    [mc("weathered_cut_copper_slab")]: mc("waxed_weathered_cut_copper_slab"),
    [mc("oxidized_cut_copper_slab")]: mc("waxed_oxidized_cut_copper_slab"),
    [mc("cut_copper_stairs")]: mc("waxed_cut_copper_stairs"),
    [mc("exposed_cut_copper_stairs")]: mc("waxed_exposed_cut_copper_stairs"),
    [mc("weathered_cut_copper_stairs")]: mc("waxed_weathered_cut_copper_stairs"),
    [mc("oxidized_cut_copper_stairs")]: mc("waxed_oxidized_cut_copper_stairs"),
    [mc("copper_trapdoor")]: mc("waxed_copper_trapdoor"),
    [mc("exposed_copper_trapdoor")]: mc("waxed_exposed_copper_trapdoor"),
    [mc("weathered_copper_trapdoor")]: mc("waxed_weathered_copper_trapdoor"),
    [mc("oxidized_copper_trapdoor")]: mc("waxed_oxidized_copper_trapdoor"),
    [mc("copper_door")]: mc("waxed_copper_door"),
    [mc("exposed_copper_door")]: mc("waxed_exposed_copper_door"),
    [mc("weathered_copper_door")]: mc("waxed_weathered_copper_door"),
    [mc("oxidized_copper_door")]: mc("waxed_oxidized_copper_door"),
};
const validDoors = Object.keys(conversionLookup).filter((e) => e.includes("_door") || e.includes("_trapdoor"));
world.beforeEvents.itemUseOn.subscribe((event) => {
    if (!checkForCorrectItem(event.itemStack)) {
        return;
    }
    if (event.block.typeId.includes("sign")) {
        system.run(() => {
            eventProcessor(event, event.block);
        });
        event.cancel = true;
    }
});
world.afterEvents.itemUseOn.subscribe((event) => {
    if (!checkForCorrectItem(event.itemStack)) {
        return;
    }
    eventProcessor(event, event.block);
});
world.afterEvents.itemUse.subscribe((event) => {
    if (!checkForCorrectItem(event.itemStack)) {
        return;
    }
    const block = event.source.getBlockFromViewDirection({
        maxDistance: 7,
    });
    if (!block) {
        return;
    }
    eventProcessor(event, block.block);
});
function checkForCorrectItem(itemStack) {
    if (!itemStack || itemStack.typeId !== beesWax) {
        return false;
    }
    return true;
}
function eventProcessor(event, block) {
    const { source, itemStack } = event;
    if (!block) {
        return;
    }
    const conversion = conversionLookup[block.typeId];
    if (!conversion) {
        const sign = block.getComponent(BlockComponentTypes.Sign);
        if (!sign) {
            return;
        }
        if (sign.isWaxed === false) {
            event.source.playSound("copper.wax.on");
            spawnParticle(source.dimension, block.location);
            sign.setWaxed(true);
            clearItem(event.source, itemStack.typeId);
        }
        return;
    }
    let useBlock = block;
    if (block.permutation.getState("upper_block_bit")) {
        useBlock = block.below();
    }
    convertToWaxedPermutation(source, useBlock);
    if (validDoors.includes(block.typeId)) {
        system.runTimeout(() => {
            let queryBlock = source.dimension.getBlock(block.location);
            if (block.permutation.getState("upper_block_bit")) {
                queryBlock = queryBlock.below();
            }
            queryBlock.setPermutation(queryBlock.permutation.withState("open_bit", false));
        }, 1);
    }
}
function convertToWaxedPermutation(source, block) {
    const location = block.location;
    const perm = block.permutation;
    const conversion = conversionLookup[block.typeId];
    let stateReplacement = stateStringBuilder(perm);
    source.dimension.runCommandAsync(`fill ${location.x} ${location.y} ${location.z} ${location.x} ${location.y} ${location.z} ${conversion} ${stateReplacement} replace ${block.typeId} ${stateReplacement}`);
    spawnParticle(source.dimension, location);
    source.playSound("copper.wax.on", {
        location: location,
    });
    source.runCommand("clear @p ftb_bees:bees_wax 0 1");
}
function centeredLocation(location, x, y, z) {
    return {
        x: location.x + x,
        y: location.y + y + 0.25,
        z: location.z + z,
    };
}
function spawnParticle(dimension, location) {
    let molang = new MolangVariableMap();
    molang.setColorRGB("variable.color", {
        red: 0.91,
        green: 0.55,
        blue: 0.08,
    });
    molang.setVector3("variable.direction", {
        x: 0,
        y: 0,
        z: 0,
    });
    for (let i = 0; i < 10; i++) {
        dimension.spawnParticle("minecraft:wax_particle", centeredLocation(location, Math.random(), Math.random(), Math.random()), molang);
    }
}
function stateStringBuilder(perm) {
    let output = "[";
    const states = perm.getAllStates();
    const keys = Object.keys(states);
    for (let i = 0; i < keys.length; i++) {
        const stateKey = keys[i];
        const value = states[stateKey];
        let valueRaw;
        if (typeof value === "string") {
            valueRaw = `"${value}"`;
        }
        else {
            valueRaw = value;
        }
        const ending = i !== keys.length - 1 ? ", " : "";
        output += `"${stateKey}" = ${valueRaw}${ending}`;
    }
    return output + "]";
}
