import{j as e,aH as w,be as B,bP as W,al as Y,r as h,bD as Z,F as T,bl as z,aJ as y,dF as G,a4 as C,a5 as N,bp as q,bx as V,aA as X,aB as Q,cl as ee,dG as A,q as g,x as L,f as se,aY as F,bt as te,d as k,ci as ae,aZ as ne,aI as re,ao as oe,v as D,Z as ie,$ as ce,dt as le,h as de,dH as R,dI as me,dJ as pe,bW as he,bI as ue,bX as $,dK as xe,dD as ge,dL as fe,L as je}from"./index-C6OVXshM.js";import{r as Se}from"./randomArrayString---X8tfjV.js";import"./sort-BOLIcXtz.js";const ye=(t,n)=>[{accessorKey:"name",cell:({value:r,onChange:a,onBlur:s})=>e.jsx(w,{width:"100%",minWidth:"180px",placeholder:"property identifier",value:r,onChange:o=>{a(o.target.value)},onMouseLeave:o=>{s(o),n()}})},{accessorKey:"type",cell:({value:r,row:a})=>e.jsx(B,{width:"160px",value:r,"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:r,row:a})=>e.jsx(W,{width:"100%",isMulti:!0,value:r,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:r,row:a})=>e.jsx(Y,{ml:"8px",width:"80px",variant:"gray",checked:r,onChange:s=>{t(a.index,"required",!s)}})},{accessorKey:"description",cell:({value:r,onChange:a,onBlur:s})=>e.jsx(w,{width:"100%",minWidth:"472px",placeholder:"property description",value:r,onChange:o=>{a(o.target.value)},onMouseLeave:s})}],be=({properties:t,setProperties:n,isDisabled:r,onClearErrors:a})=>{const s=h.useCallback((p,m,i)=>{const S=t.map((u,x)=>x===p?Z(m,i,u):u);n(S)},[t,n]),o=h.useCallback(p=>{n(t.filter((m,i)=>i!==p))},[t,n]),j=()=>{n([{name:"",type:"String",required:!0,enum:void 0,description:""},...t])},d=ye(s,a);return e.jsxs(T,{children:[e.jsx(z,{mb:"10px",children:"Properties"}),e.jsx(y,{disabled:r,mb:"10px",onClick:j,children:"Add a property"}),!!t.length&&e.jsx(G,{columns:d,data:t,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})})]})},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,we=ie().shape({name:le.label("Name"),description:ce.label("Description")}),Te=({error:t,isDisabled:n,isEdit:r=!1,onSubmit:a})=>{var E,I;const{state:s}=q(),o=Ne(s),j=V(),[d,p]=h.useState(o==="JSON"?1:0),m=X({defaultValues:{name:(E=s==null?void 0:s.selected)!=null&&E.name?`copy_of_${s.selected.name}`:"",description:((I=s==null?void 0:s.selected)==null?void 0:I.description)||"",properties:o==="UI"?A(s==null?void 0:s.selected):[],jsonSchema:JSON.stringify(o==="JSON"?ee(["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(we)}),{formState:{errors:i},control:S,watch:u,register:x,setValue:f,setError:b,handleSubmit:H,clearErrors:P}=m,[v,J]=u(["jsonSchema","properties"]),K=H(({jsonSchema:c,...l})=>{if(!d){if(!l.properties.length){b("properties",{message:"Add at least one property"});return}if(l.properties.some(O=>!O.name)){b("properties",{message:"Name is required for all properties"});return}}a(d?{jsonSchema:c,...l}:l)});h.useEffect(()=>{try{const c=JSON.parse(v);!c.type||!c.properties?b("jsonSchema",{message:"invalid Json"}):P()}catch{b("jsonSchema",{message:"invalid Json"})}},[v]);const _=c=>{if(d!==c)if(c===1){if(!de(J)){const l=R(J);f("jsonSchema",JSON.stringify(me(JSON.parse(v),l),null,4))}p(c)}else try{const l=JSON.parse(v);if(pe(l)&&!window.confirm(Ce))return;const O=A(l);f("properties",O),p(c)}catch{}},M=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(L.settings.schematisedTags.list),children:"⇽ all schemas"}),!!t&&e.jsx(C,{content:t,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(se,{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:n,placeholder:"name of schema",...F("name",i)}),e.jsx(w,{label:"Description",...x("description"),disabled:n,optional:!0,labelProps:{ml:"20px",flex:1},placeholder:"description of the schema",...F("description",i)})]}),e.jsx(te.Controlled,{activeTab:d,onTabChange:_,my:"20px",tabs:[{title:"UI editor"},{title:"JSON editor"}]}),d===0&&e.jsx(ve,{properties:J,setProperties:M,isDisabled:n,onClearErrors:P}),d===1&&e.jsxs(g,{flexDirection:"column",children:[e.jsxs(g,{mb:"8px",children:[e.jsx(k,{flex:"1",children:"Properties"}),e.jsx(y,{p:"0",ml:"auto",width:"auto",intent:"inline",external:!0,target:"_blank",href:ae("/user_guides/fs/tags/tags/"),children:"Tags documentation↗"})]}),e.jsx(ne,{name:"jsonSchema",control:S,render:({field:{onChange:c,value:l}})=>e.jsx(re,{value:l,mode:"json",readOnly:!1,onChange:c})}),e.jsxs(g,{mt:"20px",children:[e.jsx(oe,{mr:"5px",size:"sm",color:i.jsonSchema?"labels.orange":"primary",icon:i.jsonSchema?D.warning:D.validate}),e.jsx(k,{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:n||!!Object.keys(i).length,onClick:K,mt:"20px",alignSelf:"flex-end",children:r?"Save":"Create new tag schema"})]})})]})},Ie=()=>{const t=he(),{state:n}=q(),r=V(),a=ue(),s=$(xe),o=$(ge),[j,d]=h.useState(),p=h.useCallback(async m=>{if(d(()=>""),!!o.find(({name:f})=>f===m.name)){d(()=>"Name already exists");return}const{properties:S,jsonSchema:u,...x}=m;await t.schematisedTagView.create({name:m.name,data:u?JSON.stringify({name:m.name,description:m.description,...JSON.parse(u||"{}")}):{...x,...R(S)}}),a.success({title:"New schema created",content:`${x.name} has been added`}),t.schematisedTags.clear(),localStorage.setItem(fe,Se(20)[0]),r(L.settings.schematisedTags.list,{state:n})},[t.schematisedTagView,t.schematisedTags,r,a,n,o]);return h.useEffect(()=>{t.schematisedTags.fetch()},[t]),s?e.jsx(je,{}):e.jsx(Te,{error:j,onSubmit:p,isDisabled:!1})};export{Ie as default};
