import { world, ItemStack, system, BlockPermutation } from "@minecraft/server";
import { SettingsHandler } from "./settings";
// Store the original time when blocks are placed
const timeMap = new Map();
export function nightSpongePlace(block, player) {
    // Check if night sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("nightSponge")) {
        block.dimension.getBlock(block.location)?.setPermutation(BlockPermutation.resolve("minecraft:air"));
        player.sendMessage("§cNight Sponge is disabled in settings!");
        return;
    }
    const timeOfDay = world.getTimeOfDay();
    const blockPos = `${block.location.x},${block.location.y},${block.location.z}`;
    // Check if it's daytime (between 0 and 13000)
    if (timeOfDay >= 0 && timeOfDay < 13000) {
        // Store the original time and set to midnight
        timeMap.set(blockPos, timeOfDay);
        system.run(() => {
            world.setTimeOfDay(18000);
            // Spawn the time set night particle at block position
            block.dimension.spawnParticle("wildphire_spg:time_set_night_particle", { x: block.location.x + 0.5, y: block.location.y + 0.5, z: block.location.z + 0.5 });
        });
    }
    else {
        // If not daytime, break block, spawn item and show error
        block.dimension.getBlock(block.location)?.setPermutation(BlockPermutation.resolve("minecraft:air"));
        const nightSponge = new ItemStack("wildphire_spg:night_sponge", 1);
        block.dimension.spawnItem(nightSponge, block.location);
        world.sendMessage("§cNight Sponge can only be placed during daytime!§r");
    }
}
export function nightSpongeRemove(block) {
    const blockPos = `${block.location.x},${block.location.y},${block.location.z}`;
    const originalTime = timeMap.get(blockPos);
    if (originalTime !== undefined) {
        system.run(() => {
            world.setTimeOfDay(originalTime);
            // Spawn the time set day particle at block position
            block.dimension.spawnParticle("wildphire_spg:time_set_day_particle", { x: block.location.x + 0.5, y: block.location.y + 0.5, z: block.location.z + 0.5 });
        });
        timeMap.delete(blockPos);
    }
}
