import{i as r}from"./_virtual___federation_fn_import-DB-0B1B_.js";import{a as x}from"./zod-DVFqTTZn.js";import{l as I}from"./error-formatter-B41KnHG1.js";import{N as j}from"./error-Deo1phpd.js";import{s as B}from"./sortBy-C9bvycg5.js";const{createEnumBaseSchema:T,EnumUtils:U,FeatureUtils:L,modelEnumEntityType:m}=await r("@baseplate-dev/project-builder-lib"),{useDefinitionSchema:M,useProjectDefinition:O,useResettableForm:$}=await r("@baseplate-dev/project-builder-lib/web"),{useEventCallback:A}=await r("@baseplate-dev/ui-components"),{useNavigate:K}=await r("@tanstack/react-router"),{useMemo:u,useRef:P}=await r("react");function V(){return{id:m.generateNewId(),name:"",featureRef:"",isExposed:!1,values:[]}}function Q({enumKey:g,omit:D,onSubmitSuccess:N,isCreate:o}={}){const{definition:c,saveDefinitionWithFeedback:E,isSavingDefinition:k}=O(),b=K(),w=o?void 0:m.idFromKey(g),n=w?U.byId(c,w):void 0;if(!o&&!n)throw new j("Enum not found");const a=u(()=>V(),[]),i=M(T),d=P(D),f=u(()=>d.current?i.omit(Object.fromEntries(d.current.map(t=>[t,!0]))):i,[i]),v=u(()=>{const t=n??a;return d.current?f.parse(t):t},[n,a,f]),y=$({resolver:x(f),defaultValues:v}),{reset:l,handleSubmit:S,setError:h}=y,F=A(N),R=u(()=>S(t=>{const e=i.parse({...n,...t,id:n?.id??m.generateNewId()});if(c.enums?.find(s=>s.id!==e.id&&s.name.toLowerCase()===e.name.toLowerCase())){h("name",{message:`Enum with name ${e.name} already exists.`});return}return E(s=>{e.featureRef=L.ensureFeatureByNameRecursively(s,e.featureRef),s.enums=B([...s.enums?.filter(p=>p.id!==e.id)??[],e],[p=>p.name])},{successMessage:o?"Successfully created enum!":"Successfully saved enum!",onSuccess:()=>{o?(b({to:"/data/enums/edit/$key",params:{key:m.keyFromId(e.id)}}).catch(I),l(a)):l(t),F?.()}})}),[b,l,E,h,S,c,o,n,a,F,i]);return{form:y,onSubmit:R,defaultValues:v,isSavingDefinition:k}}export{Q as u};
//# sourceMappingURL=use-enum-form-BxerQuZY.js.map
