import{loadStEngine as d,createConnectionDefaultsJson as l,connectionSummaryJson as p}from"./index-Dqa23PTc.js";import"./plchelper_wasm-DS8UhsA2.js";const f="plchelper",y=1,a="connections";function N(){return new Promise((n,t)=>{const e=indexedDB.open(f,y);e.onupgradeneeded=()=>{const o=e.result;o.objectStoreNames.contains(a)||o.createObjectStore(a,{keyPath:"id"})},e.onsuccess=()=>n(e.result),e.onerror=()=>t(e.error??new Error("IndexedDB open failed"))})}let s=null;function r(){return s||(s=N()),s}function c(n){return new Promise((t,e)=>{n.onsuccess=()=>t(n.result),n.onerror=()=>e(n.error??new Error("IndexedDB request failed"))})}async function u(n){const e=(await r()).transaction(a,"readonly");return c(e.objectStore(a).get(n))}async function i(n){const e=(await r()).transaction(a,"readwrite");await c(e.objectStore(a).put(n))}function w(n){return JSON.parse(p(JSON.stringify(n)))}async function A(){await d();const t=(await r()).transaction(a,"readonly");return(await c(t.objectStore(a).getAll())).map(w).sort((o,m)=>m.updatedAt-o.updatedAt)}async function S(n){const t=await u(n);if(!t)throw new Error(`connection ${n} not found`);return t}async function x(n){await d();const t=JSON.parse(l(n.ioName,n.commType)),e=Date.now(),o={id:crypto.randomUUID(),name:n.name,ioName:n.ioName,commType:t.commType,connection:t.connection,pollPeriodMs:t.pollPeriodMs,settings:t.settings,variables:[],signalGen:null,createdAt:e,updatedAt:e};return await i(o),o}async function C(n){const t=await u(n.id);if(!t)throw new Error(`connection ${n.id} not found`);if(t.ioName!==n.ioName)throw new Error(`io type change: was "${t.ioName}", attempted "${n.ioName}"`);await i({...n,updatedAt:Date.now()})}async function E(n){const e=(await r()).transaction(a,"readwrite");await c(e.objectStore(a).delete(n))}async function P(n,t){await d();const o={...await S(n),name:t,updatedAt:Date.now()};return await i(o),w(o)}function D(n){const t=JSON.parse(n),e=Date.now();return{...t,id:crypto.randomUUID(),createdAt:e,updatedAt:e}}async function J(n,t){const e=D(n);return t&&t.trim()&&(e.name=t.trim()),await i(e),e}export{x as createConnection,E as deleteConnection,S as getConnection,J as importConnectionFromJson,A as listConnections,D as parseConnectionPreview,P as renameConnection,C as saveConnection};
