import{Block as t,Direction as e,EntityTypes as o,ItemComponentTypes as n,ItemStack as a,Player as r,system as i}from"@minecraft/server";import{Entities as s,Items as m,Properties as l}from"./consts";import{calculateRotation as c,consumeHeldItem as p,convertTypeId as d,getBlockEntities as y,getEntityItemStack as f,getPlayerHeldItem as u,getSideOfBlock as S,setEntityItemStack as k}from"./utils";import{displayRegistry as b}from"./display/registry";import{WeaponStone as x}from"./display/weapon_stone";import{DisplayContext as h}from"./display/context";import{GlassCase as I}from"./display/glass_case";import{Easel as P}from"./display/easel";import{ArmorStand as _}from"./display/armor_stand";export function createEntity(t,e,o,a,r,m){let d=b.fromTypeId(m.typeId);if(d&&!d.supportsItem(o)){t.onScreenDisplay.setActionBar({translate:"display.unsupported_item"}),t.playSound("bc_ds:error");return}let f=m?.permutation.getState("minecraft:cardinal_direction")??"",{x:u,y:x,z:g}=(d?m:S(m,a)).location,$={x:u+.5,y:x,z:g+.5},E=c(t.getRotation().y,d,f);i.run(()=>{let i=p(t);if(i){let c=t.dimension.spawnEntity(e,$);c.setProperty(l.Context,d?.displayContext()??h.Free),c.setProperty(l.Layers,1),c.setProperty(l.Rotation,E),k(c,i);let S=o.getComponent(n.Enchantable),b=S?S.getEnchantments().length>0:"minecraft:enchanted_golden_apple"===o.typeId||"minecraft:enchanted_book"===o.typeId;if(b){if(d instanceof _){let w=t.dimension.spawnEntity(s.ArmorEnchantLocator,$);w.setProperty(l.Armor,d.displayContext()),w.setProperty(l.Rotation,E)}else c.setProperty(l.Enchanted,1)}if(d instanceof I)t.dimension.setBlockPermutation(m.location,m.permutation.withState("bc_ds:stand",!0));else if(d instanceof P&&1===m.permutation.getState("bc_ds:wall"))c.setProperty(l.Layers,2);else if(d&&d.maxItems()>1){let z=d.calculateBestIndex(f,a,{x:r.x-u,y:r.y-x,z:r.z-g}),A=y(t.dimension,m).filter(t=>t.id!==c.id).map(t=>t.getProperty(l.Layers)),B=Array.from({length:d.maxItems()},(t,e)=>e+1).filter(t=>!A.includes(t)).sort((t,e)=>Math.abs(t-z)-Math.abs(e-z));c.setProperty(l.Layers,B[0])}}})}export function removeEntities(t,e){let o=b.fromTypeId(e?.typeId),n=y(t.dimension,e);n.forEach(e=>{let n=f(e);if(!n){e.remove();return}popStack(t,o,e,n,-1)});let a=y(t.dimension,e,!0);a.forEach(t=>t.remove())}export function handleEntityInteraction(t,e,o=null){if(!e?.typeId.startsWith("bc_ds:item_"))return;let n=f(e);if(!n)return;let a=b.fromTypeId(o?.typeId),r=u(t);switch(r?.typeId){case d(e.typeId):if(r.isStackableWith(n)){if(n.amount>=(a?.maxStackSize()??n.maxAmount)){t.onScreenDisplay.setActionBar({translate:"display.max_stack_size"}),t.playSound("bc_ds:error");break}}else break;if(p(t)){let{layers:i}=addToStack(t,a,e,n);i>1&&a instanceof I&&t.dimension.setBlockPermutation(o.location,o.permutation.withState("bc_ds:stand",!1)),k(e,n),t.dimension.playSound("bc_ds:place",e.location)}break;case m.Rotate:if(!a||!a.lockedRotation()){rotateStack(t,e,a),t.dimension.playSound("bc_ds:rotate",e.location);return}t.onScreenDisplay.setActionBar({translate:"display.locked_rotation"}),t.playSound("bc_ds:error");break;default:t.dimension.playSound("bc_ds:take",e.location);let{newSize:s,layers:l}=popStack(t,a,e,n);s>0&&k(e,n),a instanceof I&&t.dimension.setBlockPermutation(o.location,o.permutation.withState("bc_ds:stand",l>0))}}function addToStack(t,e,o,n,a=1){let r=e?.maxStackSize()??n.maxAmount;n.amount=Math.min(n.amount+a,r);let i=Math.floor(n.amount/r*15)+1;return o.setProperty(l.Layers,i),{layers:i}}function rotateStack(t,e,o){t.dimension.getBlock(e.location);let n=(e.getProperty(l.Rotation)+(o instanceof x?4:1))%8;e.setProperty(l.Rotation,n)}function popStack(t,e,o,n,a=1){if(0===a)return;a<0&&(a=n.amount);let r=n.clone();r.amount=Math.min(a,n.amount),t.dimension.spawnItem(r,o.location);let i=e?.maxStackSize()??n.maxAmount;if(n.amount<=a||n.amount>i){let s=y(t.dimension,o,!0);return s.forEach(t=>t.remove()),o.remove(),{newSize:0,layers:0}}{n.amount-=a;let m=Math.floor(n.amount/i*15)+1;return o.setProperty(l.Layers,m),{newSize:n.amount,layers:m}}}