import { world, ItemStack } from '@minecraft/server';
import { holiDates1, getDateRangeInfo } from './holiday.js';

world.afterEvents.entityDie.subscribe((event) => {
  const deadEntity = event.deadEntity;
  //
  const damageSource = event.damageSource;
  const cause = damageSource.cause;
  const damagingEntity = damageSource.damagingEntity;
  const damagingProjectile = damageSource.damagingProjectile;
  
  const family = deadEntity.getComponent('type_family');

  if (
    deadEntity.typeId === 'minecraft:player' ||
    deadEntity.typeId === 'minecraft:item' ||
    family.hasTypeFamily('inanimate')
  ) {
    return;
  }

  const deadEntityDimension = deadEntity.dimension;
  const deadEntityLocation = deadEntity.location;
  const deadEntityTypeId = deadEntity.typeId;

  const now = new Date();
  const easter1 = getDateRangeInfo(now, holiDates1.easter1, { weeks: 1 });
  const summer1 = getDateRangeInfo(now, holiDates1.summer1, { weeks: 1 });
  const halloween1 = getDateRangeInfo(now, holiDates1.halloween1, { weeks: 1 });
  const festive1 = getDateRangeInfo(now, holiDates1.festive1, { weeks: 1 });
  const today1 = getDateRangeInfo(now, now, { days: 1 });

  if (Math.random() <= 0.05 ) {
    if (today1.isInRange) {
      deadEntityDimension.spawnItem(new ItemStack('just:ducke_bag', 1), deadEntityLocation);
    } else if (easter1.isInRange) {
      deadEntityDimension.spawnItem(new ItemStack('just:ducke_easter', 1), deadEntityLocation);
    } else if (summer1.isInRange) {
      deadEntityDimension.spawnItem(new ItemStack('just:ducke_july', 1), deadEntityLocation);
    } else if (halloween1.isInRange) {
      deadEntityDimension.spawnItem(new ItemStack('just:ducke_halloween', 1), deadEntityLocation);
    } else if (festive1.isInRange) {
      deadEntityDimension.spawnItem(new ItemStack('just:ducke_christmas', 1), deadEntityLocation);
    }
  }
})

world.afterEvents.entityDie.subscribe(({ deadEntityTypeId }) => {
  const item = new ItemStack("just:rubber_ducke", 1);
player.dimension.spawnItem(item, player.location);
}, 
{ entityTypes: ["just:rubber_ducke"] })
