"use strict";var y=Object.create;var v=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var C=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty;var O=(o,t)=>{for(var e in t)v(o,e,{get:t[e],enumerable:!0})},f=(o,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of k(t))!A.call(o,n)&&n!==e&&v(o,n,{get:()=>t[n],enumerable:!(s=L(t,n))||s.enumerable});return o};var B=(o,t,e)=>(e=o!=null?y(C(o)):{},f(t||!o||!o.__esModule?v(e,"default",{value:o,enumerable:!0}):e,o)),I=o=>f(v({},"__esModule",{value:!0}),o);var S={};O(S,{CR:()=>g,FS:()=>E,Hl7Base:()=>a,Hl7Client:()=>m,Hl7CloseEvent:()=>l,Hl7Connection:()=>p,Hl7ErrorEvent:()=>i,Hl7MessageEvent:()=>d,Hl7Server:()=>u,VT:()=>P});module.exports=I(S);var a=class extends EventTarget{addEventListener(t,e,s){super.addEventListener(t,e,s)}removeEventListener(t,e,s){super.removeEventListener(t,e,s)}};var x=require("node:net");var H=require("@medplum/core"),h=require("iconv-lite");var P=11,g=13,E=28;var d=class extends Event{constructor(e,s){super("message");this.connection=e;this.message=s}},i=class extends Event{constructor(e){super("error");this.error=e}},l=class extends Event{constructor(){super("close")}};var p=class extends a{constructor(e,s="utf-8"){super();this.socket=e;this.encoding=s;this.chunks=[];this.messageQueue=[];e.on("data",n=>{try{if(this.appendData(n),n.at(-2)===28&&n.at(-1)===13){let r=Buffer.concat(this.chunks),c=r.subarray(1,r.length-2),w=(0,h.decode)(c,this.encoding),b=H.Hl7Message.parse(w);this.dispatchEvent(new d(this,b)),this.resetBuffer()}}catch(r){this.dispatchEvent(new i(r))}}),e.on("error",n=>{this.resetBuffer(),this.dispatchEvent(new i(n))}),e.on("end",()=>{this.close()}),this.addEventListener("message",n=>{let r=this.messageQueue.shift();if(!r){this.dispatchEvent(new i(new Error(`Received a message when no pending messages were in the queue. Message: ${n.message}`)));return}r.resolve?.(n.message)})}sendImpl(e,s){this.messageQueue.push(s);let n=e.toString(),r=(0,h.encode)(n,this.encoding),c=Buffer.alloc(r.length+3);c.writeInt8(11,0),r.copy(c,1),c.writeInt8(28,r.length+1),c.writeInt8(13,r.length+2),this.socket.write(c)}send(e){this.sendImpl(e,{message:e})}async sendAndWait(e){return new Promise((s,n)=>{let r={message:e,resolve:s,reject:n};this.sendImpl(e,r)})}close(){this.socket.end(),this.socket.destroy(),this.dispatchEvent(new l)}appendData(e){this.chunks.push(e)}resetBuffer(){this.chunks=[]}};var m=class extends a{constructor(t){super(),this.options=t,this.host=this.options.host,this.port=this.options.port,this.encoding=this.options.encoding,this.keepAlive=this.options.keepAlive??!1}connect(){return this.connection?Promise.resolve(this.connection):new Promise((t,e)=>{let s=(0,x.connect)({host:this.host,port:this.port,keepAlive:this.keepAlive},()=>{let n;this.connection=n=new p(s,this.encoding),s.off("error",e),n.addEventListener("close",()=>this.dispatchEvent(new l)),n.addEventListener("error",r=>this.dispatchEvent(new i(r.error))),t(this.connection)});s.on("error",e)})}async send(t){return(await this.connect()).send(t)}async sendAndWait(t){return(await this.connect()).sendAndWait(t)}close(){this.connection&&(this.connection.close(),delete this.connection)}};var M=B(require("node:net"));var u=class{constructor(t){this.handler=t}start(t,e){let s=M.default.createServer(n=>{let r=new p(n,e);this.handler(r)});s.listen(t),this.server=s}async stop(){return new Promise((t,e)=>{if(!this.server){e(new Error("Stop was called but there is no server running"));return}this.server.close(s=>{if(s){e(s);return}t()}),this.server=void 0})}};0&&(module.exports={CR,FS,Hl7Base,Hl7Client,Hl7CloseEvent,Hl7Connection,Hl7ErrorEvent,Hl7MessageEvent,Hl7Server,VT}); //# sourceMappingURL=index.cjs.map