// Structure Item Registry (modular version, July 2025)
export const structureItems = {
    "single-street-light": {
      "item": "dss_st:single_street_light",
      "display_name": "Single Street Light",
      "structure_templates": ["single_street_light"]
    },
    "double-street-light": {
      "item": "dss_st:double_street_light",
      "display_name": "Double Street Light",
      "structure_templates": ["double_street_light"]
    },
    "util-pole": {
      "item": "dss_st:util_pole",
      "display_name": "Utility Pole",
      "structure_templates": ["line_utility_pole", "junction_utility_pole"]
    },
    "util-pole-trans": {
      "item": "dss_st:util_pole_trans",
      "display_name": "Utility Pole w/ Transformer",
      "structure_templates": ["line_utility_pole_trans", "junction_utility_pole_trans"]
    }
  };
  
  export const structureTemplates = {
    // --- STREET LIGHTS ---
      "single_street_light": {
    "display_name": "Single Street Light",
    "placement_bounds": {
      "x": 4, "y": 13, "z": 4
    },
    "parts": [
        {
          "name": "base",
          "block": "dss_st:dss_st_street_light_base",
          "placement": { "type": "on_ground" }
        },
        {
          "name": "pole",
          "block": "dss_st:dss_st_street_light_pole",
          "repeat": { "direction": "up", "min": 5, "max": 10 },
          "placement": { "type": "on_previous", "target": "base" }
        },
        {
          "name": "top",
          "block": "dss_st:dss_st_street_light_top",
          "placement": { "type": "on_top", "target": "pole" }
        },
        {
          "name": "arm",
          "block": "dss_st:dss_st_street_light_arm",
          "repeat": { "direction": "right", "min": 1, "max": 2 },
          "placement": {
            "type": "attach_right",
            "target": "top",
            "facing": "left"
          }
        },
        {
          "name": "light",
          "block": "dss_st:dss_st_street_light_light",
          "placement": {
            "type": "attach_right",
            "target": "arm",
            "attach_to": "last",
            "facing": "left"
          }
        }
      ]
    },
      "double_street_light": {
    "display_name": "Double Street Light",
    "placement_bounds": {
      "x": 4, "y": 13, "z": 4
    },
    "parts": [
        {
          "name": "base",
          "block": "dss_st:dss_st_street_light_base",
          "placement": { "type": "on_ground" }
        },
        {
          "name": "pole",
          "block": "dss_st:dss_st_street_light_pole",
          "repeat": { "direction": "up", "min": 5, "max": 10 },
          "placement": { "type": "on_previous", "target": "base" }
        },
        {
          "name": "top",
          "block": "dss_st:dss_st_street_light_top",
          "placement": { "type": "on_top", "target": "pole" }
        },
        {
          "name": "arm",
          "block": "dss_st:dss_st_street_light_arm",
          "repeat": { "direction": "right", "min": 1, "max": 2 },
          "placement": {
            "type": "attach_right",
            "target": "top",
            "facing": "left"
          }
        },
        {
          "name": "arm2",
          "block": "dss_st:dss_st_street_light_arm",
          "repeat": { "direction": "left", "min": 1, "max": 2 },
          "placement": {
            "type": "attach_left",
            "target": "top",
            "facing": "right"
          }
        },
        {
          "name": "light",
          "block": "dss_st:dss_st_street_light_light",
          "placement": {
            "type": "attach_right",
            "target": "arm",
            "attach_to": "last",
            "facing": "left"
          }
        },
        {
          "name": "light2",
          "block": "dss_st:dss_st_street_light_light",
          "placement": {
            "type": "attach_left",
            "target": "arm2",
            "attach_to": "last",
            "facing": "right"
          }
        }
      ]
    },
  
    // --- UTILITY POLES ---
      "line_utility_pole": {
    "display_name": "Line Utility Pole",
    "placement_bounds": {
      "x": 2, "y": 13, "z": 2
    },
    "parts": [
        {
          "name": "base",
          "block": "dss_st:dss_st_util_pole_base",
          "placement": { "type": "on_ground" }
        },
        {
          "name": "pole",
          "block": "dss_st:dss_st_util_pole_pole",
          "repeat": { "direction": "up", "min": 5, "max": 10 },
          "placement": { "type": "on_previous", "target": "base" }
        },
        {
          "name": "top",
          "block": "dss_st:dss_st_util_pole_top_line",
          "placement": { "type": "on_top", "target": "pole" }
        },
        {
          "name": "arm",
          "block": "dss_st:dss_st_util_pole_arm_sin",
          "placement": {
            "type": "attach_right",
            "target": "top",
            "facing": "left"
          }
        },
        {
          "name": "arm2",
          "block": "dss_st:dss_st_util_pole_arm_sin",
          "placement": {
            "type": "attach_left",
            "target": "top",
            "facing": "right"
          }
        }
      ]
    },
      "junction_utility_pole": {
    "display_name": "Junction Utility Pole",
    "placement_bounds": {
      "x": 2, "y": 13, "z": 2
    },
    "parts": [
        {
          "name": "base",
          "block": "dss_st:dss_st_util_pole_base",
          "placement": { "type": "on_ground" }
        },
        {
          "name": "pole",
          "block": "dss_st:dss_st_util_pole_pole",
          "repeat": { "direction": "up", "min": 5, "max": 10 },
          "placement": { "type": "on_previous", "target": "base" }
        },
        {
          "name": "top",
          "block": "dss_st:dss_st_util_pole_top_junc",
          "placement": { "type": "on_top", "target": "pole" }
        },
        {
          "name": "arm",
          "block": "dss_st:util_pole_arm_junc_top",
          "placement": {
            "type": "attach_right",
            "target": "top",
            "facing": "left"
          }
        },
        {
          "name": "arm2",
          "block": "dss_st:util_pole_arm_junc_top",
          "placement": {
            "type": "attach_left",
            "target": "top",
            "facing": "right"
          }
        },
        {
          "name": "arm3",
          "block": "dss_st:util_pole_arm_junc_bot",
          "placement": {
            "type": "attach_back",
            "target": "top",
            "facing": "forward"
          }
        },
        {
          "name": "arm4",
          "block": "dss_st:util_pole_arm_junc_bot",
          "placement": {
            "type": "attach_front",
            "target": "top",
            "facing": "backward"
          }
        }
      ]
    },
  
    // --- UTILITY POLE WITH TRANSFORMER ---
      "line_utility_pole_trans": {
    "display_name": "Line Utility Pole with Transformer",
    "placement_bounds": {
      "x": 2, "y": 13, "z": 2
    },
    "parts": [
        {
          "name": "base",
          "block": "dss_st:dss_st_util_pole_base",
          "placement": { "type": "on_ground" }
        },
        {
          "name": "pole",
          "block": "dss_st:dss_st_util_pole_pole",
          "repeat": { "direction": "up", "min": 5, "max": 10 },
          "placement": { "type": "on_previous", "target": "base" }
        },
        {
          "name": "top",
          "block": "dss_st:dss_st_util_pole_top_line",
          "placement": { "type": "on_top", "target": "pole" }
        },
        {
          "name": "arm",
          "block": "dss_st:dss_st_util_pole_arm_sin",
          "placement": {
            "type": "attach_right",
            "target": "top",
            "facing": "left"
          }
        },
        {
          "name": "arm2",
          "block": "dss_st:dss_st_util_pole_arm_sin",
          "placement": {
            "type": "attach_left",
            "target": "top",
            "facing": "right"
          }
        },
        {
          "name": "transformer",
          "block": "dss_st:dss_st_transformer",
          "placement": {
            "type": "attach_left",
            "target": "pole",
            "at_level": "pole-2",
            "facing": "right"
          }
        },
        {
          "name": "transformer2",
          "block": "dss_st:dss_st_transformer",
          "placement": {
            "type": "attach_right",
            "target": "pole",
            "at_level": "pole-2",
            "facing": "left"
          }
        }
      ]
    },
      "junction_utility_pole_trans": {
    "display_name": "Junction Utility Pole with Transformer",
    "placement_bounds": {
      "x": 2, "y": 13, "z": 2
    },
    "parts": [
        {
          "name": "base",
          "block": "dss_st:dss_st_util_pole_base",
          "placement": { "type": "on_ground" }
        },
        {
          "name": "pole",
          "block": "dss_st:dss_st_util_pole_pole",
          "repeat": { "direction": "up", "min": 4, "max": 10 },
          "placement": { "type": "on_previous", "target": "base" }
        },
        {
          "name": "top",
          "block": "dss_st:dss_st_util_pole_top_junc",
          "placement": { "type": "on_top", "target": "pole" }
        },
        {
          "name": "arm",
          "block": "dss_st:util_pole_arm_junc_top",
          "placement": {
            "type": "attach_right",
            "target": "top",
            "facing": "left"
          }
        },
        {
          "name": "arm2",
          "block": "dss_st:util_pole_arm_junc_top",
          "placement": {
            "type": "attach_left",
            "target": "top",
            "facing": "right"
          }
        },
        {
          "name": "arm3",
          "block": "dss_st:util_pole_arm_junc_bot",
          "placement": {
            "type": "attach_back",
            "target": "top",
            "facing": "forward"
          }
        },
        {
          "name": "arm4",
          "block": "dss_st:util_pole_arm_junc_bot",
          "placement": {
            "type": "attach_front",
            "target": "top",
            "facing": "backward"
          }
        },
        {
          "name": "transformer",
          "block": "dss_st:dss_st_transformer",
          "placement": {
            "type": "attach_left",
            "target": "pole",
            "at_level": "pole-2",
            "facing": "right"
          }
        },
        {
          "name": "transformer2",
          "block": "dss_st:dss_st_transformer",
          "placement": {
            "type": "attach_right",
            "target": "pole",
            "at_level": "pole-2",
            "facing": "left"
          }
        }
      ]
    }
  };
  
// Utility functions for structure family management
/**
 * Extract all unique block types used across all structure templates
 * @returns {Set<string>} Set of all exclusive structure block IDs
 */
export function getExclusiveStructureBlocks() {
  const blockSet = new Set();
  
  for (const template of Object.values(structureTemplates)) {
    for (const part of template.parts) {
      blockSet.add(part.block);
    }
  }
  
  return blockSet;
}

/**
 * Get structure families organized by their block types and structure types
 * @returns {Object} Structure families configuration
 */
export function getStructureFamilies() {
  return {
    'street_light': {
      blocks: extractBlocksFromTemplates(['single_street_light', 'double_street_light']),
      types: ['single-street-light', 'double-street-light']
    },
    'utility_pole': {
      blocks: extractBlocksFromTemplates([
        'line_utility_pole', 
        'junction_utility_pole', 
        'line_utility_pole_trans', 
        'junction_utility_pole_trans'
      ]),
      types: ['util-pole', 'util-pole-trans']
    }
  };
}

/**
 * Extract all unique blocks from specific structure templates
 * @param {string[]} templateKeys - Array of template keys to extract blocks from
 * @returns {string[]} Array of unique block IDs
 */
function extractBlocksFromTemplates(templateKeys) {
  const blockSet = new Set();
  
  for (const templateKey of templateKeys) {
    const template = structureTemplates[templateKey];
    if (template) {
      for (const part of template.parts) {
        blockSet.add(part.block);
      }
    }
  }
  
  return Array.from(blockSet);
}

/**
 * Get the structure family for a specific block type
 * @param {string} blockType - The block type to check
 * @returns {string|null} The structure family or null if not found
 */
export function getStructureFamilyForBlock(blockType) {
  const families = getStructureFamilies();
  
  for (const [familyName, familyConfig] of Object.entries(families)) {
    if (familyConfig.blocks.includes(blockType)) {
      return familyName;
    }
  }
  
  return null;
}

/**
 * Check if a block type is an exclusive structure block
 * @param {string} blockType - The block type to check
 * @returns {boolean} True if the block is an exclusive structure block
 */
export function isExclusiveStructureBlock(blockType) {
  const exclusiveBlocks = getExclusiveStructureBlocks();
  return exclusiveBlocks.has(blockType);
}

/**
 * Validate that there's enough space for a structure placement (no other structures nearby)
 * @param {string} templateKey - The structure template key
 * @param {Vector3} location - The placement location 
 * @param {Dimension} dimension - The dimension to check in
 * @returns {Object} Validation result with valid flag and reason
 */
export function validateStructureSpacing(templateKey, location, dimension) {
  const template = structureTemplates[templateKey];
  
  if (!template) {
    return {
      valid: false,
      reason: "Unknown structure template"
    };
  }
  
  // Use template bounds or default fallback
  const bounds = template.placement_bounds;
  
  const conflictingBlocks = [];
  
  for (let x = -bounds.x; x <= bounds.x; x++) {
    for (let y = 0; y <= bounds.y; y++) {
      for (let z = -bounds.z; z <= bounds.z; z++) {
        const checkLocation = {
          x: location.x + x,
          y: location.y + y,
          z: location.z + z
        };
        
        const block = dimension.getBlock(checkLocation);
        if (block && isExclusiveStructureBlock(block.typeId)) {
          conflictingBlocks.push({
            location: checkLocation,
            blockType: block.typeId
          });
        }
      }
    }
  }
  
  if (conflictingBlocks.length > 0) {
    const nearestConflict = conflictingBlocks[0];
    return {
      valid: false,
      reason: `Another structure is too close. This structure needs at least ${bounds.x}x${bounds.y}x${bounds.z} blocks of space.`,
      details: `Found ${conflictingBlocks.length} conflicting structure block(s)`,
      conflictLocation: nearestConflict.location,
      conflictBlock: nearestConflict.blockType
    };
  }
  
  return { 
    valid: true,
    message: `Space clear for ${template.display_name} (${bounds.x}x${bounds.y}x${bounds.z})`
  };
}
  