// Main entry point for the Structure Building System
// Handles itemUseOn events and coordinates structure placement

import { world } from '@minecraft/server';
import { registerStructureEvents } from './events.js';
import { structureItems } from './config.js';
// import { showStructureUI } from './ui.js';
import { debug } from './utils.js';

/**
 * Initialize the Structure Building System
 */
export function initializeStructureSystem() {
    // Register itemUseOn event handlers for all structure items
    registerStructureEvents();
    
    // Log registered items
    const itemCount = Object.keys(structureItems).length;
    debug('info', `Structure system initialized with ${itemCount} items`);
    
    return true;
}

/**
 * Get structure item configuration by item ID
 * @param {string} itemId - The Minecraft item identifier
 * @returns {Object|null} Structure item config or null if not found
 */
export function getStructureItemByItemId(itemId) {
  for (const [key, config] of Object.entries(structureItems)) {
    if (config.item === itemId) {
      return { key, ...config };
    }
  }
  return null;
}

/**
 * Check if an item is a structure item
 * @param {string} itemId - The Minecraft item identifier
 * @returns {boolean} True if item is a structure item
 */
export function isStructureItem(itemId) {
  return getStructureItemByItemId(itemId) !== null;
}

// Auto-initialize when module loads
world.afterEvents.worldInitialize.subscribe(() => {
  initializeStructureSystem();
});