import{t as F,a as N}from"./DW82XzVD.js";import{p as P,K as g,L as E,M as y,s as o,t as B,g as e,a as D,c as n,r as s}from"./DNoLdQ8h.js";import{d as J,e as q}from"./CDjKoFya.js";import{s as K,r as L}from"./vLZe4-wl.js";import{b as M}from"./CHe8X9Gp.js";import{E as V,f as b}from"./CFmbs6M2.js";import{i as W,g as X}from"./D8tU3sxp.js";var Z=(v,a)=>{var i;return y(a,(i=v.target.files)==null?void 0:i[0],!0)},G=F('<form class="form-group"><div><label for="title" class="form-label">Tytuł</label> <input type="text" id="title" name="title" class="form-control"></div> <div><label for="cover" class="form-label">Okładka</label> <img alt="" class="cover svelte-18j8qky"> <input type="file" id="cover" name="cover" class="form-control"></div> <div><label for="content" class="form-label">Treść</label> <!></div> <div class="place-end"><button class="btn">Zapisz</button></div></form>');function et(v,a){var T,j;P(a,!0);let i=g(E(((T=a.article)==null?void 0:T.title)||"")),l=g(E(((j=a.article)==null?void 0:j.content)||{time:Date.now(),blocks:[]})),r=g(null);async function h(){const t=await b("/mgr/api/upload-image",{method:"POST",body:e(r),headers:{"Content-Type":e(r).type,"X-Filename":e(r).name}});if(!(t!=null&&t.ok))throw new Error("Coś poszło nie tak");return(await t.json()).file.url}async function z(){if(!e(r))throw new Error("Brak okładki");console.log("Saving article started...");const t=await h();return console.log("Article cover uploaded"),await b("/mgr/aktualnosci/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({title:e(i),content:e(l),coverUrl:t})})}async function C(){if(!a.article)throw new Error("Bład aplikacji. Nie mozna zaktualizowac artykułu jeśli on nie istnieje.");let t=a.article.coverUrl;return e(r)&&(t=await h()),await b("/mgr/aktualnosci/",{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({id:a.article.id,title:e(i),content:e(l),coverUrl:t})})}async function O(t){t.preventDefault();const p=await(a.article?C():z());p!=null&&p.ok&&(await W(),X("/mgr/aktualnosci"))}const S=()=>e(i)&&e(i).length>5&&e(l)&&e(l).blocks.length>0&&(!a.article&&e(r)||!0);var c=G(),d=n(c),k=o(n(d),2);L(k),s(d);var m=o(d,2),w=o(n(m),2),U=o(w,2);U.__change=[Z,r],s(m);var u=o(m,2),x=o(n(u),2);V(x,{get value(){return e(l)},set value(t){y(l,t,!0)}}),s(u);var _=o(u,2),A=n(_);s(_),s(c),B(t=>{var f;K(w,"src",(f=a.article)==null?void 0:f.coverUrl),A.disabled=t},[()=>!S()]),q("submit",c,t=>O(t)),M(k,()=>e(i),t=>y(i,t)),N(v,c),D()}J(["change"]);export{et as F};
