import{j as e,b5 as w,c4 as M,as as z,r as h,bv as W,d as T,bG as G,b4 as y,d6 as Y,ah as C,ai as N,al as Q,aG as X,cx as Z,bi as L,bp as R,aD as ee,c8 as se,aE as ae,B as g,s as V,y as te,aR as D,bk as re,b as I,c5 as ne,aS as oe,b6 as ie,au as ce,C as A,h as le,d7 as me,bI as de,bz as pe,bK as $,d8 as he,L as ue}from"./index-C1DmWO_C.js";import{b as xe,s as ge}from"./schematised-tags.selectors-xn4c-iOu.js";import{r as fe}from"./randomArrayString---X8tfjV.js";import"./sort-BOLIcXtz.js";import{m as F,a as B,j as je}from"./utils-C4DNH4Wu.js";const Se=(a,t)=>[{accessorKey:"name",cell:({value:r,onChange:n,onBlur:s})=>e.jsx(w,{width:"100%",minWidth:"180px",placeholder:"property identifier",value:r,onChange:o=>{n(o.target.value)},onMouseLeave:o=>{s(o),t()}})},{accessorKey:"type",cell:({value:r,row:n})=>e.jsx(M,{minWidth:"110px",value:r,onChange:s=>{a(n.index,"type",s)},options:["String","Integer","Float","Boolean","Array of Float","Array of Integer","Array of String","Array of Boolean"],placeholder:"",appendToBody:!0})},{accessorKey:"required",cell:({value:r,row:n})=>e.jsx(z,{ml:"8px",width:"80px",variant:"gray",checked:r,onChange:s=>{a(n.index,"required",!s)}})},{accessorKey:"description",cell:({value:r,onChange:n,onBlur:s})=>e.jsx(w,{width:"100%",minWidth:"472px",placeholder:"property description",value:r,onChange:o=>{n(o.target.value)},onMouseLeave:s})}],ye=({properties:a,setProperties:t,isDisabled:r,onClearErrors:n})=>{const s=h.useCallback((p,d,i)=>{const S=a.map((u,x)=>x===p?W(d,i,u):u);t(S)},[a,t]),o=h.useCallback(p=>{t(a.filter((d,i)=>i!==p))},[a,t]),j=()=>{t([{name:"",type:["String"],required:!0,description:""},...a])},m=Se(s,n);return e.jsxs(T,{children:[e.jsx(G,{mb:"10px",children:"Properties"}),e.jsx(y,{disabled:r,mb:"10px",onClick:j,children:"Add a property"}),!!a.length&&e.jsx(Y,{columns:m,data:a,hasFreezeButton:!1,updateData:s,onDeleteRow:o}),e.jsx(T,{mt:"20px",children:e.jsx(C,{content:"Note that schematised tags are immutable",type:N.neutral})})]})},be=h.memo(ye),ve=`You have properties that can not be mapped to the UI editor.
You might loose them. 
Are you sure you want to continue?`,Ce=a=>a!=null&&a.selected?JSON.parse(a.selected.value).$schema?"JSON":"UI":null,Ne=Q().shape({name:X.label("Name"),description:Z.label("Description")}),we=({error:a,isDisabled:t,isEdit:r=!1,onSubmit:n})=>{var P,k;const{state:s}=L(),o=Ce(s),j=R(),[m,p]=h.useState(o==="JSON"?1:0),d=ee({defaultValues:{name:(P=s==null?void 0:s.selected)!=null&&P.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:ae(Ne)}),{formState:{errors:i},control:S,watch:u,register:x,setValue:f,setError:b,handleSubmit:U,clearErrors:O}=d,[v,J]=u(["jsonSchema","properties"]),_=U(({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}}n(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 q=c=>{if(m!==c)if(c===1){if(!le(J)){const l=B(J);f("jsonSchema",JSON.stringify(me(JSON.parse(v),l),null,4))}p(c)}else try{const l=JSON.parse(v);if(je(l)&&!window.confirm(ve))return;const E=F(l);f("properties",E),p(c)}catch{}},K=h.useCallback(c=>f("properties",c),[f]);return e.jsxs(g,{flexDirection:"column",alignItems:"center",children:[e.jsx(y,{ml:"-12px",intent:"inline",alignSelf:"flex-start",onClick:()=>j(V.settings.schematisedTags.list),children:"⇽ all schemas"}),!!a&&e.jsx(C,{content:a,type:N.error}),!!i.properties&&e.jsx(T,{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(y,{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(w,{label:"Name",...x("name"),disabled:t,placeholder:"name of schema",...D("name",i)}),e.jsx(w,{label:"Description",...x("description"),disabled:t,optional:!0,labelProps:{ml:"20px",flex:1},placeholder:"description of the schema",...D("description",i)})]}),e.jsx(re.Controlled,{activeTab:m,onTabChange:q,my:"20px",tabs:[{title:"UI editor"},{title:"JSON editor"}]}),m===0&&e.jsx(be,{properties:J,setProperties:K,isDisabled:t,onClearErrors:O}),m===1&&e.jsxs(g,{flexDirection:"column",children:[e.jsxs(g,{mb:"8px",children:[e.jsx(I,{flex:"1",children:"Properties"}),e.jsx(y,{p:"0",ml:"auto",width:"auto",intent:"inline",external:!0,target:"_blank",href:ne("/user_guides/fs/tags/tags/"),children:"Tags documentation↗"})]}),e.jsx(oe,{name:"jsonSchema",control:S,render:({field:{onChange:c,value:l}})=>e.jsx(ie,{value:l,mode:"json",readOnly:!1,onChange:c})}),e.jsxs(g,{mt:"20px",children:[e.jsx(ce,{mr:"5px",size:"sm",color:i.jsonSchema?"labels.orange":"primary",icon:i.jsonSchema?A.warning:A.validate}),e.jsx(I,{color:i.jsonSchema?"labels.orange":"primary",children:i.jsonSchema?"json invalid":"json valid"})]}),e.jsx(T,{mt:"20px",children:e.jsx(C,{content:"Note that schematised tags are immutable",type:N.neutral})})]})]}),e.jsx(y,{disabled:t||!!Object.keys(i).length,onClick:_,mt:"20px",alignSelf:"flex-end",children:r?"Save":"Create new tag schema"})]})})]})},De=()=>{const a=de(),{state:t}=L(),r=R(),n=pe(),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:S,jsonSchema:u,...x}=d;await a.schematisedTagView.create({name:d.name,data:u?JSON.stringify({name:d.name,description:d.description,...JSON.parse(u||"{}")}):{...x,...B(S)}}),n.success({title:"New schema created",content:`${x.name} has been added`}),a.schematisedTags.clear(),localStorage.setItem(he,fe(20)[0]),r(V.settings.schematisedTags.list,{state:t})},[a.schematisedTagView,a.schematisedTags,r,n,t,o]);return h.useEffect(()=>{a.schematisedTags.fetch()},[a]),s?e.jsx(ue,{}):e.jsx(we,{error:j,onSubmit:p,isDisabled:!1})};export{De as default};
