(()=>{"use strict";!function(){const e=window.arraySubsProfileFields;if(!e)return;const{nonce:t,apiBaseUrl:a,i18n:r}=e;function n(e){const t=a||"";return`${t.endsWith("/")?t:`${t}/`}${String(e||"").replace(/^\/+/,"")}`}function o(){if(!e.avatarEnabled)return;if(!document.getElementById("arraysubs-avatar-section"))return;const a=document.getElementById("arraysubs-avatar-input"),o=document.getElementById("arraysubs-avatar-preview"),s=document.getElementById("arraysubs-avatar-upload-btn"),i=document.getElementById("arraysubs-avatar-remove-btn"),c=document.getElementById("arraysubs-avatar-status");function l(e,t){c&&(c.textContent=e,c.className="arraysubs-avatar-section__help",t&&c.classList.add(`arraysubs-avatar-section__help--${t}`))}function d(e){s&&s.classList.toggle("arraysubs-avatar-section__upload-btn--loading",e),i&&(i.disabled=e),a&&(a.disabled=e)}function u(a){if(s){const e=a?r.changeAvatar:r.uploadAvatar,t=Array.from(s.childNodes).filter(e=>e.nodeType===Node.TEXT_NODE);t.length>0&&(t[0].textContent=e)}const i=document.getElementById("arraysubs-avatar-actions");if(!i)return;let c=document.getElementById("arraysubs-avatar-remove-btn");if(a&&!c){const a=document.createElement("button");a.type="button",a.className="button arraysubs-avatar-section__remove-btn",a.id="arraysubs-avatar-remove-btn",a.textContent=r.removeAvatar,i.appendChild(a),a.addEventListener("click",async function(){d(!0);try{const a=await fetch(n("arraysubs/v1/profile-fields/avatar"),{method:"DELETE",headers:{"X-WP-Nonce":t,"Content-Type":"application/json"}}),r=await a.json();a.ok&&r.content&&(o.src=r.content.url||e.gravatarUrl,u(!1))}catch{l(r.removeFailed,"error")}finally{d(!1)}})}else!a&&c&&c.remove()}a&&o&&(a.addEventListener("change",async function(){const s=this.files[0];if(!s)return;const i=1024*(e.avatarMaxSize||2)*1024;if(s.size>i)return l(r.fileTooLarge,"error"),void(this.value="");const c=s.name.split(".").pop().toLowerCase();if(!(e.avatarTypes||["jpg","jpeg","png","gif"]).includes(c))return l(r.invalidFileType,"error"),void(this.value="");l(r.uploading,"info"),d(!0);const f=new FormData;f.append("avatar",s);try{const e=await fetch(n("arraysubs/v1/profile-fields/avatar"),{method:"POST",headers:{"X-WP-Nonce":t},body:f}),a=await e.json();e.ok&&a.content&&a.content.url?(o.src=a.content.url,l("",""),u(!0)):l(a.message||r.uploadFailed,"error")}catch{l(r.uploadFailed,"error")}finally{d(!1),a.value=""}}),i&&i.addEventListener("click",async function(){d(!0),l("","");try{const a=await fetch(n("arraysubs/v1/profile-fields/avatar"),{method:"DELETE",headers:{"X-WP-Nonce":t,"Content-Type":"application/json"}}),s=await a.json();a.ok&&s.content?(o.src=s.content.url||e.gravatarUrl,u(!1)):l(s.message||r.removeFailed,"error")}catch{l(r.removeFailed,"error")}finally{d(!1)}}))}function s(){document.querySelectorAll(".arraysubs-profile-field__remove-file").forEach(e=>{e.addEventListener("click",function(){const e=this.dataset.field;if(!e)return;const t=this.closest(".arraysubs-profile-field--upload");if(!t)return;const a=t.querySelector(".arraysubs-profile-field__current-file");a&&(a.style.display="none");let r=t.querySelector(`input[name="arraysubs_remove_file_${e}"]`);r||(r=document.createElement("input"),r.type="hidden",r.name=`arraysubs_remove_file_${e}`,r.value="1",t.appendChild(r))})})}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",function(){o(),s()}):(o(),s())}()})();