import{j as e,b2 as w,c3 as M,an as W,r as h,bt as Y,L as T,bF as z,b1 as b,dm as X,a3 as v,a5 as N,bg as L,bo as V,aC as G,aD as Q,ar as Z,c7 as ee,$ as g,q as _,b as se,aN as k,bk as te,f as I,c4 as ae,aO as re,b3 as ne,ap as oe,C as A,cu as ie,aE as ce,h as le,dn as me,bH as de,by as pe,bI as $,D as he}from"./index-46ma1THH.js";import{b as ue,s as xe,c as ge}from"./schematised-tags.selectors-WK3AUml0.js";import{r as fe}from"./randomArrayString---X8tfjV.js";import"./sort-BOLIcXtz.js";import{m as F,a as q,j as je}from"./utils-B4WkaoPO.js";import"./complement-Cl658Mix.js";const Se=(t,a)=>[{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),a()}})},{accessorKey:"type",cell:({value:r,row:n})=>e.jsx(M,{minWidth:"110px",value:r,onChange:s=>{t(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(W,{ml:"8px",width:"80px",variant:"gray",checked:r,onChange:s=>{t(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})}],be=({properties:t,setProperties:a,isDisabled:r,onClearErrors:n})=>{const s=h.useCallback((p,d,i)=>{const S=t.map((u,x)=>x===p?Y(d,i,u):u);a(S)},[t,a]),o=h.useCallback(p=>{a(t.filter((d,i)=>i!==p))},[t,a]),j=()=>{a([{name:"",type:["String"],required:!0,description:""},...t])},m=Se(s,n);return e.jsxs(T,{children:[e.jsx(z,{mb:"10px",children:"Properties"}),e.jsx(b,{disabled:r,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(T,{mt:"20px",children:e.jsx(v,{content:"Note that schematised tags are immutable",type:N.neutral})})]})},ye=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?`,ve=t=>t!=null&&t.selected?JSON.parse(t.selected.value).$schema?"JSON":"UI":null,Ne=Z().shape({name:ce.label("Name"),description:ie.label("Description")}),we=({error:t,isDisabled:a,isEdit:r=!1,onSubmit:n})=>{var P,D;const{state:s}=L(),o=ve(s),j=V(),[m,p]=h.useState(o==="JSON"?1:0),d=G({defaultValues:{name:(P=s==null?void 0:s.selected)!=null&&P.name?`copy_of_${s.selected.name}`:"",description:((D=s==null?void 0:s.selected)==null?void 0:D.description)||"",properties:o==="UI"?F(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(Ne)}),{formState:{errors:i},control:S,watch:u,register:x,setValue:f,setError:y,handleSubmit:R,clearErrors:O}=d,[C,J]=u(["jsonSchema","properties"]),H=R(({jsonSchema:c,...l})=>{if(!m){if(!l.properties.length){y("properties",{message:"Add at least one property"});return}if(l.properties.some(E=>!E.name)){y("properties",{message:"Name is required for all properties"});return}}n(m?{jsonSchema:c,...l}:l)});h.useEffect(()=>{try{const c=JSON.parse(C);!c.type||!c.properties?y("jsonSchema",{message:"invalid Json"}):O()}catch{y("jsonSchema",{message:"invalid Json"})}},[C]);const U=c=>{if(m!==c)if(c===1){if(!le(J)){const l=q(J);f("jsonSchema",JSON.stringify(me(JSON.parse(C),l),null,4))}p(c)}else try{const l=JSON.parse(C);if(je(l)&&!window.confirm(Ce))return;const E=F(l);f("properties",E),p(c)}catch{}},B=h.useCallback(c=>f("properties",c),[f]);return e.jsxs(g,{flexDirection:"column",alignItems:"center",children:[e.jsx(b,{ml:"-12px",intent:"inline",alignSelf:"flex-start",onClick:()=>j(_.settings.schematisedTags.list),children:"⇽ all schemas"}),!!t&&e.jsx(v,{content:t,type:N.error}),!!i.properties&&e.jsx(T,{width:"100%",mt:"10px",mb:"10px",children:e.jsx(v,{type:N.error,content:i.properties.message})}),e.jsx(se,{mt:"10px",width:"100%",title:"Create new tag schema",actions:s?e.jsx(b,{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:a,placeholder:"name of schema",...k("name",i)}),e.jsx(w,{label:"Description",...x("description"),disabled:a,optional:!0,labelProps:{ml:"20px",flex:1},placeholder:"description of the schema",...k("description",i)})]}),e.jsx(te.Controlled,{activeTab:m,onTabChange:U,my:"20px",tabs:[{title:"UI editor"},{title:"JSON editor"}]}),m===0&&e.jsx(ye,{properties:J,setProperties:B,isDisabled:a,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(b,{p:"0",ml:"auto",width:"auto",intent:"inline",external:!0,target:"_blank",href:ae("/user_guides/fs/tags/tags/"),children:"Tags documentation↗"})]}),e.jsx(re,{name:"jsonSchema",control:S,render:({field:{onChange:c,value:l}})=>e.jsx(ne,{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?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(v,{content:"Note that schematised tags are immutable",type:N.neutral})})]})]}),e.jsx(b,{disabled:a||!!Object.keys(i).length,onClick:H,mt:"20px",alignSelf:"flex-end",children:r?"Save":"Create new tag schema"})]})})]})},Ie=()=>{const t=de(),{state:a}=L(),r=V(),n=pe(),s=$(ue),o=$(xe),[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 t.schematisedTagView.create({name:d.name,data:u?JSON.stringify({name:d.name,description:d.description,...JSON.parse(u||"{}")}):{...x,...q(S)}}),n.success({title:"New schema created",content:`${x.name} has been added`}),t.schematisedTags.clear(),localStorage.setItem(ge,fe(20)[0]),r(_.settings.schematisedTags.list,{state:a})},[t.schematisedTagView,t.schematisedTags,r,n,a,o]);return h.useEffect(()=>{t.schematisedTags.fetch()},[t]),s?e.jsx(he,{}):e.jsx(we,{error:j,onSubmit:p,isDisabled:!1})};export{Ie as default};
