import{m as i,u as c}from"./madar-api-D5dAUylW.js";import"./index-DcNlVx-A.js";const u=document.getElementById("image-form"),o=document.getElementById("image-gallery"),y=document.getElementById("image-upgrade"),r=document.getElementById("image-style"),d=document.getElementById("image-enhance"),l=document.getElementById("image-enhanced-preview");let p=!1;async function v(){const{data:e}=await i.get("/ai/images/options"),t=c(e),a=await i.get("/subscription/status").catch(()=>null);p=(a?c(a.data)?.plan?.capabilities:{})?.save_attachments??!1,r&&t?.styles&&(r.innerHTML=t.styles.map(n=>`<option value="${n.key}">${n.label_ar||n.key}</option>`).join(""))}async function f(e){const t=e.image_url?.startsWith("http")?e.image_url:e.image_url?`${location.origin}${e.image_url}`:"";if(t&&t.includes("/storage/")||!e.id)return t;try{const a=await i.get(`/ai/images/${e.id}/file`,{responseType:"blob"});return URL.createObjectURL(a.data)}catch{return t}}async function m(){const{data:e}=await i.get("/ai/images"),t=c(e)?.items??[];if(!o)return;o.innerHTML='<p class="text-zinc-500">جاري التحميل...</p>';const a=[];for(const s of t){const n=await f(s),g=s.image_url?.startsWith("http")?s.image_url:`${location.origin}${s.image_url||""}`;a.push(`
        <div class="glass-card overflow-hidden">
            <img src="${n}" alt="" class="aspect-square w-full object-cover" loading="lazy">
            <div class="p-3">
                <p class="truncate text-xs text-zinc-400">${s.original_prompt||s.prompt||""}</p>
                <div class="mt-2 flex gap-2">
                    ${p&&n?`<a href="${n}" download="madar.png" class="text-xs text-cyan-400">تحميل</a>`:'<span class="text-xs text-zinc-500">تحميل (Pro)</span>'}
                    <button type="button" class="share-btn text-xs text-violet-400" data-url="${g}">مشاركة</button>
                </div>
            </div>
        </div>`)}o.innerHTML=a.join("")||'<p class="text-zinc-500">لا صور بعد</p>'}u?.addEventListener("submit",async e=>{e.preventDefault();const t=u.querySelector("[type=submit]");t.disabled=!0,t.textContent="جاري التوليد...";try{const a={prompt:document.getElementById("image-prompt").value,style:r?.value,aspect_ratio:document.getElementById("image-ratio")?.value,quality:document.getElementById("image-quality")?.value,auto_enhance:d?.checked??!0},{data:s}=await i.post("/ai/images",a),n=c(s);n.status==="completed"||n.result_url?await m():setTimeout(m,3e3)}catch(a){y?.classList.remove("hidden"),alert(a.madarMessage||"فشل التوليد")}finally{t.disabled=!1,t.textContent="توليد الصورة"}});d?.addEventListener("change",async()=>{if(!d.checked||!l)return;const e=document.getElementById("image-prompt")?.value;if(e?.trim())try{const{data:t}=await i.post("/ai/images/enhance-preview",{prompt:e,style:r?.value});l.textContent=c(t)?.enhanced_prompt||"",l.classList.remove("hidden")}catch{}});o?.addEventListener("click",e=>{const t=e.target.closest(".share-btn");t&&navigator.clipboard.writeText(t.dataset.url||location.href)});v();m();
