import"../chunks/DsnmJJEf.js";import{p as ne,i as _,m as de,u as ce,g as t,a as v,b as fe,h as I,j as c,k as ve,o as ue,c as m,s as b,f as me,t as A,d as o,$ as pe,r as l,n as J,l as _e}from"../chunks/BfUaJvxP.js";import{d as ge,e as xe,a as he,s as F}from"../chunks/Bci9YmGs.js";import{i as O}from"../chunks/C8yDvkQM.js";import{h as ye,s as L}from"../chunks/VWvGCFra.js";import{i as M}from"../chunks/CF1ujZlL.js";import{p as be}from"../chunks/DpTm09Um.js";import{g as B}from"../chunks/TWCB2oXp.js";import{b as D}from"../chunks/DlhQF7fG.js";import{c as $e,g as je,f as we}from"../chunks/DJbHolJ5.js";import{B as ke}from"../chunks/OPr8ajXy.js";import{B as R,t as W}from"../chunks/CuIpA3jY.js";import{S as Ee}from"../chunks/Cvq1zDM5.js";import{A as Be}from"../chunks/Cdd5krRs.js";import{L as G}from"../chunks/BeSnoT4y.js";import{S as De}from"../chunks/BZvocykC.js";import{P as ze}from"../chunks/Ca-xaGfc.js";var Ce=m('<div class="flex h-8 w-8 items-center justify-center rounded-lg bg-primary/10 text-primary"><!></div>'),Ne=m("<!> Create",1),Se=m('<div><div><div class="flex items-center gap-3"><button type="button" class="inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"><!></button> <!> <h1> </h1></div> <!></div></div>'),Te=m('<div class="flex flex-1 items-center justify-center"><!></div>'),Ae=m('<div class="flex flex-1 items-center justify-center"><div class="text-center"><p class="text-sm text-destructive"> </p> <!></div></div>'),Fe=m('<div class="flex-1 overflow-y-auto"><div class="space-y-6 px-6 py-6"><!></div></div>'),Oe=m('<div class="flex h-full flex-col"><!> <!></div>');function et(H,q){ne(q,!0);const d=I(()=>be.params.type??"");let z=_(null),g=_(null),C=_(!0),x=_(!1),$=_(""),u=_(de({}));const P=I(()=>t(u)._post_entries_save??"edit");function N(e){const r={};for(const a of e){if(a.default!==void 0){const s=a.name.includes(".")?a.name.split(".").pop():a.name;r[s]=a.default}a.fields&&Object.assign(r,N(a.fields))}return r}async function Q(){c(C,!0),c($,"");try{const[e,r]=await Promise.all([$e(t(d)),je().catch(()=>[])]);c(g,e,!0),c(z,r.find(a=>a.type===t(d))??null,!0),c(u,N(e.fields),!0)}catch{c($,`Failed to load blueprint for '${t(d)}'.`)}finally{c(C,!1)}}function U(e,r){const a=e.split("."),s={...t(u)};let i=s;for(let f=0;f<a.length-1;f++){const n=a[f];typeof i[n]!="object"||i[n]===null?i[n]={}:i[n]={...i[n]},i=i[n]}i[a[a.length-1]]=r,c(u,s,!0)}async function K(){c(x,!0);try{const{_post_entries_save:e,...r}=t(u),a=await we(t(d),r);W.success(M.t("ADMIN_NEXT.FLEX_OBJECTS.NEW.OBJECT_CREATED")),t(P)==="create-new"?c(u,t(g)?N(t(g).fields):{},!0):t(P)==="list"?B(`${D}/flex-objects/${t(d)}`):B(`${D}/flex-objects/${t(d)}/${a.key}`)}catch(e){const r=e&&typeof e=="object"&&"message"in e?e.message:"Failed to create object";W.error(r)}finally{c(x,!1)}}function V(e){(e.metaKey||e.ctrlKey)&&e.key==="s"&&(e.preventDefault(),t(x)||K())}ce(()=>{t(d),Q()});var S=Oe();ye("fhxi7h",e=>{ve(()=>{pe.title=`New ${t(z)?.title??t(d)??""} — Grav Admin`})}),xe("keydown",ue,V);var X=o(S);Ee(X,{children:(r,a)=>{let s=()=>a?.().scrolled;var i=Se(),f=o(i),n=o(f),h=o(n),j=o(h);Be(j,{size:16}),l(h);var w=b(h,2);{var ae=y=>{var k=Ce(),E=o(k);ze(E,{size:16}),l(k),v(y,k)};O(w,y=>{s()||y(ae)})}var T=b(w,2),re=o(T);l(T),l(n);var se=b(n,2);R(se,{size:"sm",onclick:K,get disabled(){return t(x)},children:(y,k)=>{var E=Ne(),ie=me(E);{var oe=p=>{G(p,{size:14,class:"mr-1.5 animate-spin"})},le=p=>{De(p,{size:14,class:"mr-1.5"})};O(ie,p=>{t(x)?p(oe):p(le,-1)})}J(),v(y,E)},$$slots:{default:!0}}),l(f),l(i),A(()=>{L(i,1,`px-6 transition-[padding] duration-200 ${s()?"py-2":"pt-6 pb-3"}`),L(f,1,`flex items-center justify-between gap-4 ${s()?"min-h-6":"min-h-8"}`),L(T,1,`font-semibold text-foreground transition-[font-size] duration-200 ${s()?"text-sm":"text-lg"}`),F(re,`New ${t(z)?.title??t(d)??""}`)}),he("click",h,()=>B(`${D}/flex-objects/${t(d)}`)),v(r,i)},$$slots:{default:!0}});var Y=b(X,2);{var Z=e=>{var r=Te(),a=o(r);G(a,{size:24,class:"animate-spin text-muted-foreground"}),l(r),v(e,r)},ee=e=>{var r=Ae(),a=o(r),s=o(a),i=o(s,!0);l(s);var f=b(s,2);R(f,{variant:"outline",size:"sm",class:"mt-3",onclick:()=>B(`${D}/flex-objects/${t(d)}`),children:(n,h)=>{J();var j=_e();A(w=>F(j,w),[()=>M.t("ADMIN_NEXT.FLEX_OBJECTS.NEW.BACK_TO_LIST")]),v(n,j)},$$slots:{default:!0}}),l(a),l(r),A(()=>F(i,t($))),v(e,r)},te=e=>{var r=Fe(),a=o(r),s=o(a);ke(s,{get fields(){return t(g).fields},get data(){return t(u)},onchange:U}),l(a),l(r),v(e,r)};O(Y,e=>{t(C)?e(Z):t($)?e(ee,1):t(g)&&e(te,2)})}l(S),v(H,S),fe()}ge(["click"]);export{et as component};
