import{j as e,aM as w,c0 as M,aq as z,r as h,bA as W,e as T,bK as Y,aG as y,c$ as Q,af as C,ag as N,aj as Z,aC as G,cs as X,bq as V,bw as F,az as ee,c3 as se,aA as ae,B as g,s as L,m as te,b8 as k,bs as re,Q as D,c1 as ne,b9 as oe,aJ as ie,as as ce,Z as I,g as le,d0 as me,aD as de,aE as pe,aL as $,d1 as he,L as ue}from"./index-dP1W1_bd.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 q,a as B,j as je}from"./utils-C2zMEnYg.js";import"./complement-D_Q2yZKt.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(Y,{mb:"10px",children:"Properties"}),e.jsx(y,{disabled:r,mb:"10px",onClick:j,children:"Add a property"}),!!a.length&&e.jsx(Q,{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=Z().shape({name:G.label("Name"),description:X.label("Description")}),we=({error:a,isDisabled:t,isEdit:r=!1,onSubmit:n})=>{var A,P;const{state:s}=V(),o=Ce(s),j=F(),[m,p]=h.useState(o==="JSON"?1:0),d=ee({defaultValues:{name:(A=s==null?void 0:s.selected)!=null&&A.name?`copy_of_${s.selected.name}`:"",description:((P=s==null?void 0:s.selected)==null?void 0:P.description)||"",properties:o==="UI"?q(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:R,clearErrors:O}=d,[v,J]=u(["jsonSchema","properties"]),_=R(({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 K=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=q(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(y,{ml:"-12px",intent:"inline",alignSelf:"flex-start",onClick:()=>j(L.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",...k("name",i)}),e.jsx(w,{label:"Description",...x("description"),disabled:t,optional:!0,labelProps:{ml:"20px",flex:1},placeholder:"description of the schema",...k("description",i)})]}),e.jsx(re.Controlled,{activeTab:m,onTabChange:K,my:"20px",tabs:[{title:"UI editor"},{title:"JSON editor"}]}),m===0&&e.jsx(be,{properties:J,setProperties:U,isDisabled:t,onClearErrors:O}),m===1&&e.jsxs(g,{flexDirection:"column",children:[e.jsxs(g,{mb:"8px",children:[e.jsx(D,{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?I.warning:I.validate}),e.jsx(D,{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}=V(),r=F(),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(L.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};
