import{r as e}from"./rolldown-runtime-Dw2cE7zH.js";import{E as t,G as n,H as r,I as i,K as a,M as o,P as s,Q as ee,R as c,U as l,X as te,b as u,f as d,g as f,k as p,l as ne,m as re,mt as m,p as ie,s as h,u as g,w as _,x as v,z as y}from"./antd-NNbrPHoG.js";import{I as b,V as ae,_ as oe,et as se,v as ce}from"./antd-icons-BW4cpGcx.js";import{t as le}from"./i18n-BuHCC44e.js";import{n as x}from"./api-BW0YTlHd.js";import{t as S}from"./react-vendor-nIJYBVRh.js";import{n as ue}from"./AuthContext-Bb7tFn4r.js";import{t as de}from"./ConsolePage-BgvAIq9_.js";import{t as fe}from"./PageToolbar-Dmyk1Tio.js";import{t as pe}from"./ConfirmDeleteButton-CRkFEjCr.js";import{n as me,r as he,t as ge}from"./useInitialTableLoading-adK-LcHZ.js";import{t as C}from"./tablePagination-BkASz1xx.js";var w=e(m(),1),T=S(),E=`function alarm_level(temperature, client_id)
  if temperature > 80 then
    return client_id .. ": high"
  end
  return client_id .. ": normal"
end`,D=`fn alarm_level(temperature, client_id) {
  if temperature > 80 {
    client_id + ": high"
  } else {
    client_id + ": normal"
  }
}`,O=[`88`,`"device-1"`],k=[`21`];function A(e=`Lua`){return{id:``,name:``,call_name:``,version:`v1`,runtime:e,output_type:N(e),sha256:``,enabled:!0,source_code:e===`Rhai`?D:e===`Lua`?E:``,module_bytes:void 0,entrypoint:e===`Wasm`?`velamq_udf`:void 0,timeout_ms:20,max_memory_bytes:16*1024*1024,max_output_bytes:256*1024,created_at_ms:0,updated_at_ms:0}}function _e(e){if(!e?.length)return``;let t=``,n=32768;for(let r=0;r<e.length;r+=n)t+=String.fromCharCode(...e.slice(r,r+n));return window.btoa(t)}function j(e){let t=String(e??``).trim().replace(/^data:.*;base64,/,``).replace(/\s+/g,``);if(!t)return;let n=window.atob(t);return Array.from(n,e=>e.charCodeAt(0))}function M(e){return e.trim().replace(/[^a-zA-Z0-9_]+/g,`_`).replace(/^([^a-zA-Z_])/,`_$1`).replace(/_+/g,`_`).replace(/^_+$/,``).slice(0,64)}function N(e){return e===`Wasm`?`I64`:`Json`}function P(e,t,n,r){return e===`Lua`?String(t??``).match(/(?:^|\n)\s*(?:local\s+)?function\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/)?.[1]??``:e===`Rhai`?String(t??``).match(/(?:^|\n)\s*fn\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/)?.[1]??``:String(n??``).trim()||M(String(r??``))}function F(e,t){return t(e===`Lua`?`functions.luaCallNameRequired`:e===`Rhai`?`functions.rhaiCallNameRequired`:`functions.wasmEntrypointRequired`)}function I(e,t){return t(`functions.requiredField`,{field:e})}function L(e,t){let n=e.runtime??`Lua`,r=n===`Wasm`?j(e.module_base64):void 0,i=n===`Wasm`?void 0:String(e.source_code??``),a=n===`Wasm`?String(e.entrypoint??`velamq_udf`).trim():void 0,o=P(n,i,a,e.name);if(!o)throw Error(F(n,t));return{...A(n),...e,name:String(e.name??``).trim(),call_name:o,version:`v1`,runtime:n,output_type:N(n),source_code:i,module_bytes:r,entrypoint:a,timeout_ms:Number(e.timeout_ms??20),max_memory_bytes:Number(e.max_memory_bytes??16*1024*1024),max_output_bytes:Number(e.max_output_bytes??256*1024)}}function R(e){return e.success?typeof e.output==`string`?e.output:JSON.stringify(e.output,null,2):e.error||JSON.stringify(e,null,2)}function z(e){return e===`Wasm`?[...k]:[...O]}function B(e){let t=e.trim();if(!t)return null;try{return JSON.parse(t)}catch{return t}}function V(){let{t:e}=le(),{canWrite:m}=ue(),[S,E]=(0,w.useState)([]),[D,O]=(0,w.useState)(!1),k=ge(D),[M,N]=(0,w.useState)(!1),[P,F]=(0,w.useState)(null),[V,H]=(0,w.useState)(!1),[ve,U]=(0,w.useState)(!1),[W,ye]=(0,w.useState)(null),[G,K]=(0,w.useState)(z(`Lua`)),[q,J]=(0,w.useState)(!1),[Y,X]=(0,w.useState)(``),[Z]=t.useForm(),Q=async()=>{O(!0);try{E(he(await x.listFunctions(),e=>e.updated_at_ms))}catch(e){v.error(e instanceof Error?e.message:String(e))}finally{O(!1)}};(0,w.useEffect)(()=>{Q()},[]);let $=t.useWatch(`runtime`,Z),be=()=>{F(null),Z.setFieldsValue(A(`Lua`)),K(z(`Lua`)),N(!0),X(``)},xe=e=>{F(e),Z.setFieldsValue({...A(e.runtime),id:e.id,name:e.name,call_name:e.call_name,version:e.version,runtime:e.runtime,output_type:e.output_type,enabled:e.enabled,sha256:e.sha256,source_code:e.source_code,module_bytes:e.module_bytes,module_base64:_e(e.module_bytes),entrypoint:e.entrypoint,timeout_ms:e.timeout_ms,max_memory_bytes:e.max_memory_bytes,max_output_bytes:e.max_output_bytes,created_at_ms:e.created_at_ms,updated_at_ms:e.updated_at_ms}),K(z(e.runtime)),N(!0),X(``)},Se=async()=>{let t=await Z.validateFields();H(!0);try{await x.upsertFunction(L(t,e)),v.success(e(`functions.saved`)),N(!1),await Q()}catch(e){v.error(e instanceof Error?e.message:String(e))}finally{H(!1)}},Ce=async()=>{let t=await Z.validateFields();J(!0);try{let n=G.map(B),r=await x.testFunction({draft:L(t,e),args:n});X(R(r)),r.success?v.success(e(`functions.testPassed`,{output:r.output_text||`OK`})):v.error(r.error||e(`functions.testFailed`))}catch(e){v.error(e instanceof Error?e.message:String(e))}finally{J(!1)}},we=async()=>{if(W){J(!0);try{let t=G.map(B),n=await x.testFunction({function_id:W.id,args:t});X(R(n)),n.success?v.success(e(`functions.testPassed`,{output:n.output_text||`OK`})):v.error(n.error||e(`functions.testFailed`))}catch(e){v.error(e instanceof Error?e.message:String(e))}finally{J(!1)}}},Te=(0,w.useMemo)(()=>[{title:e(`common.name`),dataIndex:`name`,key:`name`,width:180,render:(e,t)=>(0,T.jsx)(h.Text,{strong:!0,children:t.name})},{title:e(`functions.callName`),dataIndex:`call_name`,key:`call_name`,width:220,render:(e,t)=>(0,T.jsx)(h.Text,{code:!0,children:t.call_name})},{title:e(`functions.runtime`),dataIndex:`runtime`,key:`runtime`,width:100,render:e=>(0,T.jsx)(d,{color:`blue`,children:e})},{title:e(`common.status`),dataIndex:`enabled`,key:`enabled`,width:90,render:t=>(0,T.jsx)(d,{color:t?`success`:`default`,icon:t?(0,T.jsx)(n,{}):(0,T.jsx)(ee,{}),children:e(t?`common.enabled`:`functions.paused`)})},{title:e(`functions.callStats`),key:`stats`,width:150,render:(t,n)=>(0,T.jsx)(h.Text,{type:`secondary`,children:e(`functions.callStatsValue`,{success:n.success_total,fail:n.fail_total})})},{title:e(`functions.updatedAt`),dataIndex:`updated_at_ms`,key:`updated_at_ms`,width:180,render:e=>me(e)},{title:e(`common.actions`),key:`actions`,width:210,fixed:`right`,render:(t,n)=>(0,T.jsxs)(p,{size:4,children:[(0,T.jsx)(r,{title:e(`functions.test`),children:(0,T.jsx)(y,{type:`text`,icon:(0,T.jsx)(b,{}),onClick:()=>{ye(n),K(z(n.runtime)),X(``),U(!0)}})}),(0,T.jsx)(r,{title:e(`common.edit`),children:(0,T.jsx)(y,{type:`text`,icon:(0,T.jsx)(ne,{}),disabled:!m,onClick:()=>xe(n)})}),m?(0,T.jsx)(r,{title:n.enabled?e(`functions.disable`):e(`functions.enable`),children:(0,T.jsx)(y,{type:`text`,icon:n.enabled?(0,T.jsx)(ce,{}):(0,T.jsx)(oe,{}),onClick:async()=>{try{n.enabled?await x.disableFunction(n.id):await x.enableFunction(n.id),await Q()}catch(e){v.error(e instanceof Error?e.message:String(e))}}})}):null,m?(0,T.jsx)(pe,{size:`small`,tooltipTitle:e(`common.remove`),onConfirm:async()=>{try{await x.deleteFunction(n.id),await Q()}catch(e){v.error(e instanceof Error?e.message:String(e))}}}):null]})}],[m,e]);return(0,T.jsxs)(de,{title:e(`functions.title`),icon:(0,T.jsx)(ae,{}),actions:(0,T.jsxs)(fe,{wrap:!0,children:[(0,T.jsx)(y,{icon:(0,T.jsx)(se,{}),onClick:()=>void Q(),loading:D,children:e(`common.refresh`)}),(0,T.jsx)(y,{type:`primary`,icon:(0,T.jsx)(c,{}),onClick:be,disabled:!m,children:e(`functions.add`)})]}),children:[(0,T.jsx)(i,{children:(0,T.jsx)(ie,{rowKey:`id`,loading:k,columns:Te,dataSource:S,locale:{emptyText:(0,T.jsx)(a,{image:a.PRESENTED_IMAGE_SIMPLE,description:e(`functions.noData`)})},pagination:C(e),scroll:{x:1100}})}),(0,T.jsxs)(u,{open:M,title:e(P?`functions.edit`:`functions.add`),width:980,onCancel:()=>N(!1),onOk:()=>void Se(),okButtonProps:{loading:V,disabled:!m},okText:e(`functions.save`),children:[(0,T.jsx)(t,{form:Z,layout:`vertical`,disabled:!m,onValuesChange:e=>{if(`runtime`in e){let t=A(e.runtime);Z.setFieldsValue({source_code:t.source_code,entrypoint:t.entrypoint,module_bytes:void 0,module_base64:``}),K(z(e.runtime))}},children:(0,T.jsxs)(f,{gutter:16,children:[(0,T.jsx)(s,{span:12,children:(0,T.jsx)(t.Item,{name:`name`,label:e(`common.name`),rules:[{required:!0,message:I(e(`common.name`),e)}],children:(0,T.jsx)(_,{placeholder:e(`functions.namePlaceholder`)})})}),(0,T.jsx)(s,{span:12,children:(0,T.jsx)(t.Item,{name:`runtime`,label:e(`functions.runtime`),rules:[{required:!0,message:I(e(`functions.runtime`),e)}],children:(0,T.jsx)(l,{options:[{label:`Lua`,value:`Lua`},{label:`Rhai`,value:`Rhai`},{label:`WASM`,value:`Wasm`}]})})}),(0,T.jsx)(s,{span:8,children:(0,T.jsx)(t.Item,{name:`timeout_ms`,label:e(`functions.timeoutMs`),children:(0,T.jsx)(o,{min:1,max:5e3,style:{width:`100%`}})})}),(0,T.jsx)(s,{span:8,children:(0,T.jsx)(t.Item,{name:`max_memory_bytes`,label:e(`functions.maxMemoryBytes`),children:(0,T.jsx)(o,{min:65536,style:{width:`100%`}})})}),(0,T.jsx)(s,{span:8,children:(0,T.jsx)(t.Item,{name:`max_output_bytes`,label:e(`functions.maxOutputBytes`),children:(0,T.jsx)(o,{min:1,max:4*1024*1024,style:{width:`100%`}})})}),(0,T.jsx)(s,{span:8,children:(0,T.jsx)(t.Item,{name:`enabled`,label:e(`common.enabled`),valuePropName:`checked`,children:(0,T.jsx)(re,{})})}),$===`Wasm`?(0,T.jsx)(s,{span:16,children:(0,T.jsx)(t.Item,{name:`entrypoint`,label:e(`functions.entrypoint`),children:(0,T.jsx)(_,{placeholder:`velamq_udf`})})}):null,$===`Wasm`?(0,T.jsxs)(s,{span:24,children:[(0,T.jsx)(te,{type:`info`,showIcon:!0,message:e(`functions.wasmAbiHint`),style:{marginBottom:12}}),(0,T.jsx)(t.Item,{name:`module_base64`,label:e(`functions.wasmModuleBase64`),rules:[{required:!0,message:I(e(`functions.wasmModuleBase64`),e)},{validator:async(t,n)=>{try{j(n)}catch{throw Error(e(`functions.invalidBase64`))}}}],children:(0,T.jsx)(_.TextArea,{rows:8,spellCheck:!1,placeholder:e(`functions.wasmBase64Placeholder`)})})]}):(0,T.jsx)(s,{span:24,children:(0,T.jsx)(t.Item,{name:`source_code`,label:e($===`Rhai`?`functions.rhaiScript`:`functions.luaScript`),rules:[{required:!0,message:I(e($===`Rhai`?`functions.rhaiScript`:`functions.luaScript`),e)}],children:(0,T.jsx)(_.TextArea,{rows:10,spellCheck:!1})})})]})}),(0,T.jsxs)(i,{size:`small`,title:e(`functions.testFunction`),style:{marginTop:12},children:[(0,T.jsxs)(f,{gutter:12,children:[(0,T.jsx)(s,{span:12,children:(0,T.jsxs)(p,{direction:`vertical`,style:{width:`100%`},children:[G.map((e,t)=>(0,T.jsxs)(p,{direction:`vertical`,size:4,style:{width:`100%`},children:[(0,T.jsx)(h.Text,{type:`secondary`,children:`arg${t+1}`}),(0,T.jsx)(_.TextArea,{rows:3,value:e,spellCheck:!1,onChange:e=>{let n=[...G];n[t]=e.target.value,K(n)}})]},t)),(0,T.jsxs)(p,{children:[(0,T.jsx)(y,{icon:(0,T.jsx)(c,{}),onClick:()=>K(e=>[...e,`null`]),children:e(`functions.addArg`)}),(0,T.jsx)(y,{icon:(0,T.jsx)(g,{}),disabled:G.length===0,onClick:()=>K(e=>e.slice(0,-1)),children:e(`functions.removeLastArg`)})]})]})}),(0,T.jsx)(s,{span:12,children:(0,T.jsx)(_.TextArea,{rows:8,value:Y,readOnly:!0,spellCheck:!1})})]}),(0,T.jsx)(y,{style:{marginTop:12},icon:(0,T.jsx)(b,{}),loading:q,onClick:()=>void Ce(),children:e(`functions.testDraft`)})]})]}),(0,T.jsx)(u,{open:ve,title:W?e(`functions.testFunctionWithName`,{name:W.call_name}):e(`functions.testFunction`),width:860,onCancel:()=>U(!1),onOk:()=>void we(),okText:e(`functions.runTest`),okButtonProps:{loading:q},children:(0,T.jsxs)(f,{gutter:12,children:[(0,T.jsx)(s,{span:12,children:(0,T.jsxs)(p,{direction:`vertical`,style:{width:`100%`},children:[G.map((e,t)=>(0,T.jsxs)(p,{direction:`vertical`,size:4,style:{width:`100%`},children:[(0,T.jsx)(h.Text,{type:`secondary`,children:`arg${t+1}`}),(0,T.jsx)(_.TextArea,{rows:3,value:e,spellCheck:!1,onChange:e=>{let n=[...G];n[t]=e.target.value,K(n)}})]},t)),(0,T.jsxs)(p,{children:[(0,T.jsx)(y,{icon:(0,T.jsx)(c,{}),onClick:()=>K(e=>[...e,`null`]),children:e(`functions.addArg`)}),(0,T.jsx)(y,{icon:(0,T.jsx)(g,{}),disabled:G.length===0,onClick:()=>K(e=>e.slice(0,-1)),children:e(`functions.removeLastArg`)})]})]})}),(0,T.jsx)(s,{span:12,children:(0,T.jsx)(_.TextArea,{rows:12,value:Y,readOnly:!0,spellCheck:!1})})]})})]})}export{V as default};