import{ALL_DRAWER_TYPES as t,getType as e}from"../../main.js";import{getOpsiteDirection as o,camelCaseNamespacedID as s}from"../../utils.js";import{getPlayerHeldItem as r}from"../../utils/players.js";import{ActionFormData as n}from"@minecraft/server-ui";import{SORTS as a}from"../../display_unit/sort.js";class i{onPlayerInteract(t){"ftb_sd:drawer_viewer"!==t.block?.typeId||function(t,o){const i=r(o),c=i?.typeId;if(c&&(c.endsWith("_key")||c.startsWith("ftb_sd:grouped_key")))return void(new n).title({translate:"ftb_sd.drawer_viewer.display_settings"}).button({translate:"ftb_sd.sort_mode_amount"}).button({translate:"ftb_sd.sort_mode_name"}).show(o).then((e=>{e.canceled||(0===e.selection?t.dimension.setBlockPermutation(t.location,t.permutation.withState("ftb_sd:sort_mode",1)):t.dimension.setBlockPermutation(t.location,t.permutation.withState("ftb_sd:sort_mode",2)))}));const l=t.permutation.getState("ftb_sd:sort_mode"),d=1===l?a.AMOUNT:a.NAME;let m=(new n).title({translate:"ftb_sd.drawer_viewer.title_"+(2===l?"amount":"name")}).body({translate:"ftb_sd.drawer_viewer.body"});const f=d.sort(t.dimension.getEntities({families:["ftb_sd:has_items"],maxDistance:8,location:t.location}));if(0===f.size)return void o.sendMessage({translate:"ftb_sd.no_drawers_found"});for(const[t,e]of f)m=m.button(`${s(t)}: ${e}`);const _=Array.from(f);m.show(o).then((s=>{if(s.canceled)return;const r=_[s.selection],n=r[0];if(0!==r[1])for(const s of t.dimension.getEntities({families:["ftb_sd:has_items"],maxDistance:8,location:t.location})){const t=e(s);t.allowedHands.forEach((e=>{const r=t.getStackInDrawer(s,e);if(void 0===r||r.typeId!==n)return;const a=t.getDrawerItemCount(s,e),i=Math.min(a,r.maxAmount);t.requestRemoveFromDrawer(i,o,r,e,s,s.dimension.getBlock(s.location))}))}else o.sendMessage({translate:"ftb_sd.no_items_to_remove"})}))}(t.block,t.player)}}class c{onPlayerInteract(e){t.forEach((t=>{if(t.isBlockDrawer(e.block)){if(e.block.permutation.getState("minecraft:cardinal_direction")!==o(e.face))return;t.shouldSneakCheck&&!e.player.isSneaking||r(e.player)||t.onUse(e.block,e.player)}else;}))}onPlace(e){for(const o of t)if(o.isBlockDrawer(e.block)){o.onPlace(void 0,e.block);break}}onTick(e){for(const o of t)if(o.isBlockDrawer(e.block)){o.tick(e.block);break}}}export{c as ClickableBlockComponent,i as DrawerViewer};
