import { system, world } from "@minecraft/server";
import { CustomEntityBucketItems as CustomEntitySpawnEggs } from "../item/CustomEntityBucketItems.js";
export class AbstractSpawnEggEntity {
  constructor(identifier, spawnEggVariations) {
    this.identifier = identifier;
    this.spawnEggVariations = spawnEggVariations;
    this.onUseSpawnEgg();
  }
  onUseSpawnEgg() {
    world.afterEvents.itemUseOn.subscribe((e) => {
      system.run(() => {
        const player = e.source;
        const clickedBlock = e.block;
        const clickedFace = e.blockFace;
        const spawnAt = {
          x: clickedBlock.x + 0.5,
          y: clickedBlock.y + (clickedFace === "Up" ? 1 : 0),
          z: clickedBlock.z + 0.5,
        };
        const item = e.itemStack;
        let entitySpawned;
        if (
          !item.typeId.startsWith("sf_nba") ||
          !this.spawnEggVariations.names().find((egg) => egg === item.typeId)
        )
          return;
        if (this.spawnEggHasVariations()) {
          const variation = this.spawnEggVariations.getVariation(item.typeId);
          entitySpawned = player.dimension.spawnEntity(
            `${this.identifier}<minecraft:entity_spawned>`,
            spawnAt
          );
          entitySpawned.triggerEvent(`sf_nba:set_variant_${variation}`);
        } else {
          entitySpawned = player.dimension.spawnEntity(
            `${this.identifier}<minecraft:entity_spawned>`,
            spawnAt
          );
        }
      });
    });
  }
  spawnEggHasVariations() {
    return typeof this.spawnEggVariations === "function";
  }
}