(()=>{"use strict";var e,t={21:()=>{const e=window.wp.blocks,t=window.React,a=window.wp.element,n=window.wp.components,o=window.wp.blockEditor,r=window.wp.i18n,l={},s=(e,t)=>e.cmc!==t.cmc?e.cmc-t.cmc:e.name.localeCompare(t.name),c=(e,a)=>((e=>{e.length})(a),a.length>0?(0,t.createElement)("div",{key:e},(0,t.createElement)("h2",{className:"has-small-font-size"},`${e} (${(e=>e.reduce(((e,t)=>e+(t.quantity||1)),0))(a)})`),(0,t.createElement)("div",{className:"mtg-tools-card-category"},a.map(((e,a)=>(0,t.createElement)("div",{key:e.id||a,className:"mtg-tools-card has-small-font-size","data-card-name":e.scryfallName||e.name,"data-card-front-image-uri":e.frontImage||"","data-card-back-image-uri":e.backImage||"","data-card-foil":e.foil?"Yes":"No"},(0,t.createElement)("span",{className:"mtg-tools-quantity"},e.quantity||1),(0,t.createElement)("div",{className:"mtg-tools-card-name"},e.scryfallName||e.name),e.backImage&&(0,t.createElement)("div",{className:"mtg-tools-flip-button"},(0,t.createElement)("button",{className:"mtg-tools-flip dashicons dashicons-image-rotate",type:"button"}))))))):null),m=JSON.parse('{"UU":"mtg-tools/mtg-tools"}');(0,e.registerBlockType)(m.UU,{edit:function({attributes:e,setAttributes:s}){const c=(0,o.useBlockProps)(),[m,i]=(0,a.useState)(e.cards||[]),[d,u]=(0,a.useState)(!1),f=(0,a.useRef)(null),g=["Token","Creature","Land","Artifact","Enchantment","Planeswalker","Battle","Instant","Sorcery"],p=(e,t,a)=>{const n=[...m];n[e][t]=a,i(n),s({cards:n}),"name"!==t&&"set"!==t&&"number"!==t||((e,t)=>{clearTimeout(f.current),f.current=setTimeout((()=>{u(!0),(async(e,t)=>{const a=`${e.name}-${e.set}-${e.number}`;if(l[a])return l[a];let n="";n=e.set&&e.number?`https://api.scryfall.com/cards/${encodeURIComponent(e.set)}/${encodeURIComponent(e.number)}`:`https://api.scryfall.com/cards/named?fuzzy=${encodeURIComponent(e.name)}&set=${encodeURIComponent(e.set)}`;try{const e=await fetch(n),t=await e.json();if(200===e.status)return l[a]=t,t;if(404===e.status)return{status:404,message:"Not Found"}}catch(e){}finally{u(!1)}})(e).then((a=>{if(a){const n=[...m];let o=null,r=null,l="",c="",d=e.name;if(404===a.status)n[t]={...n[t],notFound:!0};else{"modal_dfc"===a.layout||"transform"===a.layout?(o=a.card_faces?.[0]?.image_uris?.normal||null,r=a.card_faces?.[1]?.image_uris?.normal||null,l=a.card_faces?.[0]?.mana_cost,c=a.card_faces?.[0]?.type_line,d=a.card_faces?.[0]?.name):"adventure"===a.layout?(o=a.image_uris?.normal||null,l=a.mana_cost,c=a.card_faces?.[0]?.type_line||a.type_line,d=a.card_faces?.[0]?.name||a.name):(o=a.image_uris?.normal||null,l=a.mana_cost,c=a.type_line,d=a.name);const e=(e=>{const t=e.split(" ");for(let e of g)if(t.includes(e))return e;return t[0]||"Unknown"})(c);n[t]={...n[t],scryfallName:d,scryfallSet:a.set_name,scryfallCollectorNumber:a.collector_number,cmc:a.cmc,manaCost:l,type:e,frontImage:o,backImage:r,scryfall_uri:a.scryfall_uri,notFound:!1}}i(n),s({cards:n})}}))}),1e3)})(n[e],e)};return(0,t.createElement)("div",{...c},d,m.map(((e,a)=>(0,t.createElement)("div",{key:a,className:"mtg-card"},(0,t.createElement)("div",{className:"card-inputs"},(0,t.createElement)(n.TextControl,{label:(0,r.__)("Name","mtg-tools"),value:e.name,onChange:e=>p(a,"name",e)}),(0,t.createElement)(n.TextControl,{label:(0,r.__)("Set","mtg-tools"),value:e.set,onChange:e=>p(a,"set",e)}),(0,t.createElement)(n.TextControl,{label:(0,r.__)("Number","mtg-tools"),value:e.number,onChange:e=>p(a,"number",e)}),(0,t.createElement)(n.TextControl,{label:(0,r.__)("Quantity","mtg-tools"),type:"number",value:e.quantity,onChange:e=>p(a,"quantity",parseInt(e))}),(0,t.createElement)(n.CheckboxControl,{label:(0,r.__)("Commander","mtg-tools"),checked:e.commander,onChange:e=>p(a,"commander",e)}),(0,t.createElement)(n.CheckboxControl,{label:(0,r.__)("Foil","mtg-tools"),checked:e.foil,onChange:e=>p(a,"foil",e)}),(0,t.createElement)(n.CheckboxControl,{label:(0,r.__)("Sideboard","mtg-tools"),checked:e.Sideboard,onChange:e=>p(a,"sideboard",e)})),(0,t.createElement)("div",{className:"card-preview"},e.notFound?(0,t.createElement)("div",{className:"not-found"},(0,r.__)("Not Found","mtg-tools")):e.frontImage?(0,t.createElement)("img",{src:e.frontImage,alt:e.name}):(0,t.createElement)("div",{className:"placeholder"})),(0,t.createElement)(n.Button,{isDestructive:!0,onClick:()=>(e=>{const t=m.filter(((t,a)=>a!==e));i(t),s({cards:t})})(a)},(0,r.__)("Remove Card","mtg-tools"))))),(0,t.createElement)(n.Button,{isPrimary:!0,onClick:()=>{const e={name:"",set:"",number:"",quantity:1,commander:!1,foil:!1,sideboard:!1};i([...m,e]),s({cards:[...m,e]})}},(0,r.__)("Add Card","mtg-tools")))},save:function({attributes:e}){const a=o.useBlockProps.save(),{cards:n=[]}=e,r=n.filter((e=>e.commander)).sort(s),l=n.filter((e=>e.sideboard)).sort(s),m=[{type:"Artifacts",condition:e=>"Artifact"===e.type&&!e.commander&&!e.sideboard},{type:"Battles",condition:e=>"Battle"===e.type&&!e.commander&&!e.sideboard},{type:"Creatures",condition:e=>"Creature"===e.type&&!e.commander&&!e.sideboard},{type:"Planeswalkers",condition:e=>"Planeswalker"===e.type&&!e.commander&&!e.sideboard},{type:"Enchantments",condition:e=>"Enchantment"===e.type&&!e.commander&&!e.sideboard},{type:"Instants",condition:e=>"Instant"===e.type&&!e.commander&&!e.sideboard},{type:"Sorceries",condition:e=>"Sorcery"===e.type&&!e.commander&&!e.sideboard},{type:"Lands",condition:e=>"Land"===e.type&&!e.commander&&!e.sideboard}].reduce(((e,t)=>(e[t.type]=n.filter(t.condition).sort(s),e)),{}),i={Commander:r,...m,Tokens:n.filter((e=>"Token"===e.type&&!e.commander)).sort(s),Sideboard:l},d=e=>e.length,u=Object.values(i).reduce(((e,t)=>e+d(t)),0),f=Math.ceil(u/2);let g={Commander:i.Commander},p={},b=d(i.Commander);delete i.Commander;for(const[e,t]of Object.entries(i))if("Tokens"!==e&&"Sideboard"!==e){const a=d(t);b+a<=f?(g[e]=t,b+=a):p[e]=t}p.Tokens=i.Tokens,p.Sideboard=i.Sideboard;const y=r[0]||Object.values(m).flat()[0],_=y?.frontImage,v=y?.scryfallName||"Magic: The Gathering Card",h=y?.foil?"Yes":"No";return(0,t.createElement)("div",{...a,className:"mtg-tools-container"},(0,t.createElement)("div",{className:"mtg-tools-column mtg-tools-image-column"},(0,t.createElement)("div",{className:"mtg-tools-sticky"},(0,t.createElement)("img",{id:"mtg-tools-default-image",src:_,alt:v,className:"mtg-tools-image","data-foil":h}),y?.foil&&(0,t.createElement)("div",{className:"mtg-tools-gradient-overlay"}))),(0,t.createElement)("div",{className:"mtg-tools-column"},Object.entries(g).map((([e,t])=>c(e,t)))),(0,t.createElement)("div",{className:"mtg-tools-column"},Object.entries(p).map((([e,t])=>c(e,t)))))}})}},a={};function n(e){var o=a[e];if(void 0!==o)return o.exports;var r=a[e]={exports:{}};return t[e](r,r.exports,n),r.exports}n.m=t,e=[],n.O=(t,a,o,r)=>{if(!a){var l=1/0;for(i=0;i=r)&&Object.keys(n.O).every((e=>n.O[e](a[c])))?a.splice(c--,1):(s=!1,r0&&e[i-1][2]>r;i--)e[i]=e[i-1];e[i]=[a,o,r]},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e={57:0,350:0};n.O.j=t=>0===e[t];var t=(t,a)=>{var o,r,[l,s,c]=a,m=0;if(l.some((t=>0!==e[t]))){for(o in s)n.o(s,o)&&(n.m[o]=s[o]);if(c)var i=c(n)}for(t&&t(a);mn(21)));o=n.O(o)})();