import{j as e,aH as T,bd as M,bH as z,an as B,r as h,bu as Y,L as w,bG as G,aJ as S,dn as X,a3 as C,a5 as N,bg as L,bo as R,aC as Z,aD as Q,ar as ee,c8 as se,a as g,t as V,W as te,aY as P,bk as ae,f as A,c5 as re,aZ as ne,aI as oe,ap as ie,d as D,cv as ce,aE as le,l as me,dp as de,bI as pe,bz as he,bJ as $,D as ue}from"./index-_eM6l4Nf.js";import{b as xe,s as ge,c as fe}from"./schematised-tags.selectors-WK3AUml0.js";import{r as je}from"./randomArrayString---X8tfjV.js";import"./sort-BOLIcXtz.js";import{m as F,a as q,j as ye}from"./utils-Cwnan-GL.js";import"./complement-CE-nEpWn.js";const Se=(t,r)=>[{accessorKey:"name",cell:({value:n,onChange:a,onBlur:s})=>e.jsx(T,{width:"100%",minWidth:"180px",placeholder:"property identifier",value:n,onChange:o=>{a(o.target.value)},onMouseLeave:o=>{s(o),r()}})},{accessorKey:"type",cell:({value:n,row:a})=>e.jsx(M,{width:"160px",value:n,"data-test":`${a.index}-type-select`,onChange:s=>{t(a.index,"type",s)},options:["String","Integer","Float","Boolean","Array of Float","Array of Integer","Array of String","Array of Boolean"],placeholder:""})},{accessorKey:"enum",cell:({value:n,row:a})=>e.jsx(z,{width:"100%",isMulti:!0,value:n,onChange:s=>{t(a.index,"enum",s)},options:[],placeholder:a.original.type!=="String"?"Requires String type":"No enum values",disabled:a.original.type!=="String"})},{accessorKey:"required",cell:({value:n,row:a})=>e.jsx(B,{ml:"8px",width:"80px",variant:"gray",checked:n,onChange:s=>{t(a.index,"required",!s)}})},{accessorKey:"description",cell:({value:n,onChange:a,onBlur:s})=>e.jsx(T,{width:"100%",minWidth:"472px",placeholder:"property description",value:n,onChange:o=>{a(o.target.value)},onMouseLeave:s})}],be=({properties:t,setProperties:r,isDisabled:n,onClearErrors:a})=>{const s=h.useCallback((p,d,i)=>{const y=t.map((u,x)=>x===p?Y(d,i,u):u);r(y)},[t,r]),o=h.useCallback(p=>{r(t.filter((d,i)=>i!==p))},[t,r]),j=()=>{r([{name:"",type:"String",required:!0,enum:void 0,description:""},...t])},m=Se(s,a);return e.jsxs(w,{children:[e.jsx(G,{mb:"10px",children:"Properties"}),e.jsx(S,{disabled:n,mb:"10px",onClick:j,children:"Add a property"}),!!t.length&&e.jsx(X,{columns:m,data:t,hasFreezeButton:!1,updateData:s,onDeleteRow:o}),e.jsx(w,{mt:"20px",children:e.jsx(C,{content:"Note that schematised tags are immutable",type:N.neutral})})]})},ve=h.memo(be),Ce=`You have properties that can not be mapped to the UI editor.
You might loose them. 
Are you sure you want to continue?`,Ne=t=>t!=null&&t.selected?JSON.parse(t.selected.value).$schema?"JSON":"UI":null,Te=ee().shape({name:le.label("Name"),description:ce.label("Description")}),we=({error:t,isDisabled:r,isEdit:n=!1,onSubmit:a})=>{var I,k;const{state:s}=L(),o=Ne(s),j=R(),[m,p]=h.useState(o==="JSON"?1:0),d=Z({defaultValues:{name:(I=s==null?void 0:s.selected)!=null&&I.name?`copy_of_${s.selected.name}`:"",description:((k=s==null?void 0:s.selected)==null?void 0:k.description)||"",properties:o==="UI"?F(s==null?void 0:s.selected):[],jsonSchema:JSON.stringify(o==="JSON"?se(["name","description"],JSON.parse(s.selected.value)):{$schema:"http://json-schema.org/draft-07/schema#",type:"object",properties:{id:{type:"string"}},required:["id"],additionalProperties:!1},null,2)},shouldUnregister:!1,resolver:Q(Te)}),{formState:{errors:i},control:y,watch:u,register:x,setValue:f,setError:b,handleSubmit:H,clearErrors:O}=d,[v,J]=u(["jsonSchema","properties"]),_=H(({jsonSchema:c,...l})=>{if(!m){if(!l.properties.length){b("properties",{message:"Add at least one property"});return}if(l.properties.some(E=>!E.name)){b("properties",{message:"Name is required for all properties"});return}}a(m?{jsonSchema:c,...l}:l)});h.useEffect(()=>{try{const c=JSON.parse(v);!c.type||!c.properties?b("jsonSchema",{message:"invalid Json"}):O()}catch{b("jsonSchema",{message:"invalid Json"})}},[v]);const K=c=>{if(m!==c)if(c===1){if(!me(J)){const l=q(J);f("jsonSchema",JSON.stringify(de(JSON.parse(v),l),null,4))}p(c)}else try{const l=JSON.parse(v);if(ye(l)&&!window.confirm(Ce))return;const E=F(l);f("properties",E),p(c)}catch{}},U=h.useCallback(c=>f("properties",c),[f]);return e.jsxs(g,{flexDirection:"column",alignItems:"center",children:[e.jsx(S,{ml:"-12px",intent:"inline",alignSelf:"flex-start",onClick:()=>j(V.settings.schematisedTags.list),children:"⇽ all schemas"}),!!t&&e.jsx(C,{content:t,type:N.error}),!!i.properties&&e.jsx(w,{width:"100%",mt:"10px",mb:"10px",children:e.jsx(C,{type:N.error,content:i.properties.message})}),e.jsx(te,{mt:"10px",width:"100%",title:"Create new tag schema",actions:s?e.jsx(S,{p:0,intent:"inline",href:s.pathname,children:s.text}):null,maxHeight:"calc(100vh - 265px)",overflow:"hidden",children:e.jsxs(g,{flexDirection:"column",children:[e.jsxs(g,{flexDirection:"column",children:[e.jsxs(g,{children:[e.jsx(T,{label:"Name",...x("name"),disabled:r,placeholder:"name of schema",...P("name",i)}),e.jsx(T,{label:"Description",...x("description"),disabled:r,optional:!0,labelProps:{ml:"20px",flex:1},placeholder:"description of the schema",...P("description",i)})]}),e.jsx(ae.Controlled,{activeTab:m,onTabChange:K,my:"20px",tabs:[{title:"UI editor"},{title:"JSON editor"}]}),m===0&&e.jsx(ve,{properties:J,setProperties:U,isDisabled:r,onClearErrors:O}),m===1&&e.jsxs(g,{flexDirection:"column",children:[e.jsxs(g,{mb:"8px",children:[e.jsx(A,{flex:"1",children:"Properties"}),e.jsx(S,{p:"0",ml:"auto",width:"auto",intent:"inline",external:!0,target:"_blank",href:re("/user_guides/fs/tags/tags/"),children:"Tags documentation↗"})]}),e.jsx(ne,{name:"jsonSchema",control:y,render:({field:{onChange:c,value:l}})=>e.jsx(oe,{value:l,mode:"json",readOnly:!1,onChange:c})}),e.jsxs(g,{mt:"20px",children:[e.jsx(ie,{mr:"5px",size:"sm",color:i.jsonSchema?"labels.orange":"primary",icon:i.jsonSchema?D.warning:D.validate}),e.jsx(A,{color:i.jsonSchema?"labels.orange":"primary",children:i.jsonSchema?"json invalid":"json valid"})]}),e.jsx(w,{mt:"20px",children:e.jsx(C,{content:"Note that schematised tags are immutable",type:N.neutral})})]})]}),e.jsx(S,{disabled:r||!!Object.keys(i).length,onClick:_,mt:"20px",alignSelf:"flex-end",children:n?"Save":"Create new tag schema"})]})})]})},De=()=>{const t=pe(),{state:r}=L(),n=R(),a=he(),s=$(xe),o=$(ge),[j,m]=h.useState(),p=h.useCallback(async d=>{if(m(()=>""),!!o.find(({name:f})=>f===d.name)){m(()=>"Name already exists");return}const{properties:y,jsonSchema:u,...x}=d;await t.schematisedTagView.create({name:d.name,data:u?JSON.stringify({name:d.name,description:d.description,...JSON.parse(u||"{}")}):{...x,...q(y)}}),a.success({title:"New schema created",content:`${x.name} has been added`}),t.schematisedTags.clear(),localStorage.setItem(fe,je(20)[0]),n(V.settings.schematisedTags.list,{state:r})},[t.schematisedTagView,t.schematisedTags,n,a,r,o]);return h.useEffect(()=>{t.schematisedTags.fetch()},[t]),s?e.jsx(ue,{}):e.jsx(we,{error:j,onSubmit:p,isDisabled:!1})};export{De as default};
