import { world } from '@minecraft/server';
import ScriptingModule from '../scriptingModule';
import Totem from './totem';
import { findNeighboringBlocks } from './utils';
import TotemInteractComponent from './blockComponents/totemInteractComponent';
import TotemTransformationComponent from './blockComponents/totemTransformationComponent';
export default class SummonTotemModule extends ScriptingModule {
    constructor() {
        super('SummonTotemModule');
    }
    onInitialize() {
        world.beforeEvents.worldInitialize.subscribe((e) => {
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:totem.interact', new TotemInteractComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:totem.transformation', new TotemTransformationComponent());
        });
        world.afterEvents.playerPlaceBlock.subscribe(this.onPlayerPlaceBlock);
    }
    onPlayerPlaceBlock(e) {
        const { permutation } = e.block;
        let totem;
        if (permutation.matches('minecraft:bone_block')) {
            for (const lodestone of findNeighboringBlocks(e.block, 'minecraft:lodestone')) {
                totem = Totem.fromLodestone(lodestone);
                if (totem)
                    break;
            }
        }
        else if (permutation.matches('minecraft:lodestone')) {
            totem = Totem.fromLodestone(e.block);
        }
        else {
            return;
        }
        if (totem?.isBuiltByPlayer()) {
            totem.setBuildCompleted();
        }
    }
}
