
const TIERS = {
  coal:     { count: 26, size: 17, minY: -63, maxY: 65,  shape: 'uniform'  },
  iron:     { count: 26, size: 9,  minY: -63, maxY: 65,  shape: 'uniform'  },
  copper:   { count: 21, size: 20, minY: -63, maxY: 40,  shape: 'uniform'  },
  gold:     { count: 5,  size: 9,  minY: -63, maxY: 1,   shape: 'uniform'  },
  lapis:    { count: 5,  size: 7,  minY: -63, maxY: 1,   shape: 'triangle' },
  redstone: { count: 10, size: 8,  minY: -63, maxY: 1,   shape: 'uniform'  },
  diamond:  { count: 9,  size: 8,  minY: -63, maxY: 14, shape: 'triangle' },
  emerald:  { count: 4,  size: 3,  minY: -63, maxY: 30,  shape: 'uniform'  },
}

function addMiningOre(event, anchors, stoneBlock, deepslateBlock, tier) {
  const t = TIERS[tier]
  if (!t) {
    console.error(`[mining_dim] Unknown tier: "${tier}"`)
    return
  }

  const id = stoneBlock.replace(':', '_') + '_jamd'

  event.addOre(ore => {
    ore.id = `kubejs:${id}`
    ore.biomes = { id: 'jamd:mining' }

    ore.addTarget('#minecraft:stone_ore_replaceables', stoneBlock)
    if (deepslateBlock) {
      ore.addTarget('minecraft:deepslate', deepslateBlock)
    }

    ore.size = t.size
    ore.count([t.count])
    ore.squared()

    if (t.shape === 'triangle') {
      ore.triangleHeight(anchors.absolute(t.minY), anchors.absolute(t.maxY))
    } else {
      ore.uniformHeight(anchors.absolute(t.minY), anchors.absolute(t.maxY))
    }

    ore.noSurface = 0.0
    ore.worldgenLayer = 'underground_ores'
  })
}

WorldgenEvents.remove(event => {
  event.removeOres(props => {
    props.worldgenLayer = 'underground_ores'
    props.biomes = { id: 'jamd:mining' }
    props.blocks = [
      'minecraft:coal_ore',        'minecraft:deepslate_coal_ore',
      'minecraft:iron_ore',        'minecraft:deepslate_iron_ore',
      'minecraft:copper_ore',      'minecraft:deepslate_copper_ore',
      'minecraft:gold_ore',        'minecraft:deepslate_gold_ore',
      'minecraft:lapis_ore',       'minecraft:deepslate_lapis_ore',
      'minecraft:redstone_ore',    'minecraft:deepslate_redstone_ore',
      'minecraft:diamond_ore',     'minecraft:deepslate_diamond_ore',
      'minecraft:emerald_ore',     'minecraft:deepslate_emerald_ore',
    ]
  })
})

WorldgenEvents.add(event => {
  const { anchors } = event

  addMiningOre(event, anchors, 'minecraft:coal_ore',      'minecraft:deepslate_coal_ore',      'coal')
  addMiningOre(event, anchors, 'minecraft:iron_ore',      'minecraft:deepslate_iron_ore',      'iron')
  addMiningOre(event, anchors, 'minecraft:copper_ore',    'minecraft:deepslate_copper_ore',    'copper')
  addMiningOre(event, anchors, 'minecraft:gold_ore',      'minecraft:deepslate_gold_ore',      'gold')
  addMiningOre(event, anchors, 'minecraft:lapis_ore',     'minecraft:deepslate_lapis_ore',     'lapis')
  addMiningOre(event, anchors, 'minecraft:redstone_ore',  'minecraft:deepslate_redstone_ore',  'redstone')
  addMiningOre(event, anchors, 'minecraft:diamond_ore',   'minecraft:deepslate_diamond_ore',   'diamond')
  addMiningOre(event, anchors, 'minecraft:emerald_ore',   'minecraft:deepslate_emerald_ore',   'emerald')

  addMiningOre(event, anchors, 'create:zinc_ore',         'create:deepslate_zinc_ore',         'gold')

  addMiningOre(event, anchors, 'immersiveengineering:ore_aluminum', 'immersiveengineering:deepslate_ore_aluminum', 'iron')

  addMiningOre(event, anchors, 'mekanism:osmium_ore',     'mekanism:deepslate_osmium_ore',     'iron')
  addMiningOre(event, anchors, 'mekanism:uranium_ore',    'mekanism:deepslate_uranium_ore',    'gold')
  addMiningOre(event, anchors, 'mekanism:fluorite_ore',   'mekanism:deepslate_fluorite_ore',   'gold')

  addMiningOre(event, anchors, 'thermal:tin_ore',         'thermal:deepslate_tin_ore',         'iron')
  addMiningOre(event, anchors, 'thermal:lead_ore',        'thermal:deepslate_lead_ore',        'iron')
  addMiningOre(event, anchors, 'thermal:silver_ore',      'thermal:deepslate_silver_ore',      'gold')
  addMiningOre(event, anchors, 'thermal:nickel_ore',      'thermal:deepslate_nickel_ore',      'gold')
  addMiningOre(event, anchors, 'thermal:apatite_ore',     'thermal:deepslate_apatite_ore',     'iron')
  addMiningOre(event, anchors, 'thermal:cinnabar_ore',    'thermal:deepslate_cinnabar_ore',    'gold')
  addMiningOre(event, anchors, 'thermal:niter_ore',       'thermal:deepslate_niter_ore',       'iron')
  addMiningOre(event, anchors, 'thermal:sulfur_ore',      'thermal:deepslate_sulfur_ore',      'iron')

  addMiningOre(event, anchors, 'mysticalagriculture:prosperity_ore', null, 'gold')

  addMiningOre(event, anchors, 'evilcraft:dark_ore', 'evilcraft:dark_ore_deepslate', 'diamond')

  addMiningOre(event, anchors, 'kubejs:arcane_ore', 'kubejs:deepslate_arcane_ore', 'diamond')

})