@munchi_oy/react-native-epson-printer 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,6 +30,25 @@ This package provides:
30
30
 
31
31
  This package does not include Star or Sunmi drivers.
32
32
 
33
+ ## Runtime Contract
34
+
35
+ For both iOS and Android, the SDK aims to provide this behavior:
36
+
37
+ - Native printer work may take time, especially for Bluetooth connection, status checks, and image printing.
38
+ - Slow printer operations are acceptable; visible app hangs are not.
39
+ - Printer work must not block the app UI/main thread while running.
40
+ - Errors from native printer operations must reject back to JS and must not be swallowed.
41
+ - Image and logo printing may be more expensive than text-only receipts, but must still remain asynchronous from the app's point of view.
42
+
43
+ ## Observed Performance Expectations
44
+
45
+ These are real-device expectations, not strict guarantees:
46
+
47
+ - iOS: around 3s end-to-end can be acceptable if the app UI remains responsive.
48
+ - Android: the first print may take around 5-6s, with later warm prints often dropping to around 2-3s.
49
+ - Actual timing depends on connection state, Bluetooth behavior, printer readiness, and whether the receipt includes images.
50
+ - Responsiveness and correct JS error propagation matter more than raw duration.
51
+
33
52
  ## Behavior Support (implemented)
34
53
 
35
54
  | Behavior | Status | Details |
@@ -41,6 +60,7 @@ This package does not include Star or Sunmi drivers.
41
60
  | Retry for transient busy/timeout states | Implemented | Native operations retry for retryable states (`BUSY`, `IN_USE`, timeout-family errors). |
42
61
  | Print receive timeout guard | Implemented | Native print call fails with `PRINT_TIMEOUT` if printer callback does not return within 30s. |
43
62
  | iOS stale-session recovery | Implemented | One-shot session recovery + reconnect + retry for stale/busy/offline connection scenarios. |
63
+ | Non-blocking native printer execution | Implemented | Native connect / print / status work is executed off the app UI thread on both iOS and Android. |
44
64
  | Post-print auto-disconnect | Implemented | JS driver schedules disconnect after 5s idle after print completion. |
45
65
  | Android warm reconnect cache | Implemented | Native Android keeps a short-lived connection cache (up to 120s) for faster reconnect. |
46
66
  | Discovery cleanup/filtering | Implemented | Duplicate targets are deduped and Epson sub-devices such as `[local_display]` are filtered out. |
@@ -194,6 +214,13 @@ await printer.printEmbedded({
194
214
  });
195
215
  ```
196
216
 
217
+ ### Image printing expectations
218
+
219
+ - Image printing is supported.
220
+ - Image printing can take longer than text-only receipts because the image may need decode, resize, rasterization, and transport work.
221
+ - This extra work should not block the app UI.
222
+ - If image printing fails, the error should still reject back to JS like any other print failure.
223
+
197
224
  ### Cash drawer
198
225
 
199
226
  ```ts
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var he=Object.create;var D=Object.defineProperty;var Ee=Object.getOwnPropertyDescriptor;var Ce=Object.getOwnPropertyNames;var fe=Object.getPrototypeOf,Re=Object.prototype.hasOwnProperty;var pe=(t,e)=>{for(var r in e)D(t,r,{get:e[r],enumerable:!0})},J=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of Ce(e))!Re.call(t,i)&&i!==r&&D(t,i,{get:()=>e[i],enumerable:!(n=Ee(e,i))||n.enumerable});return t};var Ne=(t,e,r)=>(r=t!=null?he(fe(t)):{},J(e||!t||!t.__esModule?D(r,"default",{value:t,enumerable:!0}):r,t)),Pe=t=>J(D({},"__esModule",{value:!0}),t);var Be={};pe(Be,{Epos2CallbackCode:()=>te,Epos2ConnectionEvent:()=>Z,Epos2ErrorStatus:()=>ee,Epos2Font:()=>re,Epos2Lang:()=>V,Epos2StatusEvent:()=>H,EpsonModel:()=>q,FontSize:()=>b,MunchiPrinterError:()=>h,PrinterError:()=>p,PrinterErrorCode:()=>y,PrinterProvider:()=>Ue,PrinterTranslationCode:()=>v,VERSION:()=>Te,discoverPrinters:()=>ce,getGlobalLogger:()=>Y,getPrinter:()=>K,resolveEpsonError:()=>Le,resolveModelFromBluetoothName:()=>le,resolvePrinterError:()=>de,setGlobalLogger:()=>B,usePrinter:()=>k,usePrinterStatus:()=>Fe});module.exports=Pe(Be);var j={},B=t=>{j.logger=t},Y=()=>j.logger;var W=require("react-native");var y=(T=>(T.CONNECTION_FAILED="CONNECTION_FAILED",T.PRINT_FAILED="PRINT_FAILED",T.DISCOVERY_FAILED="DISCOVERY_FAILED",T.TIMEOUT="TIMEOUT",T.NATIVE_MODULE_MISSING="NATIVE_MODULE_MISSING",T.UNKNOWN="UNKNOWN",T))(y||{}),v=(c=>(c.ERR_PRINTER_RECONNECT_REQUIRED="printer.error.reconnect_required",c.ERR_PRINTER_OFFLINE="printer.error.offline",c.ERR_PRINTER_BUSY="printer.error.busy",c.ERR_PRINTER_COVER_OPEN="printer.error.cover_open",c.ERR_PRINTER_PAPER_EMPTY="printer.error.paper_empty",c.ERR_CONNECTION_TIMEOUT="printer.error.connection_timeout",c.ERR_UNKNOWN="printer.error.unknown",c))(v||{}),h=class t extends Error{constructor(r,n,i,o){super(i);this.code=r;this.translationCode=n;this.originalError=o;this.name="MunchiPrinterError",Object.setPrototypeOf(this,t.prototype)}};var Ie=t=>{if(!t||typeof t!="object")return"";let e=t.code;return typeof e=="string"?e:""},Se=t=>{if(t instanceof Error)return t.message;if(t&&typeof t=="object"){let e=t.message;if(typeof e=="string")return e}return String(t)},g=(t,e,r)=>t.includes(r)||e===r||e.includes(r),f=t=>{if(t instanceof h)return t;let e=Se(t),r=Ie(t),n=e.toUpperCase(),i=r.toUpperCase();return n.includes("ERROR CODE: 4")?new h("PRINT_FAILED","printer.error.cover_open","Printer cover is open",t):n.includes("ERROR CODE: 3")?new h("PRINT_FAILED","printer.error.busy","Printer is performing auto-recovery",t):n.includes("ERROR CODE: 7")?new h("PRINT_FAILED","printer.error.paper_empty","Printer paper is empty",t):g(n,i,"TARGET_IN_USE")||g(n,i,"BUSY")||g(n,i,"IN_USE")||g(n,i,"PRINT_BUSY")||g(n,i,"DISCOVERY_BUSY")?new h("TIMEOUT","printer.error.busy","Printer is busy",t):g(n,i,"TIMEOUT")||g(n,i,"ERR_TIMEOUT")||g(n,i,"PRINT_TIMEOUT")?new h("TIMEOUT","printer.error.connection_timeout","Operation timed out",t):g(n,i,"PRINTER_RECONNECT_REQUIRED")||n.includes("RECONNECT THE PRINTER")?new h("CONNECTION_FAILED","printer.error.reconnect_required",e,t):g(n,i,"PERMISSION_DENIED")||g(n,i,"LOCATION_DISABLED")||g(n,i,"BLUETOOTH_DISABLED")?new h("CONNECTION_FAILED","printer.error.offline",e,t):g(n,i,"DISCOVERY_ERROR")||n.includes("DISCOVERY")?new h("DISCOVERY_FAILED","printer.error.unknown",e,t):g(n,i,"PRINTER_OFFLINE")||g(n,i,"SESSION_ERROR")||g(n,i,"STATUS_ERROR")||g(n,i,"INIT_ERROR")||g(n,i,"PRINT_CANCELED")||g(n,i,"DISCONNECT")||g(n,i,"ERR_DISCONNECT")||n.includes("PRINTER NOT CONNECTED")||n.includes("OFFLINE")?new h("CONNECTION_FAILED","printer.error.offline",n.includes("OFFLINE")?e:"Printer disconnected",t):g(n,i,"CONNECT_ERROR")||n.includes("FAILED TO CONNECT")||g(n,i,"ERR_CONNECT")?new h("CONNECTION_FAILED","printer.error.offline","Failed to connect to printer",t):g(n,i,"PRINT_ERROR")||g(n,i,"PRINT_FAILURE")?new h("PRINT_FAILED","printer.error.unknown",e,t):new h("UNKNOWN","printer.error.unknown",e,t)};var H=(a=>(a[a.ONLINE=0]="ONLINE",a[a.OFFLINE=1]="OFFLINE",a[a.POWER_OFF=2]="POWER_OFF",a[a.COVER_CLOSE=3]="COVER_CLOSE",a[a.COVER_OPEN=4]="COVER_OPEN",a[a.PAPER_OK=5]="PAPER_OK",a[a.PAPER_NEAR_END=6]="PAPER_NEAR_END",a[a.PAPER_EMPTY=7]="PAPER_EMPTY",a[a.DRAWER_HIGH=8]="DRAWER_HIGH",a[a.DRAWER_LOW=9]="DRAWER_LOW",a[a.BATTERY_ENOUGH=10]="BATTERY_ENOUGH",a[a.BATTERY_EMPTY=11]="BATTERY_EMPTY",a[a.AUTO_RECOVER_ERROR=20]="AUTO_RECOVER_ERROR",a[a.AUTO_RECOVER_OK=21]="AUTO_RECOVER_OK",a[a.UNRECOVERABLE_ERROR=22]="UNRECOVERABLE_ERROR",a))(H||{}),Z=(n=>(n[n.RECONNECTING=0]="RECONNECTING",n[n.RECONNECT=1]="RECONNECT",n[n.DISCONNECT=2]="DISCONNECT",n))(Z||{}),ee=(u=>(u[u.SUCCESS=0]="SUCCESS",u[u.ERR_PARAM=1]="ERR_PARAM",u[u.ERR_CONNECT=2]="ERR_CONNECT",u[u.ERR_TIMEOUT=3]="ERR_TIMEOUT",u[u.ERR_MEMORY=4]="ERR_MEMORY",u[u.ERR_ILLEGAL=5]="ERR_ILLEGAL",u[u.ERR_PROCESSING=6]="ERR_PROCESSING",u[u.ERR_NOT_FOUND=7]="ERR_NOT_FOUND",u[u.ERR_IN_USE=8]="ERR_IN_USE",u[u.ERR_TYPE_INVALID=9]="ERR_TYPE_INVALID",u[u.ERR_DISCONNECT=10]="ERR_DISCONNECT",u[u.ERR_ALREADY_OPENED=11]="ERR_ALREADY_OPENED",u[u.ERR_ALREADY_USED=12]="ERR_ALREADY_USED",u[u.ERR_BOX_COUNT_OVER=13]="ERR_BOX_COUNT_OVER",u[u.ERR_BOX_CLIENT_OVER=14]="ERR_BOX_CLIENT_OVER",u[u.ERR_UNSUPPORTED=15]="ERR_UNSUPPORTED",u[u.ERR_DEVICE_BUSY=16]="ERR_DEVICE_BUSY",u[u.ERR_RECOVERY_FAILURE=17]="ERR_RECOVERY_FAILURE",u[u.ERR_FAILURE=255]="ERR_FAILURE",u))(ee||{}),te=(d=>(d[d.SUCCESS=0]="SUCCESS",d[d.ERR_TIMEOUT=1]="ERR_TIMEOUT",d[d.ERR_NOT_FOUND=2]="ERR_NOT_FOUND",d[d.ERR_AUTORECOVER=3]="ERR_AUTORECOVER",d[d.ERR_COVER_OPEN=4]="ERR_COVER_OPEN",d[d.ERR_CUTTER=5]="ERR_CUTTER",d[d.ERR_MECHANICAL=6]="ERR_MECHANICAL",d[d.ERR_EMPTY=7]="ERR_EMPTY",d[d.ERR_UNRECOVERABLE=8]="ERR_UNRECOVERABLE",d[d.ERR_SYSTEM=9]="ERR_SYSTEM",d[d.ERR_PORT=10]="ERR_PORT",d[d.ERR_FAILURE=255]="ERR_FAILURE",d))(te||{}),V=(l=>(l[l.EN=0]="EN",l[l.JA=1]="JA",l[l.ZH_CN=2]="ZH_CN",l[l.ZH_TW=3]="ZH_TW",l[l.KO=4]="KO",l[l.TH=5]="TH",l[l.VI=6]="VI",l[l.MULTI=7]="MULTI",l))(V||{}),re=(o=>(o[o.FONT_A=0]="FONT_A",o[o.FONT_B=1]="FONT_B",o[o.FONT_C=2]="FONT_C",o[o.FONT_D=3]="FONT_D",o[o.FONT_E=4]="FONT_E",o))(re||{}),q=(s=>(s[s.TM_M10=0]="TM_M10",s[s.TM_M30=1]="TM_M30",s[s.TM_P20=2]="TM_P20",s[s.TM_P60=3]="TM_P60",s[s.TM_P60II=4]="TM_P60II",s[s.TM_P80=5]="TM_P80",s[s.TM_T20=6]="TM_T20",s[s.TM_T60=7]="TM_T60",s[s.TM_T70=8]="TM_T70",s[s.TM_T81=9]="TM_T81",s[s.TM_T82=10]="TM_T82",s[s.TM_T83=11]="TM_T83",s[s.TM_T88=12]="TM_T88",s[s.TM_T90=13]="TM_T90",s[s.TM_T90KP=14]="TM_T90KP",s[s.TM_U220=15]="TM_U220",s[s.TM_U330=16]="TM_U330",s[s.TM_L90=17]="TM_L90",s[s.TM_H6000=18]="TM_H6000",s[s.TM_T83III=19]="TM_T83III",s[s.TM_T100=20]="TM_T100",s[s.TM_M30II=21]="TM_M30II",s[s.TM_M50=23]="TM_M50",s[s.TM_T88VII=24]="TM_T88VII",s[s.TM_L90LFC=25]="TM_L90LFC",s[s.TM_L100=26]="TM_L100",s[s.TM_P20II=27]="TM_P20II",s[s.TM_P80II=28]="TM_P80II",s[s.TM_M30III=29]="TM_M30III",s[s.TM_M50II=30]="TM_M50II",s[s.TM_M55=31]="TM_M55",s[s.TM_U220II=32]="TM_U220II",s))(q||{});var p=class t extends h{constructor(r,n,i,o,T){super(n,i,o,T);this.isHardwareError=r;this.name="PrinterError",Object.setPrototypeOf(this,t.prototype)}},z=t=>{if(t instanceof p)return t.isHardwareError;let e=String(t.message||t).toUpperCase();return e.includes("ERR_OFF_LINE")||e.includes("ERR_COVER_OPEN")||e.includes("ERR_PAPER_OUT")||e.includes("ERR_FAILURE")||e.includes("DISCONNECT")||e.includes("PRINT_TIMEOUT")||e.includes("ERROR CODE: 4")||e.includes("ERROR CODE: 7")||e.includes("ERROR CODE: 3")},ne=t=>{if(t instanceof p)return t;let e=f(t),r=z(t);return new p(r,e.code,e.translationCode,e.message,e.originalError)};var M=require("react-native"),m=M.NativeModules.MunchiEpsonModule,G=()=>m?new M.NativeEventEmitter(m):null,_e=t=>new Promise(e=>setTimeout(e,t)),Oe=t=>{if(!t||typeof t!="object")return"";let e=t.code;return typeof e=="string"?e.toUpperCase():""},ye=t=>{if(t instanceof Error)return t.message.toUpperCase();if(t&&typeof t=="object"){let e=t.message;if(typeof e=="string")return e.toUpperCase()}return String(t).toUpperCase()},N=(t,e,r)=>t.includes(r)||e===r||e.includes(r),S=async(t,e=3,r=500)=>{try{return await t()}catch(n){if(e<=1)throw n;let i=ye(n),o=Oe(n);if(N(i,o,"TARGET_IN_USE"))throw n;if(N(i,o,"BUSY")||N(i,o,"IN_USE")||N(i,o,"ERR_IN_USE")||N(i,o,"PRINT_BUSY")||N(i,o,"ERR_CONNECT")||N(i,o,"CONNECT_ERROR")||N(i,o,"ERR_TIMEOUT")||N(i,o,"PRINT_TIMEOUT"))return await _e(r),S(t,e-1,r);throw n}};var ve=new Set([0,3,5,21]),w=class{constructor(e){this.config=e}statusSubscription=null;connectionSubscription=null;connectionCallbacks=new Set;emitStatus(e){for(let r of this.connectionCallbacks)r(e)}ensureStatusListener(){if(this.statusSubscription)return;let e=G();e&&(this.config.logger?.info?.("[EpsonPrinter] Starting status listener for queue management"),this.statusSubscription=e.addListener("onPrinterStatusChange",r=>{if(!this.config.getSessionId()||r.sessionId!==this.config.getSessionId())return;let n=`[EpsonPrinter] Received Status Event: ${r.eventType}`;this.config.logger?.info?.(n),this.config.onEvent({connectionStatus:null,statusEventType:r.eventType,isRecoveryEvent:ve.has(r.eventType)})}))}ensureConnectionListener(){if(this.connectionSubscription)return;let e=G();e&&(this.connectionSubscription=e.addListener("onPrinterConnectionChange",r=>{if(!this.config.getSessionId()||r.sessionId!==this.config.getSessionId())return;let n="UNKNOWN";switch(r.status){case"RECONNECTING":n="RECONNECTING";break;case"RECONNECTED":n="CONNECTED";break;case"DISCONNECTED":n="DISCONNECTED";break;default:n="UNKNOWN"}this.config.onEvent({connectionStatus:n,statusEventType:null,isRecoveryEvent:!1}),this.emitStatus(n)}))}removeStatusListener(){this.statusSubscription&&(this.statusSubscription.remove(),this.statusSubscription=null)}onConnectionChange(e){return this.connectionCallbacks.add(e),e(this.config.getConnectionStatus()),this.ensureConnectionListener(),()=>{this.connectionCallbacks.delete(e),this.connectionCallbacks.size===0&&this.connectionSubscription&&(this.connectionSubscription.remove(),this.connectionSubscription=null)}}};var b=(n=>(n.Small="Small",n.Medium="Medium",n.Large="Large",n))(b||{});var De=(t,e=!1)=>{let r={Small:{normal:{width:1,height:1},item:{width:1,height:1}},Medium:{normal:{width:2,height:2},item:{width:2,height:2}},Large:{normal:{width:2,height:2},item:{width:3,height:3}}},n=e?"item":"normal";return r[t]?.[n]??r.Small.normal},ie=t=>{if(typeof t!="object"||t===null)return null;let e=t;return{target:String(e.target||""),name:String(e.name||""),macAddress:e.macAddress?String(e.macAddress):void 0,ipAddress:e.ipAddress?String(e.ipAddress):void 0,bdAddress:e.bdAddress?String(e.bdAddress):void 0,type:"EPSON"}},se=t=>t.commands.map(e=>{switch(e.type){case"text":return{cmd:"addText",data:e.text,align:e.align,bold:e.bold,underline:e.underline,size:e.size};case"align":return{cmd:"addTextAlign",align:e.align};case"bold":return{cmd:"addTextStyle",bold:e.value};case"fontSize":{let r=De(e.size,e.isItemName);return{cmd:"addTextSize",width:r.width,height:r.height}}case"newline":return{cmd:"addFeedLine",line:e.lines||1};case"separator":return{cmd:"addText",data:`------------------
1
+ "use strict";var he=Object.create;var D=Object.defineProperty;var Ee=Object.getOwnPropertyDescriptor;var Ce=Object.getOwnPropertyNames;var Re=Object.getPrototypeOf,fe=Object.prototype.hasOwnProperty;var Ne=(t,e)=>{for(var r in e)D(t,r,{get:e[r],enumerable:!0})},J=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of Ce(e))!fe.call(t,i)&&i!==r&&D(t,i,{get:()=>e[i],enumerable:!(n=Ee(e,i))||n.enumerable});return t};var pe=(t,e,r)=>(r=t!=null?he(Re(t)):{},J(e||!t||!t.__esModule?D(r,"default",{value:t,enumerable:!0}):r,t)),Pe=t=>J(D({},"__esModule",{value:!0}),t);var Be={};Ne(Be,{Epos2CallbackCode:()=>te,Epos2ConnectionEvent:()=>Z,Epos2ErrorStatus:()=>ee,Epos2Font:()=>re,Epos2Lang:()=>V,Epos2StatusEvent:()=>H,EpsonModel:()=>G,FontSize:()=>b,MunchiPrinterError:()=>h,PrinterError:()=>N,PrinterErrorCode:()=>y,PrinterProvider:()=>Ue,PrinterTranslationCode:()=>v,VERSION:()=>Te,discoverPrinters:()=>ce,getGlobalLogger:()=>Y,getPrinter:()=>K,resolveEpsonError:()=>Le,resolveModelFromBluetoothName:()=>le,resolvePrinterError:()=>de,setGlobalLogger:()=>B,usePrinter:()=>k,usePrinterStatus:()=>Fe});module.exports=Pe(Be);var j={},B=t=>{j.logger=t},Y=()=>j.logger;var W=require("react-native");var y=(T=>(T.CONNECTION_FAILED="CONNECTION_FAILED",T.PRINT_FAILED="PRINT_FAILED",T.DISCOVERY_FAILED="DISCOVERY_FAILED",T.TIMEOUT="TIMEOUT",T.NATIVE_MODULE_MISSING="NATIVE_MODULE_MISSING",T.UNKNOWN="UNKNOWN",T))(y||{}),v=(c=>(c.ERR_PRINTER_RECONNECT_REQUIRED="printer.error.reconnect_required",c.ERR_PRINTER_OFFLINE="printer.error.offline",c.ERR_PRINTER_BUSY="printer.error.busy",c.ERR_PRINTER_COVER_OPEN="printer.error.cover_open",c.ERR_PRINTER_PAPER_EMPTY="printer.error.paper_empty",c.ERR_CONNECTION_TIMEOUT="printer.error.connection_timeout",c.ERR_UNKNOWN="printer.error.unknown",c))(v||{}),h=class t extends Error{constructor(r,n,i,o){super(i);this.code=r;this.translationCode=n;this.originalError=o;this.name="MunchiPrinterError",Object.setPrototypeOf(this,t.prototype)}};var Ie=t=>{if(!t||typeof t!="object")return"";let e=t.code;return typeof e=="string"?e:""},Se=t=>{if(t instanceof Error)return t.message;if(t&&typeof t=="object"){let e=t.message;if(typeof e=="string")return e}return String(t)},d=(t,e,r)=>t.includes(r)||e===r||e.includes(r),R=t=>{if(t instanceof h)return t;let e=Se(t),r=Ie(t),n=e.toUpperCase(),i=r.toUpperCase();return n.includes("ERROR CODE: 4")?new h("PRINT_FAILED","printer.error.cover_open","Printer cover is open",t):n.includes("ERROR CODE: 3")?new h("PRINT_FAILED","printer.error.busy","Printer is performing auto-recovery",t):n.includes("ERROR CODE: 7")?new h("PRINT_FAILED","printer.error.paper_empty","Printer paper is empty",t):d(n,i,"TARGET_IN_USE")||d(n,i,"BUSY")||d(n,i,"DEVICE_BUSY")||d(n,i,"IN_USE")||d(n,i,"ERR_IN_USE")||d(n,i,"PROCESSING")||d(n,i,"ERR_PROCESSING")||d(n,i,"PRINT_BUSY")||d(n,i,"PRINTING")||d(n,i,"DISCOVERY_BUSY")?new h("TIMEOUT","printer.error.busy","Printer is busy",t):d(n,i,"TIMEOUT")||d(n,i,"ERR_TIMEOUT")||d(n,i,"PRINT_TIMEOUT")?new h("TIMEOUT","printer.error.connection_timeout","Operation timed out",t):d(n,i,"PRINTER_RECONNECT_REQUIRED")||n.includes("RECONNECT THE PRINTER")?new h("CONNECTION_FAILED","printer.error.reconnect_required",e,t):d(n,i,"PERMISSION_DENIED")||d(n,i,"LOCATION_DISABLED")||d(n,i,"BLUETOOTH_DISABLED")?new h("CONNECTION_FAILED","printer.error.offline",e,t):d(n,i,"DISCOVERY_ERROR")||n.includes("DISCOVERY")?new h("DISCOVERY_FAILED","printer.error.unknown",e,t):d(n,i,"PRINTER_OFFLINE")||d(n,i,"SESSION_ERROR")||d(n,i,"STATUS_ERROR")||d(n,i,"INIT_ERROR")||d(n,i,"PRINT_CANCELED")||d(n,i,"DISCONNECT")||d(n,i,"ERR_DISCONNECT")||n.includes("PRINTER NOT CONNECTED")||n.includes("OFFLINE")?new h("CONNECTION_FAILED","printer.error.offline",n.includes("OFFLINE")?e:"Printer disconnected",t):d(n,i,"CONNECT_ERROR")||n.includes("FAILED TO CONNECT")||d(n,i,"ERR_CONNECT")?new h("CONNECTION_FAILED","printer.error.offline","Failed to connect to printer",t):d(n,i,"PRINT_ERROR")||d(n,i,"PRINT_FAILURE")?new h("PRINT_FAILED","printer.error.unknown",e,t):new h("UNKNOWN","printer.error.unknown",e,t)};var H=(a=>(a[a.ONLINE=0]="ONLINE",a[a.OFFLINE=1]="OFFLINE",a[a.POWER_OFF=2]="POWER_OFF",a[a.COVER_CLOSE=3]="COVER_CLOSE",a[a.COVER_OPEN=4]="COVER_OPEN",a[a.PAPER_OK=5]="PAPER_OK",a[a.PAPER_NEAR_END=6]="PAPER_NEAR_END",a[a.PAPER_EMPTY=7]="PAPER_EMPTY",a[a.DRAWER_HIGH=8]="DRAWER_HIGH",a[a.DRAWER_LOW=9]="DRAWER_LOW",a[a.BATTERY_ENOUGH=10]="BATTERY_ENOUGH",a[a.BATTERY_EMPTY=11]="BATTERY_EMPTY",a[a.AUTO_RECOVER_ERROR=20]="AUTO_RECOVER_ERROR",a[a.AUTO_RECOVER_OK=21]="AUTO_RECOVER_OK",a[a.UNRECOVERABLE_ERROR=22]="UNRECOVERABLE_ERROR",a))(H||{}),Z=(n=>(n[n.RECONNECTING=0]="RECONNECTING",n[n.RECONNECT=1]="RECONNECT",n[n.DISCONNECT=2]="DISCONNECT",n))(Z||{}),ee=(u=>(u[u.SUCCESS=0]="SUCCESS",u[u.ERR_PARAM=1]="ERR_PARAM",u[u.ERR_CONNECT=2]="ERR_CONNECT",u[u.ERR_TIMEOUT=3]="ERR_TIMEOUT",u[u.ERR_MEMORY=4]="ERR_MEMORY",u[u.ERR_ILLEGAL=5]="ERR_ILLEGAL",u[u.ERR_PROCESSING=6]="ERR_PROCESSING",u[u.ERR_NOT_FOUND=7]="ERR_NOT_FOUND",u[u.ERR_IN_USE=8]="ERR_IN_USE",u[u.ERR_TYPE_INVALID=9]="ERR_TYPE_INVALID",u[u.ERR_DISCONNECT=10]="ERR_DISCONNECT",u[u.ERR_ALREADY_OPENED=11]="ERR_ALREADY_OPENED",u[u.ERR_ALREADY_USED=12]="ERR_ALREADY_USED",u[u.ERR_BOX_COUNT_OVER=13]="ERR_BOX_COUNT_OVER",u[u.ERR_BOX_CLIENT_OVER=14]="ERR_BOX_CLIENT_OVER",u[u.ERR_UNSUPPORTED=15]="ERR_UNSUPPORTED",u[u.ERR_DEVICE_BUSY=16]="ERR_DEVICE_BUSY",u[u.ERR_RECOVERY_FAILURE=17]="ERR_RECOVERY_FAILURE",u[u.ERR_FAILURE=255]="ERR_FAILURE",u))(ee||{}),te=(m=>(m[m.SUCCESS=0]="SUCCESS",m[m.ERR_TIMEOUT=1]="ERR_TIMEOUT",m[m.ERR_NOT_FOUND=2]="ERR_NOT_FOUND",m[m.ERR_AUTORECOVER=3]="ERR_AUTORECOVER",m[m.ERR_COVER_OPEN=4]="ERR_COVER_OPEN",m[m.ERR_CUTTER=5]="ERR_CUTTER",m[m.ERR_MECHANICAL=6]="ERR_MECHANICAL",m[m.ERR_EMPTY=7]="ERR_EMPTY",m[m.ERR_UNRECOVERABLE=8]="ERR_UNRECOVERABLE",m[m.ERR_SYSTEM=9]="ERR_SYSTEM",m[m.ERR_PORT=10]="ERR_PORT",m[m.ERR_FAILURE=255]="ERR_FAILURE",m))(te||{}),V=(l=>(l[l.EN=0]="EN",l[l.JA=1]="JA",l[l.ZH_CN=2]="ZH_CN",l[l.ZH_TW=3]="ZH_TW",l[l.KO=4]="KO",l[l.TH=5]="TH",l[l.VI=6]="VI",l[l.MULTI=7]="MULTI",l))(V||{}),re=(o=>(o[o.FONT_A=0]="FONT_A",o[o.FONT_B=1]="FONT_B",o[o.FONT_C=2]="FONT_C",o[o.FONT_D=3]="FONT_D",o[o.FONT_E=4]="FONT_E",o))(re||{}),G=(s=>(s[s.TM_M10=0]="TM_M10",s[s.TM_M30=1]="TM_M30",s[s.TM_P20=2]="TM_P20",s[s.TM_P60=3]="TM_P60",s[s.TM_P60II=4]="TM_P60II",s[s.TM_P80=5]="TM_P80",s[s.TM_T20=6]="TM_T20",s[s.TM_T60=7]="TM_T60",s[s.TM_T70=8]="TM_T70",s[s.TM_T81=9]="TM_T81",s[s.TM_T82=10]="TM_T82",s[s.TM_T83=11]="TM_T83",s[s.TM_T88=12]="TM_T88",s[s.TM_T90=13]="TM_T90",s[s.TM_T90KP=14]="TM_T90KP",s[s.TM_U220=15]="TM_U220",s[s.TM_U330=16]="TM_U330",s[s.TM_L90=17]="TM_L90",s[s.TM_H6000=18]="TM_H6000",s[s.TM_T83III=19]="TM_T83III",s[s.TM_T100=20]="TM_T100",s[s.TM_M30II=21]="TM_M30II",s[s.TM_M50=23]="TM_M50",s[s.TM_T88VII=24]="TM_T88VII",s[s.TM_L90LFC=25]="TM_L90LFC",s[s.TM_L100=26]="TM_L100",s[s.TM_P20II=27]="TM_P20II",s[s.TM_P80II=28]="TM_P80II",s[s.TM_M30III=29]="TM_M30III",s[s.TM_M50II=30]="TM_M50II",s[s.TM_M55=31]="TM_M55",s[s.TM_U220II=32]="TM_U220II",s))(G||{});var N=class t extends h{constructor(r,n,i,o,T){super(n,i,o,T);this.isHardwareError=r;this.name="PrinterError",Object.setPrototypeOf(this,t.prototype)}},q=t=>{if(t instanceof N)return t.isHardwareError;let e=String(t.message||t).toUpperCase();return e.includes("ERR_OFF_LINE")||e.includes("ERR_COVER_OPEN")||e.includes("ERR_PAPER_OUT")||e.includes("ERR_FAILURE")||e.includes("DISCONNECT")||e.includes("PRINT_TIMEOUT")||e.includes("ERROR CODE: 4")||e.includes("ERROR CODE: 7")||e.includes("ERROR CODE: 3")},ne=t=>{if(t instanceof N)return t;let e=R(t),r=q(t);return new N(r,e.code,e.translationCode,e.message,e.originalError)};var M=require("react-native"),g=M.NativeModules.MunchiEpsonModule,z=()=>g?new M.NativeEventEmitter(g):null,_e=t=>new Promise(e=>setTimeout(e,t)),Oe=t=>{if(!t||typeof t!="object")return"";let e=t.code;return typeof e=="string"?e.toUpperCase():""},ye=t=>{if(t instanceof Error)return t.message.toUpperCase();if(t&&typeof t=="object"){let e=t.message;if(typeof e=="string")return e.toUpperCase()}return String(t).toUpperCase()},p=(t,e,r)=>t.includes(r)||e===r||e.includes(r),S=async(t,e=3,r=500)=>{try{return await t()}catch(n){if(e<=1)throw n;let i=ye(n),o=Oe(n);if(p(i,o,"TARGET_IN_USE"))throw n;if(p(i,o,"BUSY")||p(i,o,"IN_USE")||p(i,o,"ERR_IN_USE")||p(i,o,"PRINT_BUSY")||p(i,o,"ERR_CONNECT")||p(i,o,"CONNECT_ERROR")||p(i,o,"ERR_TIMEOUT")||p(i,o,"PRINT_TIMEOUT"))return await _e(r),S(t,e-1,r);throw n}};var ve=new Set([0,3,5,21]),w=class{constructor(e){this.config=e}statusSubscription=null;connectionSubscription=null;connectionCallbacks=new Set;emitStatus(e){for(let r of this.connectionCallbacks)r(e)}ensureStatusListener(){if(this.statusSubscription)return;let e=z();e&&(this.config.logger?.info?.("[EpsonPrinter] Starting status listener for queue management"),this.statusSubscription=e.addListener("onPrinterStatusChange",r=>{if(!this.config.getSessionId()||r.sessionId!==this.config.getSessionId())return;let n=`[EpsonPrinter] Received Status Event: ${r.eventType}`;this.config.logger?.info?.(n),this.config.onEvent({connectionStatus:null,statusEventType:r.eventType,isRecoveryEvent:ve.has(r.eventType)})}))}ensureConnectionListener(){if(this.connectionSubscription)return;let e=z();e&&(this.connectionSubscription=e.addListener("onPrinterConnectionChange",r=>{if(!this.config.getSessionId()||r.sessionId!==this.config.getSessionId())return;let n="UNKNOWN";switch(r.status){case"RECONNECTING":n="RECONNECTING";break;case"RECONNECTED":n="CONNECTED";break;case"DISCONNECTED":n="DISCONNECTED";break;default:n="UNKNOWN"}this.config.onEvent({connectionStatus:n,statusEventType:null,isRecoveryEvent:!1}),this.emitStatus(n)}))}removeStatusListener(){this.statusSubscription&&(this.statusSubscription.remove(),this.statusSubscription=null)}onConnectionChange(e){return this.connectionCallbacks.add(e),e(this.config.getConnectionStatus()),this.ensureConnectionListener(),()=>{this.connectionCallbacks.delete(e),this.connectionCallbacks.size===0&&this.connectionSubscription&&(this.connectionSubscription.remove(),this.connectionSubscription=null)}}};var b=(n=>(n.Small="Small",n.Medium="Medium",n.Large="Large",n))(b||{});var De=(t,e=!1)=>{let r={Small:{normal:{width:1,height:1},item:{width:1,height:1}},Medium:{normal:{width:2,height:2},item:{width:2,height:2}},Large:{normal:{width:2,height:2},item:{width:3,height:3}}},n=e?"item":"normal";return r[t]?.[n]??r.Small.normal},ie=t=>{if(typeof t!="object"||t===null)return null;let e=t;return{target:String(e.target||""),name:String(e.name||""),macAddress:e.macAddress?String(e.macAddress):void 0,ipAddress:e.ipAddress?String(e.ipAddress):void 0,bdAddress:e.bdAddress?String(e.bdAddress):void 0,type:"EPSON"}},se=t=>t.commands.map(e=>{switch(e.type){case"text":return{cmd:"addText",data:e.text,align:e.align,bold:e.bold,underline:e.underline,size:e.size};case"align":return{cmd:"addTextAlign",align:e.align};case"bold":return{cmd:"addTextStyle",bold:e.value};case"fontSize":{let r=De(e.size,e.isItemName);return{cmd:"addTextSize",width:r.width,height:r.height}}case"newline":return{cmd:"addFeedLine",line:e.lines||1};case"separator":return{cmd:"addText",data:`------------------
2
2
  `};case"textLang":return{cmd:"addTextLang",lang:e.lang};case"textFont":return{cmd:"addTextFont",font:e.font};case"image":return{cmd:"addImage",data:e.base64,width:e.width};case"cut":return{cmd:"addCut"};case"drawer":return{cmd:"addPulse"};case"barcode":return{cmd:"addBarcode",data:e.data,type:e.system,width:e.width,height:e.height};case"qr":return{cmd:"addSymbol",data:e.data,type:"QRCODE_MODEL_2",level:e.errorCorrection||"M",width:e.size||3};case"feed":return{cmd:"addFeedLine",line:e.lines};default:return null}}).filter(e=>e!==null);var oe=t=>{let e=[];if(t.settings?.fontSize){let r="Medium";t.settings.fontSize==="Small"&&(r="Small"),t.settings.fontSize==="Large"&&(r="Large"),e.push({type:"fontSize",size:r})}t.settings?.fontStyle==="Bold"&&e.push({type:"bold",value:!0}),e.push({type:"align",align:"center"}),t.tbl&&e.push({type:"text",text:`Table: ${t.tbl}
3
3
  `,bold:!0,size:"double-height"}),e.push({type:"text",text:`Order: ${t.id}
4
4
  `,bold:!0}),e.push({type:"text",text:`${t.ts}
@@ -8,4 +8,4 @@
8
8
  `,align:"center",bold:!0})}else i==="B"?e.push({type:"text",text:c+`
9
9
  `,bold:!0}):i==="R"?e.push({type:"text",text:c+`
10
10
  `,bold:!0}):e.push({type:"text",text:c+`
11
- `,align:"left"})}return e.push({type:"feed",lines:3}),e.push({type:"cut"}),{commands:e}};var x=class{constructor(e){this.logger=e}queue=Promise.resolve();isQueuePaused=!1;resumeQueueResolver=null;isPaused(){return this.isQueuePaused}pause(){this.isQueuePaused=!0}resume(){this.isQueuePaused&&(this.isQueuePaused=!1,this.resumeQueueResolver&&(this.resumeQueueResolver(),this.resumeQueueResolver=null))}async waitUntilResumed(){if(this.isQueuePaused)return new Promise(e=>{let r=this.resumeQueueResolver;this.resumeQueueResolver=()=>{r&&r(),e()}})}enqueue(e){return new Promise((r,n)=>{let i=async()=>{this.isQueuePaused&&(this.logger?.info?.("[EpsonPrinter] Queue is PAUSED. Waiting for resume..."),await this.waitUntilResumed(),this.logger?.info?.("[EpsonPrinter] Queue RESUMED."));try{let o=await e();r(o)}catch(o){n(o)}};this.queue=this.queue.then(i,i)})}enqueueBackground(e){this.queue=this.queue.then(e,e)}};var A=class{constructor(e){this.config=e}recoveryTimer=null;start(){this.recoveryTimer||(this.config.logger?.info?.("[EpsonPrinter] Starting recovery polling..."),this.recoveryTimer=setInterval(async()=>{try{let e=await this.config.getStatus();if(e.online&&!e.coverOpen&&e.errorStatus==="NONE")this.config.logger?.info?.("[EpsonPrinter] Poller detected healthy status. Requesting queue resume."),this.config.onRecovered();else{let n=`[EpsonPrinter] Polling... Status: Online=${e.online}, Cover=${e.coverOpen}, Err=${e.errorStatus}`;this.config.logger?.info?.(n)}}catch(e){this.config.logger?.error("[EpsonPrinter] Recovery poll failed",e)}},2e3))}stop(){this.recoveryTimer&&(this.config.logger?.info?.("[EpsonPrinter] Stopping recovery polling."),clearInterval(this.recoveryTimer),this.recoveryTimer=null)}};var L=class{constructor(e){this.logger=e}sessionId=null;sessionInitPromise=null;getSessionId(){return this.sessionId}async ensureSession(){if(!m)throw new Error("Native module not found");return this.sessionId?this.sessionId:this.sessionInitPromise?this.sessionInitPromise:(this.sessionInitPromise=m.initSession().then(e=>(this.sessionId=e,e)).finally(()=>{this.sessionInitPromise=null}),this.sessionInitPromise)}async disposeSession(){if(!m||!this.sessionId){this.sessionId=null;return}let e=this.sessionId;this.sessionId=null;try{await m.disposeSession(e)}catch(r){this.logger?.error("[EpsonPrinter] Dispose session failed",r)}}};var U=class{constructor(e){this.config=e}statusCallbacks=new Set;latestHardwareStatus=null;statusRefreshPromise=null;statusRefreshTimer=null;subscribe(e){return this.statusCallbacks.add(e),this.latestHardwareStatus?e(this.latestHardwareStatus):this.config.getConnectionStatus()!=="CONNECTED"?e(this.config.defaultStatus):this.refreshNow(),()=>{this.statusCallbacks.delete(e)}}handleStatusEvent(){this.scheduleRefresh()}handleConnected(){this.scheduleRefresh(0)}handleDisconnected(){this.clearRefreshTimer(),this.setHardwareStatus(this.config.defaultStatus)}clear(){this.clearRefreshTimer()}async refreshNow(){if(this.config.getConnectionStatus()!=="CONNECTED"){this.setHardwareStatus(this.config.defaultStatus);return}if(this.statusRefreshPromise){await this.statusRefreshPromise;return}this.statusRefreshPromise=(async()=>{try{let e=await this.config.getStatus();this.setHardwareStatus(e)}catch(e){this.config.logger?.error("[EpsonPrinter] Status refresh failed",e),this.latestHardwareStatus||this.setHardwareStatus(this.config.defaultStatus)}})().finally(()=>{this.statusRefreshPromise=null}),await this.statusRefreshPromise}emitHardwareStatus(e){for(let r of this.statusCallbacks)r(e)}setHardwareStatus(e){this.latestHardwareStatus=e,this.emitHardwareStatus(e)}clearRefreshTimer(){this.statusRefreshTimer&&(clearTimeout(this.statusRefreshTimer),this.statusRefreshTimer=null)}scheduleRefresh(e=150){this.config.getConnectionStatus()==="CONNECTED"&&(this.statusRefreshTimer||(this.statusRefreshTimer=setTimeout(()=>{this.statusRefreshTimer=null,this.refreshNow()},e)))}};var Q={online:!1,coverOpen:!1,paperEmpty:!1,paper:"EMPTY",errorStatus:"NONE",drawerOpen:!1},Me=5e3,F=class{disconnectTimer=null;connectionStatus="DISCONNECTED";isConnecting=!1;target=null;defaultTarget;model;lang;logger;sessionManager;queueController;recoveryController;statusController;eventRouter;constructor(e){this.logger=e.logger,this.defaultTarget=e.target??null,this.model=e.model,this.lang=e.lang??0,this.sessionManager=new L(this.logger),this.queueController=new x(this.logger),this.recoveryController=new A({logger:this.logger,getStatus:()=>this.getStatus(),onRecovered:()=>this.resumeQueue()}),this.statusController=new U({logger:this.logger,defaultStatus:Q,getConnectionStatus:()=>this.connectionStatus,getStatus:()=>this.getStatus()}),this.eventRouter=new w({logger:this.logger,getSessionId:()=>this.sessionManager.getSessionId(),getConnectionStatus:()=>this.connectionStatus,onEvent:r=>{r.statusEventType!==null&&(this.statusController.handleStatusEvent(),r.isRecoveryEvent&&this.queueController.isPaused()&&(this.logger?.info?.("[EpsonPrinter] Printer recovered (Event). Resuming queue..."),this.resumeQueue())),r.connectionStatus!==null&&(r.connectionStatus==="CONNECTED"&&(this.connectionStatus="CONNECTED"),r.connectionStatus==="CONNECTED"&&this.statusController.handleConnected(),r.connectionStatus==="DISCONNECTED"&&(this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected()))}})}resumeQueue(){if(!this.queueController.isPaused())return;this.logger?.info?.("[EpsonPrinter] Resuming queue..."),this.recoveryController.stop(),this.queueController.resume()}isIosConnectionRecoveryCandidate(e){return W.Platform.OS!=="ios"?!1:e.code==="CONNECTION_FAILED"||e.code==="TIMEOUT"&&e.translationCode==="printer.error.connection_timeout"||e.translationCode==="printer.error.offline"}hasTargetInUseToken(e){if(!e||typeof e!="object")return!1;let r=String(e.code??"").toUpperCase(),n=String(e.message??"").toUpperCase();return r.includes("TARGET_IN_USE")||n.includes("TARGET_IN_USE")}isIosConnectBusyRecoveryCandidate(e){return W.Platform.OS!=="ios"||e.translationCode!=="printer.error.busy"?!1:!this.hasTargetInUseToken(e.originalError)}async recoverIosSessionForPrint(){if(!m)throw new Error("Native module not found");let e=this.target??this.defaultTarget;if(!e)throw new Error("Printer target is required");let r=this.sessionManager.getSessionId();if(r)try{await m.disconnect(r)}catch(i){this.logger?.error("[EpsonPrinter] iOS recovery disconnect failed (best-effort)",i)}await this.sessionManager.disposeSession(),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED");let n=await this.sessionManager.ensureSession();return await S(()=>m.connect(n,e,Me,this.model,this.lang)),this.target=e,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected(),n}async recoverIosSessionForConnect(e,r){if(!m)throw new Error("Native module not found");let n=this.sessionManager.getSessionId();if(n)try{await m.disconnect(n)}catch(o){this.logger?.error("[EpsonPrinter] iOS connect recovery disconnect failed (best-effort)",o)}await this.sessionManager.disposeSession(),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED");let i=await this.sessionManager.ensureSession();await S(()=>m.connect(i,e,r,this.model,this.lang)),this.target=e,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected()}async connect(e,r=5e3){let n=e??this.defaultTarget;if(!n){let i=new Error("Printer target is required");throw this.logger?.error("[EpsonPrinter] Connect failed: missing target",i),i}return this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.queueController.enqueue(async()=>{if(!m){this.logger?.error("[EpsonPrinter] Native module not found");return}if(this.connectionStatus==="CONNECTED"&&this.target===n)return;let i=await this.sessionManager.ensureSession();this.isConnecting=!0,this.connectionStatus="CONNECTING",this.eventRouter.emitStatus("CONNECTING");let o=!1;try{await S(()=>m.connect(i,n,r,this.model,this.lang)),this.target=n,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.isConnecting=!1,this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected()}catch(T){let c=f(T);if(!o&&this.isIosConnectBusyRecoveryCandidate(c)){o=!0,this.logger?.error("[EpsonPrinter] iOS busy connect suspected stale session. Recovering connection and retrying connect once...",c);try{await this.recoverIosSessionForConnect(n,r),this.logger?.info?.("[EpsonPrinter] iOS connect recovery succeeded."),this.isConnecting=!1;return}catch(l){let P=f(l);throw this.logger?.error("[EpsonPrinter] iOS connect recovery failed",P),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,P}}throw this.logger?.error(`[EpsonPrinter] Connect failed for ${n}`,c),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,c}})}async print(e){if(!m)return;this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null);let r=0,n=!1;return this.queueController.enqueue(async()=>{let i=await this.sessionManager.ensureSession(),o=se(e);for(;;){r++,this.logger?.info?.(`[EpsonPrinter] Print attempt ${r}`);try{await S(()=>m.print(i,o));break}catch(T){let c=ne(T);if(!n&&this.isIosConnectionRecoveryCandidate(c)){n=!0,this.logger?.error("[EpsonPrinter] iOS stale session suspected. Recovering connection and retrying print once...",c);try{i=await this.recoverIosSessionForPrint(),this.logger?.info?.("[EpsonPrinter] iOS session recovery succeeded. Retrying print...");continue}catch(l){let P=f(l);throw this.logger?.error("[EpsonPrinter] iOS session recovery failed",P),P}}if(c.isHardwareError){this.queueController.pause(),this.logger?.error(`[EpsonPrinter] Hardware error on attempt ${r}. Waiting for recovery before retry...`,c),this.recoveryController.start(),await this.queueController.waitUntilResumed(),this.logger?.info?.("[EpsonPrinter] Recovery detected. Sending cut to flush partial print before retry...");try{await m.print(i,[{cmd:"addCut"}])}catch{}this.logger?.info?.("[EpsonPrinter] Retrying print job...")}else throw this.logger?.error(`[EpsonPrinter] Print failed (non-recoverable) on attempt ${r}`,c),c}}}).finally(()=>{this.disconnectTimer&&clearTimeout(this.disconnectTimer),this.disconnectTimer=setTimeout(()=>{this.performDisconnect()},5e3)})}performDisconnect(){this.queueController.enqueueBackground(async()=>{if(!this.disconnectTimer)return;let e=this.sessionManager.getSessionId();if(m&&this.connectionStatus==="CONNECTED"&&e)try{await m.disconnect(e),this.connectionStatus="DISCONNECTED",this.target=null,this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),await this.sessionManager.disposeSession()}catch(r){this.logger?.error("[EpsonPrinter] Auto-Disconnect failed",r)}this.disconnectTimer=null})}async printEmbedded(e){try{let r=oe(e);return await this.print(r)}catch(r){let n=f(r);throw String(r).includes("MunchiPrinterError")||this.logger?.error("[EpsonPrinter] Embedded print failed",n),n}}async disconnect(){return this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.queueController.enqueue(async()=>{if(!m)return;let e=this.sessionManager.getSessionId();if(e)try{await m.disconnect(e),this.connectionStatus="DISCONNECTED",this.target=null,this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.eventRouter.removeStatusListener(),this.recoveryController.stop()}catch(r){this.logger?.error("[EpsonPrinter] Disconnect failed",r)}finally{this.statusController.clear(),await this.sessionManager.disposeSession()}})}async getStatus(){if(!m)return Q;let e=this.sessionManager.getSessionId();if(!e)return Q;try{return await m.getStatus(e)}catch(r){throw this.logger?.error("[EpsonPrinter] GetStatus failed",r),f(r)}}async openDrawer(){let e={commands:[{type:"drawer"}]};return this.print(e)}onConnectionChange(e){return this.eventRouter.onConnectionChange(e)}onStatusChange(e){return this.statusController.subscribe(e)}};var ue=require("react-native");var ae=ue.NativeModules.MunchiEpsonModule,we={bluetooth:["BT:"],tcp:["TCP:","TCPS:"],usb:["USB:"]},be=t=>t.includes("[local_"),xe=(t,e)=>e?we[e].some(n=>t.startsWith(n)):!0,ce=async t=>{if(!ae)return console.warn("[EpsonDiscovery] Native module not found"),[];try{return(await ae.discover({timeout:t.timeout})).map(ie).filter(r=>!r||be(r.target)?!1:xe(r.target,t.connectionType))}catch(e){let r=f(e);throw console.error("[EpsonDiscovery] Discovery failed",r),r}};var Ae=[[/^TM-m10/i,0],[/^TM-m30III/i,29],[/^TM-m30II/i,21],[/^TM-m30/i,1],[/^TM-m50II/i,30],[/^TM-m50/i,23],[/^TM-m55/i,31],[/^TM-P20II/i,27],[/^TM-P20/i,2],[/^TM-P60II/i,4],[/^TM-P60/i,3],[/^TM-P80II/i,28],[/^TM-P80/i,5],[/^TM-T20/i,6],[/^TM-T60/i,7],[/^TM-T70/i,8],[/^TM-T81/i,9],[/^TM-T82/i,10],[/^TM-T83III/i,19],[/^TM-T83/i,11],[/^TM-T88VII/i,24],[/^TM-T88/i,12],[/^TM-T90KP/i,14],[/^TM-T90/i,13],[/^TM-T100/i,20],[/^TM-U220II/i,32],[/^TM-U220/i,15],[/^TM-U330/i,16],[/^TM-L90LFC/i,25],[/^TM-L90/i,17],[/^TM-L100/i,26],[/^TM-H6000/i,18]],le=t=>{for(let[e,r]of Ae)if(e.test(t))return r;return null};var de=t=>{let e=f(t),r=e.message.toUpperCase(),n=t instanceof p?t.isHardwareError:z(t),i=e.translationCode==="printer.error.busy"||r.includes("BUSY")||r.includes("IN_USE")||r.includes("PRINT_BUSY"),o=e.code==="TIMEOUT"||e.translationCode==="printer.error.connection_timeout"||r.includes("TIMEOUT"),T=e.code==="CONNECTION_FAILED"||e.translationCode==="printer.error.offline",c=e.code==="DISCOVERY_FAILED",l="UNKNOWN";return n?l="HARDWARE":i?l="BUSY":o?l="TIMEOUT":T?l="CONNECTION":c?l="DISCOVERY":e.code==="PRINT_FAILED"&&(l="PRINT"),{code:e.code,translationCode:e.translationCode,message:e.message,category:l,isHardwareError:n,retryable:i||o,shouldPauseQueue:n,raw:t}},Le=t=>de(t);var me=Ne(require("react")),R=require("react");var ge=(0,R.createContext)(void 0),Ue=({config:t,logger:e,children:r})=>{let[n,i]=(0,R.useState)();(0,R.useEffect)(()=>{e&&B(e)},[e]);let o=(0,R.useMemo)(()=>{let c={...t,logger:e??t.logger};return K(c)},[t.lang,t.model,t.target,e]),T=(0,R.useMemo)(()=>({printer:o,config:t,isReady:!0,error:n}),[o,t,n]);return me.default.createElement(ge.Provider,{value:T},r)},k=()=>{let t=(0,R.useContext)(ge);if(!t)throw new Error("usePrinter must be used within a PrinterProvider");return t};var E=require("react");function Fe(t={}){let{enabled:e=!0}=t,{printer:r,config:n}=k(),[i,o]=(0,E.useState)("DISCONNECTED"),[T,c]=(0,E.useState)(null),[l,P]=(0,E.useState)(null),X=(0,E.useRef)("DISCONNECTED"),$=(0,E.useRef)(null),d=l!==null,I=(0,E.useCallback)(C=>{$.current=C,P(C)},[]),_=(0,E.useCallback)(C=>{if(C instanceof Error&&C.message){I(C.message);return}if(typeof C=="string"&&C.length>0){I(C);return}I("Unknown printer error")},[I]),a=(0,E.useCallback)(()=>{$.current!==null&&I(null)},[I]);return(0,E.useEffect)(()=>r.onConnectionChange(O=>{X.current=O,o(O),O==="CONNECTED"&&a()}),[a,r]),(0,E.useEffect)(()=>e?r.onStatusChange(O=>{c(O),a()}):void 0,[a,e,r]),(0,E.useEffect)(()=>{e&&i==="CONNECTED"&&r.getStatus().then(C=>{c(C),a()}).catch(_)},[_,a,i,e,r]),(0,E.useEffect)(()=>{let C=X.current;if(e){C==="DISCONNECTED"&&i==="DISCONNECTED"&&r.connect(n.target,5e3).then(a).catch(_);return}(i==="CONNECTED"||i==="CONNECTING")&&r.disconnect().then(a).catch(_)},[_,a,n.target,i,e,r]),{connectionStatus:i,hardwareStatus:T,isError:d,errorMessage:l}}var Te="1.0.7";function K(t){return new F({...t,logger:t.logger??Y()})}0&&(module.exports={Epos2CallbackCode,Epos2ConnectionEvent,Epos2ErrorStatus,Epos2Font,Epos2Lang,Epos2StatusEvent,EpsonModel,FontSize,MunchiPrinterError,PrinterError,PrinterErrorCode,PrinterProvider,PrinterTranslationCode,VERSION,discoverPrinters,getGlobalLogger,getPrinter,resolveEpsonError,resolveModelFromBluetoothName,resolvePrinterError,setGlobalLogger,usePrinter,usePrinterStatus});
11
+ `,align:"left"})}return e.push({type:"feed",lines:3}),e.push({type:"cut"}),{commands:e}};var x=class{constructor(e){this.logger=e}queue=Promise.resolve();isQueuePaused=!1;resumeQueueResolver=null;isPaused(){return this.isQueuePaused}pause(){this.isQueuePaused=!0}resume(){this.isQueuePaused&&(this.isQueuePaused=!1,this.resumeQueueResolver&&(this.resumeQueueResolver(),this.resumeQueueResolver=null))}async waitUntilResumed(){if(this.isQueuePaused)return new Promise(e=>{let r=this.resumeQueueResolver;this.resumeQueueResolver=()=>{r&&r(),e()}})}enqueue(e){return new Promise((r,n)=>{let i=async()=>{this.isQueuePaused&&(this.logger?.info?.("[EpsonPrinter] Queue is PAUSED. Waiting for resume..."),await this.waitUntilResumed(),this.logger?.info?.("[EpsonPrinter] Queue RESUMED."));try{let o=await e();r(o)}catch(o){n(o)}};this.queue=this.queue.then(i,i)})}enqueueBackground(e){this.queue=this.queue.then(e,e)}};var A=class{constructor(e){this.config=e}recoveryTimer=null;start(){this.recoveryTimer||(this.config.logger?.info?.("[EpsonPrinter] Starting recovery polling..."),this.recoveryTimer=setInterval(async()=>{try{let e=await this.config.getStatus();if(e.online&&!e.coverOpen&&e.errorStatus==="NONE")this.config.logger?.info?.("[EpsonPrinter] Poller detected healthy status. Requesting queue resume."),this.config.onRecovered();else{let n=`[EpsonPrinter] Polling... Status: Online=${e.online}, Cover=${e.coverOpen}, Err=${e.errorStatus}`;this.config.logger?.info?.(n)}}catch(e){this.config.logger?.error("[EpsonPrinter] Recovery poll failed",e)}},2e3))}stop(){this.recoveryTimer&&(this.config.logger?.info?.("[EpsonPrinter] Stopping recovery polling."),clearInterval(this.recoveryTimer),this.recoveryTimer=null)}};var L=class{constructor(e){this.logger=e}sessionId=null;sessionInitPromise=null;getSessionId(){return this.sessionId}async ensureSession(){if(!g)throw new Error("Native module not found");return this.sessionId?this.sessionId:this.sessionInitPromise?this.sessionInitPromise:(this.sessionInitPromise=g.initSession().then(e=>(this.sessionId=e,e)).finally(()=>{this.sessionInitPromise=null}),this.sessionInitPromise)}async disposeSession(){if(!g||!this.sessionId){this.sessionId=null;return}let e=this.sessionId;this.sessionId=null;try{await g.disposeSession(e)}catch(r){this.logger?.error("[EpsonPrinter] Dispose session failed",r)}}};var U=class{constructor(e){this.config=e}statusCallbacks=new Set;latestHardwareStatus=null;statusRefreshPromise=null;statusRefreshTimer=null;subscribe(e){return this.statusCallbacks.add(e),this.latestHardwareStatus?e(this.latestHardwareStatus):this.config.getConnectionStatus()!=="CONNECTED"?e(this.config.defaultStatus):this.refreshNow(),()=>{this.statusCallbacks.delete(e)}}handleStatusEvent(){this.scheduleRefresh()}handleConnected(){this.scheduleRefresh(0)}handleDisconnected(){this.clearRefreshTimer(),this.setHardwareStatus(this.config.defaultStatus)}clear(){this.clearRefreshTimer()}async refreshNow(){if(this.config.getConnectionStatus()!=="CONNECTED"){this.setHardwareStatus(this.config.defaultStatus);return}if(this.statusRefreshPromise){await this.statusRefreshPromise;return}this.statusRefreshPromise=(async()=>{try{let e=await this.config.getStatus();this.setHardwareStatus(e)}catch(e){this.config.logger?.error("[EpsonPrinter] Status refresh failed",e),this.latestHardwareStatus||this.setHardwareStatus(this.config.defaultStatus)}})().finally(()=>{this.statusRefreshPromise=null}),await this.statusRefreshPromise}emitHardwareStatus(e){for(let r of this.statusCallbacks)r(e)}setHardwareStatus(e){this.latestHardwareStatus=e,this.emitHardwareStatus(e)}clearRefreshTimer(){this.statusRefreshTimer&&(clearTimeout(this.statusRefreshTimer),this.statusRefreshTimer=null)}scheduleRefresh(e=150){this.config.getConnectionStatus()==="CONNECTED"&&(this.statusRefreshTimer||(this.statusRefreshTimer=setTimeout(()=>{this.statusRefreshTimer=null,this.refreshNow()},e)))}};var Q={online:!1,coverOpen:!1,paperEmpty:!1,paper:"EMPTY",errorStatus:"NONE",drawerOpen:!1},Me=5e3,F=class{disconnectTimer=null;connectionStatus="DISCONNECTED";isConnecting=!1;target=null;defaultTarget;model;lang;logger;sessionManager;queueController;recoveryController;statusController;eventRouter;constructor(e){this.logger=e.logger,this.defaultTarget=e.target??null,this.model=e.model,this.lang=e.lang??0,this.sessionManager=new L(this.logger),this.queueController=new x(this.logger),this.recoveryController=new A({logger:this.logger,getStatus:()=>this.getStatus(),onRecovered:()=>this.resumeQueue()}),this.statusController=new U({logger:this.logger,defaultStatus:Q,getConnectionStatus:()=>this.connectionStatus,getStatus:()=>this.getStatus()}),this.eventRouter=new w({logger:this.logger,getSessionId:()=>this.sessionManager.getSessionId(),getConnectionStatus:()=>this.connectionStatus,onEvent:r=>{r.statusEventType!==null&&(this.statusController.handleStatusEvent(),r.isRecoveryEvent&&this.queueController.isPaused()&&(this.logger?.info?.("[EpsonPrinter] Printer recovered (Event). Resuming queue..."),this.resumeQueue())),r.connectionStatus!==null&&(r.connectionStatus==="CONNECTED"&&(this.connectionStatus="CONNECTED"),r.connectionStatus==="CONNECTED"&&this.statusController.handleConnected(),r.connectionStatus==="DISCONNECTED"&&(this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected()))}})}resumeQueue(){if(!this.queueController.isPaused())return;this.logger?.info?.("[EpsonPrinter] Resuming queue..."),this.recoveryController.stop(),this.queueController.resume()}isIosConnectionRecoveryCandidate(e){return W.Platform.OS!=="ios"?!1:e.code==="CONNECTION_FAILED"||e.code==="TIMEOUT"&&e.translationCode==="printer.error.connection_timeout"||e.translationCode==="printer.error.offline"}hasTargetInUseToken(e){if(!e||typeof e!="object")return!1;let r=String(e.code??"").toUpperCase(),n=String(e.message??"").toUpperCase();return r.includes("TARGET_IN_USE")||n.includes("TARGET_IN_USE")}isIosConnectBusyRecoveryCandidate(e){return W.Platform.OS!=="ios"||e.translationCode!=="printer.error.busy"?!1:!this.hasTargetInUseToken(e.originalError)}async recoverIosSessionForPrint(){if(!g)throw new Error("Native module not found");let e=this.target??this.defaultTarget;if(!e)throw new Error("Printer target is required");let r=this.sessionManager.getSessionId();if(r)try{await g.disconnect(r)}catch(i){this.logger?.error("[EpsonPrinter] iOS recovery disconnect failed (best-effort)",i)}await this.sessionManager.disposeSession(),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED");let n=await this.sessionManager.ensureSession();return await S(()=>g.connect(n,e,Me,this.model,this.lang)),this.target=e,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected(),n}async recoverIosSessionForConnect(e,r){if(!g)throw new Error("Native module not found");let n=this.sessionManager.getSessionId();if(n)try{await g.disconnect(n)}catch(o){this.logger?.error("[EpsonPrinter] iOS connect recovery disconnect failed (best-effort)",o)}await this.sessionManager.disposeSession(),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED");let i=await this.sessionManager.ensureSession();await S(()=>g.connect(i,e,r,this.model,this.lang)),this.target=e,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected()}async connect(e,r=5e3){let n=e??this.defaultTarget;if(!n){let i=new Error("Printer target is required");throw this.logger?.error("[EpsonPrinter] Connect failed: missing target",i),i}return this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.queueController.enqueue(async()=>{if(!g){this.logger?.error("[EpsonPrinter] Native module not found");return}if(this.connectionStatus==="CONNECTED"&&this.target===n)return;let i=await this.sessionManager.ensureSession();this.isConnecting=!0,this.connectionStatus="CONNECTING",this.eventRouter.emitStatus("CONNECTING");let o=!1;try{await S(()=>g.connect(i,n,r,this.model,this.lang)),this.target=n,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.isConnecting=!1,this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected()}catch(T){let c=R(T);if(!o&&this.isIosConnectBusyRecoveryCandidate(c)){o=!0,this.logger?.error("[EpsonPrinter] iOS busy connect suspected stale session. Recovering connection and retrying connect once...",c);try{await this.recoverIosSessionForConnect(n,r),this.logger?.info?.("[EpsonPrinter] iOS connect recovery succeeded."),this.isConnecting=!1;return}catch(l){let P=R(l);throw this.logger?.error("[EpsonPrinter] iOS connect recovery failed",P),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,P}}throw this.logger?.error(`[EpsonPrinter] Connect failed for ${n}`,c),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,c}})}async print(e){if(!g)return;this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null);let r=0,n=!1;return this.queueController.enqueue(async()=>{let i=await this.sessionManager.ensureSession(),o=se(e);for(;;){r++,this.logger?.info?.(`[EpsonPrinter] Print attempt ${r}`);try{await S(()=>g.print(i,o));break}catch(T){let c=ne(T);if(!n&&this.isIosConnectionRecoveryCandidate(c)){n=!0,this.logger?.error("[EpsonPrinter] iOS stale session suspected. Recovering connection and retrying print once...",c);try{i=await this.recoverIosSessionForPrint(),this.logger?.info?.("[EpsonPrinter] iOS session recovery succeeded. Retrying print...");continue}catch(l){let P=R(l);throw this.logger?.error("[EpsonPrinter] iOS session recovery failed",P),P}}if(c.isHardwareError){this.queueController.pause(),this.logger?.error(`[EpsonPrinter] Hardware error on attempt ${r}. Waiting for recovery before retry...`,c),this.recoveryController.start(),await this.queueController.waitUntilResumed(),this.logger?.info?.("[EpsonPrinter] Recovery detected. Sending cut to flush partial print before retry...");try{await g.print(i,[{cmd:"addCut"}])}catch{}this.logger?.info?.("[EpsonPrinter] Retrying print job...")}else throw this.logger?.error(`[EpsonPrinter] Print failed (non-recoverable) on attempt ${r}`,c),c}}}).finally(()=>{this.disconnectTimer&&clearTimeout(this.disconnectTimer),this.disconnectTimer=setTimeout(()=>{this.performDisconnect()},5e3)})}performDisconnect(){this.queueController.enqueueBackground(async()=>{if(!this.disconnectTimer)return;let e=this.sessionManager.getSessionId();if(g&&this.connectionStatus==="CONNECTED"&&e)try{await g.disconnect(e),this.connectionStatus="DISCONNECTED",this.target=null,this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),await this.sessionManager.disposeSession()}catch(r){this.logger?.error("[EpsonPrinter] Auto-Disconnect failed",r)}this.disconnectTimer=null})}async printEmbedded(e){try{let r=oe(e);return await this.print(r)}catch(r){let n=R(r);throw String(r).includes("MunchiPrinterError")||this.logger?.error("[EpsonPrinter] Embedded print failed",n),n}}async disconnect(){return this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.queueController.enqueue(async()=>{if(!g)return;let e=this.sessionManager.getSessionId();if(e)try{await g.disconnect(e),this.connectionStatus="DISCONNECTED",this.target=null,this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.eventRouter.removeStatusListener(),this.recoveryController.stop()}catch(r){this.logger?.error("[EpsonPrinter] Disconnect failed",r)}finally{this.statusController.clear(),await this.sessionManager.disposeSession()}})}async getStatus(){if(!g)return Q;let e=this.sessionManager.getSessionId();if(!e)return Q;try{return await g.getStatus(e)}catch(r){throw this.logger?.error("[EpsonPrinter] GetStatus failed",r),R(r)}}async openDrawer(){let e={commands:[{type:"drawer"}]};return this.print(e)}onConnectionChange(e){return this.eventRouter.onConnectionChange(e)}onStatusChange(e){return this.statusController.subscribe(e)}};var ue=require("react-native");var ae=ue.NativeModules.MunchiEpsonModule,we={bluetooth:["BT:"],tcp:["TCP:","TCPS:"],usb:["USB:"]},be=t=>t.includes("[local_"),xe=(t,e)=>e?we[e].some(n=>t.startsWith(n)):!0,ce=async t=>{if(!ae)return console.warn("[EpsonDiscovery] Native module not found"),[];try{return(await ae.discover({timeout:t.timeout})).map(ie).filter(r=>!r||be(r.target)?!1:xe(r.target,t.connectionType))}catch(e){let r=R(e);throw console.error("[EpsonDiscovery] Discovery failed",r),r}};var Ae=[[/^TM-m10/i,0],[/^TM-m30III/i,29],[/^TM-m30II/i,21],[/^TM-m30/i,1],[/^TM-m50II/i,30],[/^TM-m50/i,23],[/^TM-m55/i,31],[/^TM-P20II/i,27],[/^TM-P20/i,2],[/^TM-P60II/i,4],[/^TM-P60/i,3],[/^TM-P80II/i,28],[/^TM-P80/i,5],[/^TM-T20/i,6],[/^TM-T60/i,7],[/^TM-T70/i,8],[/^TM-T81/i,9],[/^TM-T82/i,10],[/^TM-T83III/i,19],[/^TM-T83/i,11],[/^TM-T88VII/i,24],[/^TM-T88/i,12],[/^TM-T90KP/i,14],[/^TM-T90/i,13],[/^TM-T100/i,20],[/^TM-U220II/i,32],[/^TM-U220/i,15],[/^TM-U330/i,16],[/^TM-L90LFC/i,25],[/^TM-L90/i,17],[/^TM-L100/i,26],[/^TM-H6000/i,18]],le=t=>{for(let[e,r]of Ae)if(e.test(t))return r;return null};var de=t=>{let e=R(t),r=e.message.toUpperCase(),n=t instanceof N?t.isHardwareError:q(t),i=e.translationCode==="printer.error.busy"||r.includes("BUSY")||r.includes("IN_USE")||r.includes("PRINT_BUSY"),o=e.code==="TIMEOUT"||e.translationCode==="printer.error.connection_timeout"||r.includes("TIMEOUT"),T=e.code==="CONNECTION_FAILED"||e.translationCode==="printer.error.offline",c=e.code==="DISCOVERY_FAILED",l="UNKNOWN";return n?l="HARDWARE":i?l="BUSY":o?l="TIMEOUT":T?l="CONNECTION":c?l="DISCOVERY":e.code==="PRINT_FAILED"&&(l="PRINT"),{code:e.code,translationCode:e.translationCode,message:e.message,category:l,isHardwareError:n,retryable:i||o,shouldPauseQueue:n,raw:t}},Le=t=>de(t);var me=pe(require("react")),f=require("react");var ge=(0,f.createContext)(void 0),Ue=({config:t,logger:e,children:r})=>{let[n,i]=(0,f.useState)();(0,f.useEffect)(()=>{e&&B(e)},[e]);let o=(0,f.useMemo)(()=>{let c={...t,logger:e??t.logger};return K(c)},[t.lang,t.model,t.target,e]),T=(0,f.useMemo)(()=>({printer:o,config:t,isReady:!0,error:n}),[o,t,n]);return me.default.createElement(ge.Provider,{value:T},r)},k=()=>{let t=(0,f.useContext)(ge);if(!t)throw new Error("usePrinter must be used within a PrinterProvider");return t};var E=require("react");function Fe(t={}){let{enabled:e=!0}=t,{printer:r,config:n}=k(),[i,o]=(0,E.useState)("DISCONNECTED"),[T,c]=(0,E.useState)(null),[l,P]=(0,E.useState)(null),X=(0,E.useRef)("DISCONNECTED"),$=(0,E.useRef)(null),m=l!==null,I=(0,E.useCallback)(C=>{$.current=C,P(C)},[]),_=(0,E.useCallback)(C=>{if(C instanceof Error&&C.message){I(C.message);return}if(typeof C=="string"&&C.length>0){I(C);return}I("Unknown printer error")},[I]),a=(0,E.useCallback)(()=>{$.current!==null&&I(null)},[I]);return(0,E.useEffect)(()=>r.onConnectionChange(O=>{X.current=O,o(O),O==="CONNECTED"&&a()}),[a,r]),(0,E.useEffect)(()=>e?r.onStatusChange(O=>{c(O),a()}):void 0,[a,e,r]),(0,E.useEffect)(()=>{e&&i==="CONNECTED"&&r.getStatus().then(C=>{c(C),a()}).catch(_)},[_,a,i,e,r]),(0,E.useEffect)(()=>{let C=X.current;if(e){C==="DISCONNECTED"&&i==="DISCONNECTED"&&r.connect(n.target,5e3).then(a).catch(_);return}(i==="CONNECTED"||i==="CONNECTING")&&r.disconnect().then(a).catch(_)},[_,a,n.target,i,e,r]),{connectionStatus:i,hardwareStatus:T,isError:m,errorMessage:l}}var Te="1.0.9";function K(t){return new F({...t,logger:t.logger??Y()})}0&&(module.exports={Epos2CallbackCode,Epos2ConnectionEvent,Epos2ErrorStatus,Epos2Font,Epos2Lang,Epos2StatusEvent,EpsonModel,FontSize,MunchiPrinterError,PrinterError,PrinterErrorCode,PrinterProvider,PrinterTranslationCode,VERSION,discoverPrinters,getGlobalLogger,getPrinter,resolveEpsonError,resolveModelFromBluetoothName,resolvePrinterError,setGlobalLogger,usePrinter,usePrinterStatus});
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- var q={},z=t=>{q.logger=t},G=()=>q.logger;import{Platform as j}from"react-native";var _=(T=>(T.CONNECTION_FAILED="CONNECTION_FAILED",T.PRINT_FAILED="PRINT_FAILED",T.DISCOVERY_FAILED="DISCOVERY_FAILED",T.TIMEOUT="TIMEOUT",T.NATIVE_MODULE_MISSING="NATIVE_MODULE_MISSING",T.UNKNOWN="UNKNOWN",T))(_||{}),O=(c=>(c.ERR_PRINTER_RECONNECT_REQUIRED="printer.error.reconnect_required",c.ERR_PRINTER_OFFLINE="printer.error.offline",c.ERR_PRINTER_BUSY="printer.error.busy",c.ERR_PRINTER_COVER_OPEN="printer.error.cover_open",c.ERR_PRINTER_PAPER_EMPTY="printer.error.paper_empty",c.ERR_CONNECTION_TIMEOUT="printer.error.connection_timeout",c.ERR_UNKNOWN="printer.error.unknown",c))(O||{}),h=class t extends Error{constructor(r,n,i,o){super(i);this.code=r;this.translationCode=n;this.originalError=o;this.name="MunchiPrinterError",Object.setPrototypeOf(this,t.prototype)}};var se=t=>{if(!t||typeof t!="object")return"";let e=t.code;return typeof e=="string"?e:""},oe=t=>{if(t instanceof Error)return t.message;if(t&&typeof t=="object"){let e=t.message;if(typeof e=="string")return e}return String(t)},g=(t,e,r)=>t.includes(r)||e===r||e.includes(r),C=t=>{if(t instanceof h)return t;let e=oe(t),r=se(t),n=e.toUpperCase(),i=r.toUpperCase();return n.includes("ERROR CODE: 4")?new h("PRINT_FAILED","printer.error.cover_open","Printer cover is open",t):n.includes("ERROR CODE: 3")?new h("PRINT_FAILED","printer.error.busy","Printer is performing auto-recovery",t):n.includes("ERROR CODE: 7")?new h("PRINT_FAILED","printer.error.paper_empty","Printer paper is empty",t):g(n,i,"TARGET_IN_USE")||g(n,i,"BUSY")||g(n,i,"IN_USE")||g(n,i,"PRINT_BUSY")||g(n,i,"DISCOVERY_BUSY")?new h("TIMEOUT","printer.error.busy","Printer is busy",t):g(n,i,"TIMEOUT")||g(n,i,"ERR_TIMEOUT")||g(n,i,"PRINT_TIMEOUT")?new h("TIMEOUT","printer.error.connection_timeout","Operation timed out",t):g(n,i,"PRINTER_RECONNECT_REQUIRED")||n.includes("RECONNECT THE PRINTER")?new h("CONNECTION_FAILED","printer.error.reconnect_required",e,t):g(n,i,"PERMISSION_DENIED")||g(n,i,"LOCATION_DISABLED")||g(n,i,"BLUETOOTH_DISABLED")?new h("CONNECTION_FAILED","printer.error.offline",e,t):g(n,i,"DISCOVERY_ERROR")||n.includes("DISCOVERY")?new h("DISCOVERY_FAILED","printer.error.unknown",e,t):g(n,i,"PRINTER_OFFLINE")||g(n,i,"SESSION_ERROR")||g(n,i,"STATUS_ERROR")||g(n,i,"INIT_ERROR")||g(n,i,"PRINT_CANCELED")||g(n,i,"DISCONNECT")||g(n,i,"ERR_DISCONNECT")||n.includes("PRINTER NOT CONNECTED")||n.includes("OFFLINE")?new h("CONNECTION_FAILED","printer.error.offline",n.includes("OFFLINE")?e:"Printer disconnected",t):g(n,i,"CONNECT_ERROR")||n.includes("FAILED TO CONNECT")||g(n,i,"ERR_CONNECT")?new h("CONNECTION_FAILED","printer.error.offline","Failed to connect to printer",t):g(n,i,"PRINT_ERROR")||g(n,i,"PRINT_FAILURE")?new h("PRINT_FAILED","printer.error.unknown",e,t):new h("UNKNOWN","printer.error.unknown",e,t)};var Q=(a=>(a[a.ONLINE=0]="ONLINE",a[a.OFFLINE=1]="OFFLINE",a[a.POWER_OFF=2]="POWER_OFF",a[a.COVER_CLOSE=3]="COVER_CLOSE",a[a.COVER_OPEN=4]="COVER_OPEN",a[a.PAPER_OK=5]="PAPER_OK",a[a.PAPER_NEAR_END=6]="PAPER_NEAR_END",a[a.PAPER_EMPTY=7]="PAPER_EMPTY",a[a.DRAWER_HIGH=8]="DRAWER_HIGH",a[a.DRAWER_LOW=9]="DRAWER_LOW",a[a.BATTERY_ENOUGH=10]="BATTERY_ENOUGH",a[a.BATTERY_EMPTY=11]="BATTERY_EMPTY",a[a.AUTO_RECOVER_ERROR=20]="AUTO_RECOVER_ERROR",a[a.AUTO_RECOVER_OK=21]="AUTO_RECOVER_OK",a[a.UNRECOVERABLE_ERROR=22]="UNRECOVERABLE_ERROR",a))(Q||{}),ae=(n=>(n[n.RECONNECTING=0]="RECONNECTING",n[n.RECONNECT=1]="RECONNECT",n[n.DISCONNECT=2]="DISCONNECT",n))(ae||{}),ue=(u=>(u[u.SUCCESS=0]="SUCCESS",u[u.ERR_PARAM=1]="ERR_PARAM",u[u.ERR_CONNECT=2]="ERR_CONNECT",u[u.ERR_TIMEOUT=3]="ERR_TIMEOUT",u[u.ERR_MEMORY=4]="ERR_MEMORY",u[u.ERR_ILLEGAL=5]="ERR_ILLEGAL",u[u.ERR_PROCESSING=6]="ERR_PROCESSING",u[u.ERR_NOT_FOUND=7]="ERR_NOT_FOUND",u[u.ERR_IN_USE=8]="ERR_IN_USE",u[u.ERR_TYPE_INVALID=9]="ERR_TYPE_INVALID",u[u.ERR_DISCONNECT=10]="ERR_DISCONNECT",u[u.ERR_ALREADY_OPENED=11]="ERR_ALREADY_OPENED",u[u.ERR_ALREADY_USED=12]="ERR_ALREADY_USED",u[u.ERR_BOX_COUNT_OVER=13]="ERR_BOX_COUNT_OVER",u[u.ERR_BOX_CLIENT_OVER=14]="ERR_BOX_CLIENT_OVER",u[u.ERR_UNSUPPORTED=15]="ERR_UNSUPPORTED",u[u.ERR_DEVICE_BUSY=16]="ERR_DEVICE_BUSY",u[u.ERR_RECOVERY_FAILURE=17]="ERR_RECOVERY_FAILURE",u[u.ERR_FAILURE=255]="ERR_FAILURE",u))(ue||{}),ce=(d=>(d[d.SUCCESS=0]="SUCCESS",d[d.ERR_TIMEOUT=1]="ERR_TIMEOUT",d[d.ERR_NOT_FOUND=2]="ERR_NOT_FOUND",d[d.ERR_AUTORECOVER=3]="ERR_AUTORECOVER",d[d.ERR_COVER_OPEN=4]="ERR_COVER_OPEN",d[d.ERR_CUTTER=5]="ERR_CUTTER",d[d.ERR_MECHANICAL=6]="ERR_MECHANICAL",d[d.ERR_EMPTY=7]="ERR_EMPTY",d[d.ERR_UNRECOVERABLE=8]="ERR_UNRECOVERABLE",d[d.ERR_SYSTEM=9]="ERR_SYSTEM",d[d.ERR_PORT=10]="ERR_PORT",d[d.ERR_FAILURE=255]="ERR_FAILURE",d))(ce||{}),W=(l=>(l[l.EN=0]="EN",l[l.JA=1]="JA",l[l.ZH_CN=2]="ZH_CN",l[l.ZH_TW=3]="ZH_TW",l[l.KO=4]="KO",l[l.TH=5]="TH",l[l.VI=6]="VI",l[l.MULTI=7]="MULTI",l))(W||{}),le=(o=>(o[o.FONT_A=0]="FONT_A",o[o.FONT_B=1]="FONT_B",o[o.FONT_C=2]="FONT_C",o[o.FONT_D=3]="FONT_D",o[o.FONT_E=4]="FONT_E",o))(le||{}),k=(s=>(s[s.TM_M10=0]="TM_M10",s[s.TM_M30=1]="TM_M30",s[s.TM_P20=2]="TM_P20",s[s.TM_P60=3]="TM_P60",s[s.TM_P60II=4]="TM_P60II",s[s.TM_P80=5]="TM_P80",s[s.TM_T20=6]="TM_T20",s[s.TM_T60=7]="TM_T60",s[s.TM_T70=8]="TM_T70",s[s.TM_T81=9]="TM_T81",s[s.TM_T82=10]="TM_T82",s[s.TM_T83=11]="TM_T83",s[s.TM_T88=12]="TM_T88",s[s.TM_T90=13]="TM_T90",s[s.TM_T90KP=14]="TM_T90KP",s[s.TM_U220=15]="TM_U220",s[s.TM_U330=16]="TM_U330",s[s.TM_L90=17]="TM_L90",s[s.TM_H6000=18]="TM_H6000",s[s.TM_T83III=19]="TM_T83III",s[s.TM_T100=20]="TM_T100",s[s.TM_M30II=21]="TM_M30II",s[s.TM_M50=23]="TM_M50",s[s.TM_T88VII=24]="TM_T88VII",s[s.TM_L90LFC=25]="TM_L90LFC",s[s.TM_L100=26]="TM_L100",s[s.TM_P20II=27]="TM_P20II",s[s.TM_P80II=28]="TM_P80II",s[s.TM_M30III=29]="TM_M30III",s[s.TM_M50II=30]="TM_M50II",s[s.TM_M55=31]="TM_M55",s[s.TM_U220II=32]="TM_U220II",s))(k||{});var R=class t extends h{constructor(r,n,i,o,T){super(n,i,o,T);this.isHardwareError=r;this.name="PrinterError",Object.setPrototypeOf(this,t.prototype)}},A=t=>{if(t instanceof R)return t.isHardwareError;let e=String(t.message||t).toUpperCase();return e.includes("ERR_OFF_LINE")||e.includes("ERR_COVER_OPEN")||e.includes("ERR_PAPER_OUT")||e.includes("ERR_FAILURE")||e.includes("DISCONNECT")||e.includes("PRINT_TIMEOUT")||e.includes("ERROR CODE: 4")||e.includes("ERROR CODE: 7")||e.includes("ERROR CODE: 3")},K=t=>{if(t instanceof R)return t;let e=C(t),r=A(t);return new R(r,e.code,e.translationCode,e.message,e.originalError)};import{NativeEventEmitter as de,NativeModules as me}from"react-native";var m=me.MunchiEpsonModule,L=()=>m?new de(m):null,ge=t=>new Promise(e=>setTimeout(e,t)),Te=t=>{if(!t||typeof t!="object")return"";let e=t.code;return typeof e=="string"?e.toUpperCase():""},he=t=>{if(t instanceof Error)return t.message.toUpperCase();if(t&&typeof t=="object"){let e=t.message;if(typeof e=="string")return e.toUpperCase()}return String(t).toUpperCase()},f=(t,e,r)=>t.includes(r)||e===r||e.includes(r),P=async(t,e=3,r=500)=>{try{return await t()}catch(n){if(e<=1)throw n;let i=he(n),o=Te(n);if(f(i,o,"TARGET_IN_USE"))throw n;if(f(i,o,"BUSY")||f(i,o,"IN_USE")||f(i,o,"ERR_IN_USE")||f(i,o,"PRINT_BUSY")||f(i,o,"ERR_CONNECT")||f(i,o,"CONNECT_ERROR")||f(i,o,"ERR_TIMEOUT")||f(i,o,"PRINT_TIMEOUT"))return await ge(r),P(t,e-1,r);throw n}};var Ee=new Set([0,3,5,21]),y=class{constructor(e){this.config=e}statusSubscription=null;connectionSubscription=null;connectionCallbacks=new Set;emitStatus(e){for(let r of this.connectionCallbacks)r(e)}ensureStatusListener(){if(this.statusSubscription)return;let e=L();e&&(this.config.logger?.info?.("[EpsonPrinter] Starting status listener for queue management"),this.statusSubscription=e.addListener("onPrinterStatusChange",r=>{if(!this.config.getSessionId()||r.sessionId!==this.config.getSessionId())return;let n=`[EpsonPrinter] Received Status Event: ${r.eventType}`;this.config.logger?.info?.(n),this.config.onEvent({connectionStatus:null,statusEventType:r.eventType,isRecoveryEvent:Ee.has(r.eventType)})}))}ensureConnectionListener(){if(this.connectionSubscription)return;let e=L();e&&(this.connectionSubscription=e.addListener("onPrinterConnectionChange",r=>{if(!this.config.getSessionId()||r.sessionId!==this.config.getSessionId())return;let n="UNKNOWN";switch(r.status){case"RECONNECTING":n="RECONNECTING";break;case"RECONNECTED":n="CONNECTED";break;case"DISCONNECTED":n="DISCONNECTED";break;default:n="UNKNOWN"}this.config.onEvent({connectionStatus:n,statusEventType:null,isRecoveryEvent:!1}),this.emitStatus(n)}))}removeStatusListener(){this.statusSubscription&&(this.statusSubscription.remove(),this.statusSubscription=null)}onConnectionChange(e){return this.connectionCallbacks.add(e),e(this.config.getConnectionStatus()),this.ensureConnectionListener(),()=>{this.connectionCallbacks.delete(e),this.connectionCallbacks.size===0&&this.connectionSubscription&&(this.connectionSubscription.remove(),this.connectionSubscription=null)}}};var U=(n=>(n.Small="Small",n.Medium="Medium",n.Large="Large",n))(U||{});var Ce=(t,e=!1)=>{let r={Small:{normal:{width:1,height:1},item:{width:1,height:1}},Medium:{normal:{width:2,height:2},item:{width:2,height:2}},Large:{normal:{width:2,height:2},item:{width:3,height:3}}},n=e?"item":"normal";return r[t]?.[n]??r.Small.normal},X=t=>{if(typeof t!="object"||t===null)return null;let e=t;return{target:String(e.target||""),name:String(e.name||""),macAddress:e.macAddress?String(e.macAddress):void 0,ipAddress:e.ipAddress?String(e.ipAddress):void 0,bdAddress:e.bdAddress?String(e.bdAddress):void 0,type:"EPSON"}},$=t=>t.commands.map(e=>{switch(e.type){case"text":return{cmd:"addText",data:e.text,align:e.align,bold:e.bold,underline:e.underline,size:e.size};case"align":return{cmd:"addTextAlign",align:e.align};case"bold":return{cmd:"addTextStyle",bold:e.value};case"fontSize":{let r=Ce(e.size,e.isItemName);return{cmd:"addTextSize",width:r.width,height:r.height}}case"newline":return{cmd:"addFeedLine",line:e.lines||1};case"separator":return{cmd:"addText",data:`------------------
1
+ var G={},q=t=>{G.logger=t},z=()=>G.logger;import{Platform as j}from"react-native";var _=(T=>(T.CONNECTION_FAILED="CONNECTION_FAILED",T.PRINT_FAILED="PRINT_FAILED",T.DISCOVERY_FAILED="DISCOVERY_FAILED",T.TIMEOUT="TIMEOUT",T.NATIVE_MODULE_MISSING="NATIVE_MODULE_MISSING",T.UNKNOWN="UNKNOWN",T))(_||{}),O=(c=>(c.ERR_PRINTER_RECONNECT_REQUIRED="printer.error.reconnect_required",c.ERR_PRINTER_OFFLINE="printer.error.offline",c.ERR_PRINTER_BUSY="printer.error.busy",c.ERR_PRINTER_COVER_OPEN="printer.error.cover_open",c.ERR_PRINTER_PAPER_EMPTY="printer.error.paper_empty",c.ERR_CONNECTION_TIMEOUT="printer.error.connection_timeout",c.ERR_UNKNOWN="printer.error.unknown",c))(O||{}),h=class t extends Error{constructor(r,n,i,o){super(i);this.code=r;this.translationCode=n;this.originalError=o;this.name="MunchiPrinterError",Object.setPrototypeOf(this,t.prototype)}};var se=t=>{if(!t||typeof t!="object")return"";let e=t.code;return typeof e=="string"?e:""},oe=t=>{if(t instanceof Error)return t.message;if(t&&typeof t=="object"){let e=t.message;if(typeof e=="string")return e}return String(t)},d=(t,e,r)=>t.includes(r)||e===r||e.includes(r),C=t=>{if(t instanceof h)return t;let e=oe(t),r=se(t),n=e.toUpperCase(),i=r.toUpperCase();return n.includes("ERROR CODE: 4")?new h("PRINT_FAILED","printer.error.cover_open","Printer cover is open",t):n.includes("ERROR CODE: 3")?new h("PRINT_FAILED","printer.error.busy","Printer is performing auto-recovery",t):n.includes("ERROR CODE: 7")?new h("PRINT_FAILED","printer.error.paper_empty","Printer paper is empty",t):d(n,i,"TARGET_IN_USE")||d(n,i,"BUSY")||d(n,i,"DEVICE_BUSY")||d(n,i,"IN_USE")||d(n,i,"ERR_IN_USE")||d(n,i,"PROCESSING")||d(n,i,"ERR_PROCESSING")||d(n,i,"PRINT_BUSY")||d(n,i,"PRINTING")||d(n,i,"DISCOVERY_BUSY")?new h("TIMEOUT","printer.error.busy","Printer is busy",t):d(n,i,"TIMEOUT")||d(n,i,"ERR_TIMEOUT")||d(n,i,"PRINT_TIMEOUT")?new h("TIMEOUT","printer.error.connection_timeout","Operation timed out",t):d(n,i,"PRINTER_RECONNECT_REQUIRED")||n.includes("RECONNECT THE PRINTER")?new h("CONNECTION_FAILED","printer.error.reconnect_required",e,t):d(n,i,"PERMISSION_DENIED")||d(n,i,"LOCATION_DISABLED")||d(n,i,"BLUETOOTH_DISABLED")?new h("CONNECTION_FAILED","printer.error.offline",e,t):d(n,i,"DISCOVERY_ERROR")||n.includes("DISCOVERY")?new h("DISCOVERY_FAILED","printer.error.unknown",e,t):d(n,i,"PRINTER_OFFLINE")||d(n,i,"SESSION_ERROR")||d(n,i,"STATUS_ERROR")||d(n,i,"INIT_ERROR")||d(n,i,"PRINT_CANCELED")||d(n,i,"DISCONNECT")||d(n,i,"ERR_DISCONNECT")||n.includes("PRINTER NOT CONNECTED")||n.includes("OFFLINE")?new h("CONNECTION_FAILED","printer.error.offline",n.includes("OFFLINE")?e:"Printer disconnected",t):d(n,i,"CONNECT_ERROR")||n.includes("FAILED TO CONNECT")||d(n,i,"ERR_CONNECT")?new h("CONNECTION_FAILED","printer.error.offline","Failed to connect to printer",t):d(n,i,"PRINT_ERROR")||d(n,i,"PRINT_FAILURE")?new h("PRINT_FAILED","printer.error.unknown",e,t):new h("UNKNOWN","printer.error.unknown",e,t)};var Q=(a=>(a[a.ONLINE=0]="ONLINE",a[a.OFFLINE=1]="OFFLINE",a[a.POWER_OFF=2]="POWER_OFF",a[a.COVER_CLOSE=3]="COVER_CLOSE",a[a.COVER_OPEN=4]="COVER_OPEN",a[a.PAPER_OK=5]="PAPER_OK",a[a.PAPER_NEAR_END=6]="PAPER_NEAR_END",a[a.PAPER_EMPTY=7]="PAPER_EMPTY",a[a.DRAWER_HIGH=8]="DRAWER_HIGH",a[a.DRAWER_LOW=9]="DRAWER_LOW",a[a.BATTERY_ENOUGH=10]="BATTERY_ENOUGH",a[a.BATTERY_EMPTY=11]="BATTERY_EMPTY",a[a.AUTO_RECOVER_ERROR=20]="AUTO_RECOVER_ERROR",a[a.AUTO_RECOVER_OK=21]="AUTO_RECOVER_OK",a[a.UNRECOVERABLE_ERROR=22]="UNRECOVERABLE_ERROR",a))(Q||{}),ae=(n=>(n[n.RECONNECTING=0]="RECONNECTING",n[n.RECONNECT=1]="RECONNECT",n[n.DISCONNECT=2]="DISCONNECT",n))(ae||{}),ue=(u=>(u[u.SUCCESS=0]="SUCCESS",u[u.ERR_PARAM=1]="ERR_PARAM",u[u.ERR_CONNECT=2]="ERR_CONNECT",u[u.ERR_TIMEOUT=3]="ERR_TIMEOUT",u[u.ERR_MEMORY=4]="ERR_MEMORY",u[u.ERR_ILLEGAL=5]="ERR_ILLEGAL",u[u.ERR_PROCESSING=6]="ERR_PROCESSING",u[u.ERR_NOT_FOUND=7]="ERR_NOT_FOUND",u[u.ERR_IN_USE=8]="ERR_IN_USE",u[u.ERR_TYPE_INVALID=9]="ERR_TYPE_INVALID",u[u.ERR_DISCONNECT=10]="ERR_DISCONNECT",u[u.ERR_ALREADY_OPENED=11]="ERR_ALREADY_OPENED",u[u.ERR_ALREADY_USED=12]="ERR_ALREADY_USED",u[u.ERR_BOX_COUNT_OVER=13]="ERR_BOX_COUNT_OVER",u[u.ERR_BOX_CLIENT_OVER=14]="ERR_BOX_CLIENT_OVER",u[u.ERR_UNSUPPORTED=15]="ERR_UNSUPPORTED",u[u.ERR_DEVICE_BUSY=16]="ERR_DEVICE_BUSY",u[u.ERR_RECOVERY_FAILURE=17]="ERR_RECOVERY_FAILURE",u[u.ERR_FAILURE=255]="ERR_FAILURE",u))(ue||{}),ce=(m=>(m[m.SUCCESS=0]="SUCCESS",m[m.ERR_TIMEOUT=1]="ERR_TIMEOUT",m[m.ERR_NOT_FOUND=2]="ERR_NOT_FOUND",m[m.ERR_AUTORECOVER=3]="ERR_AUTORECOVER",m[m.ERR_COVER_OPEN=4]="ERR_COVER_OPEN",m[m.ERR_CUTTER=5]="ERR_CUTTER",m[m.ERR_MECHANICAL=6]="ERR_MECHANICAL",m[m.ERR_EMPTY=7]="ERR_EMPTY",m[m.ERR_UNRECOVERABLE=8]="ERR_UNRECOVERABLE",m[m.ERR_SYSTEM=9]="ERR_SYSTEM",m[m.ERR_PORT=10]="ERR_PORT",m[m.ERR_FAILURE=255]="ERR_FAILURE",m))(ce||{}),W=(l=>(l[l.EN=0]="EN",l[l.JA=1]="JA",l[l.ZH_CN=2]="ZH_CN",l[l.ZH_TW=3]="ZH_TW",l[l.KO=4]="KO",l[l.TH=5]="TH",l[l.VI=6]="VI",l[l.MULTI=7]="MULTI",l))(W||{}),le=(o=>(o[o.FONT_A=0]="FONT_A",o[o.FONT_B=1]="FONT_B",o[o.FONT_C=2]="FONT_C",o[o.FONT_D=3]="FONT_D",o[o.FONT_E=4]="FONT_E",o))(le||{}),k=(s=>(s[s.TM_M10=0]="TM_M10",s[s.TM_M30=1]="TM_M30",s[s.TM_P20=2]="TM_P20",s[s.TM_P60=3]="TM_P60",s[s.TM_P60II=4]="TM_P60II",s[s.TM_P80=5]="TM_P80",s[s.TM_T20=6]="TM_T20",s[s.TM_T60=7]="TM_T60",s[s.TM_T70=8]="TM_T70",s[s.TM_T81=9]="TM_T81",s[s.TM_T82=10]="TM_T82",s[s.TM_T83=11]="TM_T83",s[s.TM_T88=12]="TM_T88",s[s.TM_T90=13]="TM_T90",s[s.TM_T90KP=14]="TM_T90KP",s[s.TM_U220=15]="TM_U220",s[s.TM_U330=16]="TM_U330",s[s.TM_L90=17]="TM_L90",s[s.TM_H6000=18]="TM_H6000",s[s.TM_T83III=19]="TM_T83III",s[s.TM_T100=20]="TM_T100",s[s.TM_M30II=21]="TM_M30II",s[s.TM_M50=23]="TM_M50",s[s.TM_T88VII=24]="TM_T88VII",s[s.TM_L90LFC=25]="TM_L90LFC",s[s.TM_L100=26]="TM_L100",s[s.TM_P20II=27]="TM_P20II",s[s.TM_P80II=28]="TM_P80II",s[s.TM_M30III=29]="TM_M30III",s[s.TM_M50II=30]="TM_M50II",s[s.TM_M55=31]="TM_M55",s[s.TM_U220II=32]="TM_U220II",s))(k||{});var f=class t extends h{constructor(r,n,i,o,T){super(n,i,o,T);this.isHardwareError=r;this.name="PrinterError",Object.setPrototypeOf(this,t.prototype)}},A=t=>{if(t instanceof f)return t.isHardwareError;let e=String(t.message||t).toUpperCase();return e.includes("ERR_OFF_LINE")||e.includes("ERR_COVER_OPEN")||e.includes("ERR_PAPER_OUT")||e.includes("ERR_FAILURE")||e.includes("DISCONNECT")||e.includes("PRINT_TIMEOUT")||e.includes("ERROR CODE: 4")||e.includes("ERROR CODE: 7")||e.includes("ERROR CODE: 3")},K=t=>{if(t instanceof f)return t;let e=C(t),r=A(t);return new f(r,e.code,e.translationCode,e.message,e.originalError)};import{NativeEventEmitter as de,NativeModules as me}from"react-native";var g=me.MunchiEpsonModule,L=()=>g?new de(g):null,ge=t=>new Promise(e=>setTimeout(e,t)),Te=t=>{if(!t||typeof t!="object")return"";let e=t.code;return typeof e=="string"?e.toUpperCase():""},he=t=>{if(t instanceof Error)return t.message.toUpperCase();if(t&&typeof t=="object"){let e=t.message;if(typeof e=="string")return e.toUpperCase()}return String(t).toUpperCase()},R=(t,e,r)=>t.includes(r)||e===r||e.includes(r),P=async(t,e=3,r=500)=>{try{return await t()}catch(n){if(e<=1)throw n;let i=he(n),o=Te(n);if(R(i,o,"TARGET_IN_USE"))throw n;if(R(i,o,"BUSY")||R(i,o,"IN_USE")||R(i,o,"ERR_IN_USE")||R(i,o,"PRINT_BUSY")||R(i,o,"ERR_CONNECT")||R(i,o,"CONNECT_ERROR")||R(i,o,"ERR_TIMEOUT")||R(i,o,"PRINT_TIMEOUT"))return await ge(r),P(t,e-1,r);throw n}};var Ee=new Set([0,3,5,21]),y=class{constructor(e){this.config=e}statusSubscription=null;connectionSubscription=null;connectionCallbacks=new Set;emitStatus(e){for(let r of this.connectionCallbacks)r(e)}ensureStatusListener(){if(this.statusSubscription)return;let e=L();e&&(this.config.logger?.info?.("[EpsonPrinter] Starting status listener for queue management"),this.statusSubscription=e.addListener("onPrinterStatusChange",r=>{if(!this.config.getSessionId()||r.sessionId!==this.config.getSessionId())return;let n=`[EpsonPrinter] Received Status Event: ${r.eventType}`;this.config.logger?.info?.(n),this.config.onEvent({connectionStatus:null,statusEventType:r.eventType,isRecoveryEvent:Ee.has(r.eventType)})}))}ensureConnectionListener(){if(this.connectionSubscription)return;let e=L();e&&(this.connectionSubscription=e.addListener("onPrinterConnectionChange",r=>{if(!this.config.getSessionId()||r.sessionId!==this.config.getSessionId())return;let n="UNKNOWN";switch(r.status){case"RECONNECTING":n="RECONNECTING";break;case"RECONNECTED":n="CONNECTED";break;case"DISCONNECTED":n="DISCONNECTED";break;default:n="UNKNOWN"}this.config.onEvent({connectionStatus:n,statusEventType:null,isRecoveryEvent:!1}),this.emitStatus(n)}))}removeStatusListener(){this.statusSubscription&&(this.statusSubscription.remove(),this.statusSubscription=null)}onConnectionChange(e){return this.connectionCallbacks.add(e),e(this.config.getConnectionStatus()),this.ensureConnectionListener(),()=>{this.connectionCallbacks.delete(e),this.connectionCallbacks.size===0&&this.connectionSubscription&&(this.connectionSubscription.remove(),this.connectionSubscription=null)}}};var U=(n=>(n.Small="Small",n.Medium="Medium",n.Large="Large",n))(U||{});var Ce=(t,e=!1)=>{let r={Small:{normal:{width:1,height:1},item:{width:1,height:1}},Medium:{normal:{width:2,height:2},item:{width:2,height:2}},Large:{normal:{width:2,height:2},item:{width:3,height:3}}},n=e?"item":"normal";return r[t]?.[n]??r.Small.normal},X=t=>{if(typeof t!="object"||t===null)return null;let e=t;return{target:String(e.target||""),name:String(e.name||""),macAddress:e.macAddress?String(e.macAddress):void 0,ipAddress:e.ipAddress?String(e.ipAddress):void 0,bdAddress:e.bdAddress?String(e.bdAddress):void 0,type:"EPSON"}},$=t=>t.commands.map(e=>{switch(e.type){case"text":return{cmd:"addText",data:e.text,align:e.align,bold:e.bold,underline:e.underline,size:e.size};case"align":return{cmd:"addTextAlign",align:e.align};case"bold":return{cmd:"addTextStyle",bold:e.value};case"fontSize":{let r=Ce(e.size,e.isItemName);return{cmd:"addTextSize",width:r.width,height:r.height}}case"newline":return{cmd:"addFeedLine",line:e.lines||1};case"separator":return{cmd:"addText",data:`------------------
2
2
  `};case"textLang":return{cmd:"addTextLang",lang:e.lang};case"textFont":return{cmd:"addTextFont",font:e.font};case"image":return{cmd:"addImage",data:e.base64,width:e.width};case"cut":return{cmd:"addCut"};case"drawer":return{cmd:"addPulse"};case"barcode":return{cmd:"addBarcode",data:e.data,type:e.system,width:e.width,height:e.height};case"qr":return{cmd:"addSymbol",data:e.data,type:"QRCODE_MODEL_2",level:e.errorCorrection||"M",width:e.size||3};case"feed":return{cmd:"addFeedLine",line:e.lines};default:return null}}).filter(e=>e!==null);var J=t=>{let e=[];if(t.settings?.fontSize){let r="Medium";t.settings.fontSize==="Small"&&(r="Small"),t.settings.fontSize==="Large"&&(r="Large"),e.push({type:"fontSize",size:r})}t.settings?.fontStyle==="Bold"&&e.push({type:"bold",value:!0}),e.push({type:"align",align:"center"}),t.tbl&&e.push({type:"text",text:`Table: ${t.tbl}
3
3
  `,bold:!0,size:"double-height"}),e.push({type:"text",text:`Order: ${t.id}
4
4
  `,bold:!0}),e.push({type:"text",text:`${t.ts}
@@ -8,4 +8,4 @@ var q={},z=t=>{q.logger=t},G=()=>q.logger;import{Platform as j}from"react-native
8
8
  `,align:"center",bold:!0})}else i==="B"?e.push({type:"text",text:c+`
9
9
  `,bold:!0}):i==="R"?e.push({type:"text",text:c+`
10
10
  `,bold:!0}):e.push({type:"text",text:c+`
11
- `,align:"left"})}return e.push({type:"feed",lines:3}),e.push({type:"cut"}),{commands:e}};var v=class{constructor(e){this.logger=e}queue=Promise.resolve();isQueuePaused=!1;resumeQueueResolver=null;isPaused(){return this.isQueuePaused}pause(){this.isQueuePaused=!0}resume(){this.isQueuePaused&&(this.isQueuePaused=!1,this.resumeQueueResolver&&(this.resumeQueueResolver(),this.resumeQueueResolver=null))}async waitUntilResumed(){if(this.isQueuePaused)return new Promise(e=>{let r=this.resumeQueueResolver;this.resumeQueueResolver=()=>{r&&r(),e()}})}enqueue(e){return new Promise((r,n)=>{let i=async()=>{this.isQueuePaused&&(this.logger?.info?.("[EpsonPrinter] Queue is PAUSED. Waiting for resume..."),await this.waitUntilResumed(),this.logger?.info?.("[EpsonPrinter] Queue RESUMED."));try{let o=await e();r(o)}catch(o){n(o)}};this.queue=this.queue.then(i,i)})}enqueueBackground(e){this.queue=this.queue.then(e,e)}};var D=class{constructor(e){this.config=e}recoveryTimer=null;start(){this.recoveryTimer||(this.config.logger?.info?.("[EpsonPrinter] Starting recovery polling..."),this.recoveryTimer=setInterval(async()=>{try{let e=await this.config.getStatus();if(e.online&&!e.coverOpen&&e.errorStatus==="NONE")this.config.logger?.info?.("[EpsonPrinter] Poller detected healthy status. Requesting queue resume."),this.config.onRecovered();else{let n=`[EpsonPrinter] Polling... Status: Online=${e.online}, Cover=${e.coverOpen}, Err=${e.errorStatus}`;this.config.logger?.info?.(n)}}catch(e){this.config.logger?.error("[EpsonPrinter] Recovery poll failed",e)}},2e3))}stop(){this.recoveryTimer&&(this.config.logger?.info?.("[EpsonPrinter] Stopping recovery polling."),clearInterval(this.recoveryTimer),this.recoveryTimer=null)}};var M=class{constructor(e){this.logger=e}sessionId=null;sessionInitPromise=null;getSessionId(){return this.sessionId}async ensureSession(){if(!m)throw new Error("Native module not found");return this.sessionId?this.sessionId:this.sessionInitPromise?this.sessionInitPromise:(this.sessionInitPromise=m.initSession().then(e=>(this.sessionId=e,e)).finally(()=>{this.sessionInitPromise=null}),this.sessionInitPromise)}async disposeSession(){if(!m||!this.sessionId){this.sessionId=null;return}let e=this.sessionId;this.sessionId=null;try{await m.disposeSession(e)}catch(r){this.logger?.error("[EpsonPrinter] Dispose session failed",r)}}};var w=class{constructor(e){this.config=e}statusCallbacks=new Set;latestHardwareStatus=null;statusRefreshPromise=null;statusRefreshTimer=null;subscribe(e){return this.statusCallbacks.add(e),this.latestHardwareStatus?e(this.latestHardwareStatus):this.config.getConnectionStatus()!=="CONNECTED"?e(this.config.defaultStatus):this.refreshNow(),()=>{this.statusCallbacks.delete(e)}}handleStatusEvent(){this.scheduleRefresh()}handleConnected(){this.scheduleRefresh(0)}handleDisconnected(){this.clearRefreshTimer(),this.setHardwareStatus(this.config.defaultStatus)}clear(){this.clearRefreshTimer()}async refreshNow(){if(this.config.getConnectionStatus()!=="CONNECTED"){this.setHardwareStatus(this.config.defaultStatus);return}if(this.statusRefreshPromise){await this.statusRefreshPromise;return}this.statusRefreshPromise=(async()=>{try{let e=await this.config.getStatus();this.setHardwareStatus(e)}catch(e){this.config.logger?.error("[EpsonPrinter] Status refresh failed",e),this.latestHardwareStatus||this.setHardwareStatus(this.config.defaultStatus)}})().finally(()=>{this.statusRefreshPromise=null}),await this.statusRefreshPromise}emitHardwareStatus(e){for(let r of this.statusCallbacks)r(e)}setHardwareStatus(e){this.latestHardwareStatus=e,this.emitHardwareStatus(e)}clearRefreshTimer(){this.statusRefreshTimer&&(clearTimeout(this.statusRefreshTimer),this.statusRefreshTimer=null)}scheduleRefresh(e=150){this.config.getConnectionStatus()==="CONNECTED"&&(this.statusRefreshTimer||(this.statusRefreshTimer=setTimeout(()=>{this.statusRefreshTimer=null,this.refreshNow()},e)))}};var F={online:!1,coverOpen:!1,paperEmpty:!1,paper:"EMPTY",errorStatus:"NONE",drawerOpen:!1},fe=5e3,b=class{disconnectTimer=null;connectionStatus="DISCONNECTED";isConnecting=!1;target=null;defaultTarget;model;lang;logger;sessionManager;queueController;recoveryController;statusController;eventRouter;constructor(e){this.logger=e.logger,this.defaultTarget=e.target??null,this.model=e.model,this.lang=e.lang??0,this.sessionManager=new M(this.logger),this.queueController=new v(this.logger),this.recoveryController=new D({logger:this.logger,getStatus:()=>this.getStatus(),onRecovered:()=>this.resumeQueue()}),this.statusController=new w({logger:this.logger,defaultStatus:F,getConnectionStatus:()=>this.connectionStatus,getStatus:()=>this.getStatus()}),this.eventRouter=new y({logger:this.logger,getSessionId:()=>this.sessionManager.getSessionId(),getConnectionStatus:()=>this.connectionStatus,onEvent:r=>{r.statusEventType!==null&&(this.statusController.handleStatusEvent(),r.isRecoveryEvent&&this.queueController.isPaused()&&(this.logger?.info?.("[EpsonPrinter] Printer recovered (Event). Resuming queue..."),this.resumeQueue())),r.connectionStatus!==null&&(r.connectionStatus==="CONNECTED"&&(this.connectionStatus="CONNECTED"),r.connectionStatus==="CONNECTED"&&this.statusController.handleConnected(),r.connectionStatus==="DISCONNECTED"&&(this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected()))}})}resumeQueue(){if(!this.queueController.isPaused())return;this.logger?.info?.("[EpsonPrinter] Resuming queue..."),this.recoveryController.stop(),this.queueController.resume()}isIosConnectionRecoveryCandidate(e){return j.OS!=="ios"?!1:e.code==="CONNECTION_FAILED"||e.code==="TIMEOUT"&&e.translationCode==="printer.error.connection_timeout"||e.translationCode==="printer.error.offline"}hasTargetInUseToken(e){if(!e||typeof e!="object")return!1;let r=String(e.code??"").toUpperCase(),n=String(e.message??"").toUpperCase();return r.includes("TARGET_IN_USE")||n.includes("TARGET_IN_USE")}isIosConnectBusyRecoveryCandidate(e){return j.OS!=="ios"||e.translationCode!=="printer.error.busy"?!1:!this.hasTargetInUseToken(e.originalError)}async recoverIosSessionForPrint(){if(!m)throw new Error("Native module not found");let e=this.target??this.defaultTarget;if(!e)throw new Error("Printer target is required");let r=this.sessionManager.getSessionId();if(r)try{await m.disconnect(r)}catch(i){this.logger?.error("[EpsonPrinter] iOS recovery disconnect failed (best-effort)",i)}await this.sessionManager.disposeSession(),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED");let n=await this.sessionManager.ensureSession();return await P(()=>m.connect(n,e,fe,this.model,this.lang)),this.target=e,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected(),n}async recoverIosSessionForConnect(e,r){if(!m)throw new Error("Native module not found");let n=this.sessionManager.getSessionId();if(n)try{await m.disconnect(n)}catch(o){this.logger?.error("[EpsonPrinter] iOS connect recovery disconnect failed (best-effort)",o)}await this.sessionManager.disposeSession(),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED");let i=await this.sessionManager.ensureSession();await P(()=>m.connect(i,e,r,this.model,this.lang)),this.target=e,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected()}async connect(e,r=5e3){let n=e??this.defaultTarget;if(!n){let i=new Error("Printer target is required");throw this.logger?.error("[EpsonPrinter] Connect failed: missing target",i),i}return this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.queueController.enqueue(async()=>{if(!m){this.logger?.error("[EpsonPrinter] Native module not found");return}if(this.connectionStatus==="CONNECTED"&&this.target===n)return;let i=await this.sessionManager.ensureSession();this.isConnecting=!0,this.connectionStatus="CONNECTING",this.eventRouter.emitStatus("CONNECTING");let o=!1;try{await P(()=>m.connect(i,n,r,this.model,this.lang)),this.target=n,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.isConnecting=!1,this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected()}catch(T){let c=C(T);if(!o&&this.isIosConnectBusyRecoveryCandidate(c)){o=!0,this.logger?.error("[EpsonPrinter] iOS busy connect suspected stale session. Recovering connection and retrying connect once...",c);try{await this.recoverIosSessionForConnect(n,r),this.logger?.info?.("[EpsonPrinter] iOS connect recovery succeeded."),this.isConnecting=!1;return}catch(l){let p=C(l);throw this.logger?.error("[EpsonPrinter] iOS connect recovery failed",p),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,p}}throw this.logger?.error(`[EpsonPrinter] Connect failed for ${n}`,c),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,c}})}async print(e){if(!m)return;this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null);let r=0,n=!1;return this.queueController.enqueue(async()=>{let i=await this.sessionManager.ensureSession(),o=$(e);for(;;){r++,this.logger?.info?.(`[EpsonPrinter] Print attempt ${r}`);try{await P(()=>m.print(i,o));break}catch(T){let c=K(T);if(!n&&this.isIosConnectionRecoveryCandidate(c)){n=!0,this.logger?.error("[EpsonPrinter] iOS stale session suspected. Recovering connection and retrying print once...",c);try{i=await this.recoverIosSessionForPrint(),this.logger?.info?.("[EpsonPrinter] iOS session recovery succeeded. Retrying print...");continue}catch(l){let p=C(l);throw this.logger?.error("[EpsonPrinter] iOS session recovery failed",p),p}}if(c.isHardwareError){this.queueController.pause(),this.logger?.error(`[EpsonPrinter] Hardware error on attempt ${r}. Waiting for recovery before retry...`,c),this.recoveryController.start(),await this.queueController.waitUntilResumed(),this.logger?.info?.("[EpsonPrinter] Recovery detected. Sending cut to flush partial print before retry...");try{await m.print(i,[{cmd:"addCut"}])}catch{}this.logger?.info?.("[EpsonPrinter] Retrying print job...")}else throw this.logger?.error(`[EpsonPrinter] Print failed (non-recoverable) on attempt ${r}`,c),c}}}).finally(()=>{this.disconnectTimer&&clearTimeout(this.disconnectTimer),this.disconnectTimer=setTimeout(()=>{this.performDisconnect()},5e3)})}performDisconnect(){this.queueController.enqueueBackground(async()=>{if(!this.disconnectTimer)return;let e=this.sessionManager.getSessionId();if(m&&this.connectionStatus==="CONNECTED"&&e)try{await m.disconnect(e),this.connectionStatus="DISCONNECTED",this.target=null,this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),await this.sessionManager.disposeSession()}catch(r){this.logger?.error("[EpsonPrinter] Auto-Disconnect failed",r)}this.disconnectTimer=null})}async printEmbedded(e){try{let r=J(e);return await this.print(r)}catch(r){let n=C(r);throw String(r).includes("MunchiPrinterError")||this.logger?.error("[EpsonPrinter] Embedded print failed",n),n}}async disconnect(){return this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.queueController.enqueue(async()=>{if(!m)return;let e=this.sessionManager.getSessionId();if(e)try{await m.disconnect(e),this.connectionStatus="DISCONNECTED",this.target=null,this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.eventRouter.removeStatusListener(),this.recoveryController.stop()}catch(r){this.logger?.error("[EpsonPrinter] Disconnect failed",r)}finally{this.statusController.clear(),await this.sessionManager.disposeSession()}})}async getStatus(){if(!m)return F;let e=this.sessionManager.getSessionId();if(!e)return F;try{return await m.getStatus(e)}catch(r){throw this.logger?.error("[EpsonPrinter] GetStatus failed",r),C(r)}}async openDrawer(){let e={commands:[{type:"drawer"}]};return this.print(e)}onConnectionChange(e){return this.eventRouter.onConnectionChange(e)}onStatusChange(e){return this.statusController.subscribe(e)}};import{NativeModules as Re}from"react-native";var Z=Re.MunchiEpsonModule,pe={bluetooth:["BT:"],tcp:["TCP:","TCPS:"],usb:["USB:"]},Ne=t=>t.includes("[local_"),Pe=(t,e)=>e?pe[e].some(n=>t.startsWith(n)):!0,Ie=async t=>{if(!Z)return console.warn("[EpsonDiscovery] Native module not found"),[];try{return(await Z.discover({timeout:t.timeout})).map(X).filter(r=>!r||Ne(r.target)?!1:Pe(r.target,t.connectionType))}catch(e){let r=C(e);throw console.error("[EpsonDiscovery] Discovery failed",r),r}};var Se=[[/^TM-m10/i,0],[/^TM-m30III/i,29],[/^TM-m30II/i,21],[/^TM-m30/i,1],[/^TM-m50II/i,30],[/^TM-m50/i,23],[/^TM-m55/i,31],[/^TM-P20II/i,27],[/^TM-P20/i,2],[/^TM-P60II/i,4],[/^TM-P60/i,3],[/^TM-P80II/i,28],[/^TM-P80/i,5],[/^TM-T20/i,6],[/^TM-T60/i,7],[/^TM-T70/i,8],[/^TM-T81/i,9],[/^TM-T82/i,10],[/^TM-T83III/i,19],[/^TM-T83/i,11],[/^TM-T88VII/i,24],[/^TM-T88/i,12],[/^TM-T90KP/i,14],[/^TM-T90/i,13],[/^TM-T100/i,20],[/^TM-U220II/i,32],[/^TM-U220/i,15],[/^TM-U330/i,16],[/^TM-L90LFC/i,25],[/^TM-L90/i,17],[/^TM-L100/i,26],[/^TM-H6000/i,18]],_e=t=>{for(let[e,r]of Se)if(e.test(t))return r;return null};var Oe=t=>{let e=C(t),r=e.message.toUpperCase(),n=t instanceof R?t.isHardwareError:A(t),i=e.translationCode==="printer.error.busy"||r.includes("BUSY")||r.includes("IN_USE")||r.includes("PRINT_BUSY"),o=e.code==="TIMEOUT"||e.translationCode==="printer.error.connection_timeout"||r.includes("TIMEOUT"),T=e.code==="CONNECTION_FAILED"||e.translationCode==="printer.error.offline",c=e.code==="DISCOVERY_FAILED",l="UNKNOWN";return n?l="HARDWARE":i?l="BUSY":o?l="TIMEOUT":T?l="CONNECTION":c?l="DISCOVERY":e.code==="PRINT_FAILED"&&(l="PRINT"),{code:e.code,translationCode:e.translationCode,message:e.message,category:l,isHardwareError:n,retryable:i||o,shouldPauseQueue:n,raw:t}},Ot=t=>Oe(t);import ye from"react";import{createContext as ve,useContext as De,useEffect as Me,useMemo as ee,useState as we}from"react";var te=ve(void 0),bt=({config:t,logger:e,children:r})=>{let[n,i]=we();Me(()=>{e&&z(e)},[e]);let o=ee(()=>{let c={...t,logger:e??t.logger};return ne(c)},[t.lang,t.model,t.target,e]),T=ee(()=>({printer:o,config:t,isReady:!0,error:n}),[o,t,n]);return ye.createElement(te.Provider,{value:T},r)},re=()=>{let t=De(te);if(!t)throw new Error("usePrinter must be used within a PrinterProvider");return t};import{useCallback as B,useEffect as x,useRef as ie,useState as Y}from"react";function Ut(t={}){let{enabled:e=!0}=t,{printer:r,config:n}=re(),[i,o]=Y("DISCONNECTED"),[T,c]=Y(null),[l,p]=Y(null),H=ie("DISCONNECTED"),V=ie(null),d=l!==null,N=B(E=>{V.current=E,p(E)},[]),I=B(E=>{if(E instanceof Error&&E.message){N(E.message);return}if(typeof E=="string"&&E.length>0){N(E);return}N("Unknown printer error")},[N]),a=B(()=>{V.current!==null&&N(null)},[N]);return x(()=>r.onConnectionChange(S=>{H.current=S,o(S),S==="CONNECTED"&&a()}),[a,r]),x(()=>e?r.onStatusChange(S=>{c(S),a()}):void 0,[a,e,r]),x(()=>{e&&i==="CONNECTED"&&r.getStatus().then(E=>{c(E),a()}).catch(I)},[I,a,i,e,r]),x(()=>{let E=H.current;if(e){E==="DISCONNECTED"&&i==="DISCONNECTED"&&r.connect(n.target,5e3).then(a).catch(I);return}(i==="CONNECTED"||i==="CONNECTING")&&r.disconnect().then(a).catch(I)},[I,a,n.target,i,e,r]),{connectionStatus:i,hardwareStatus:T,isError:d,errorMessage:l}}var be="1.0.7";function ne(t){return new b({...t,logger:t.logger??G()})}export{ce as Epos2CallbackCode,ae as Epos2ConnectionEvent,ue as Epos2ErrorStatus,le as Epos2Font,W as Epos2Lang,Q as Epos2StatusEvent,k as EpsonModel,U as FontSize,h as MunchiPrinterError,R as PrinterError,_ as PrinterErrorCode,bt as PrinterProvider,O as PrinterTranslationCode,be as VERSION,Ie as discoverPrinters,G as getGlobalLogger,ne as getPrinter,Ot as resolveEpsonError,_e as resolveModelFromBluetoothName,Oe as resolvePrinterError,z as setGlobalLogger,re as usePrinter,Ut as usePrinterStatus};
11
+ `,align:"left"})}return e.push({type:"feed",lines:3}),e.push({type:"cut"}),{commands:e}};var v=class{constructor(e){this.logger=e}queue=Promise.resolve();isQueuePaused=!1;resumeQueueResolver=null;isPaused(){return this.isQueuePaused}pause(){this.isQueuePaused=!0}resume(){this.isQueuePaused&&(this.isQueuePaused=!1,this.resumeQueueResolver&&(this.resumeQueueResolver(),this.resumeQueueResolver=null))}async waitUntilResumed(){if(this.isQueuePaused)return new Promise(e=>{let r=this.resumeQueueResolver;this.resumeQueueResolver=()=>{r&&r(),e()}})}enqueue(e){return new Promise((r,n)=>{let i=async()=>{this.isQueuePaused&&(this.logger?.info?.("[EpsonPrinter] Queue is PAUSED. Waiting for resume..."),await this.waitUntilResumed(),this.logger?.info?.("[EpsonPrinter] Queue RESUMED."));try{let o=await e();r(o)}catch(o){n(o)}};this.queue=this.queue.then(i,i)})}enqueueBackground(e){this.queue=this.queue.then(e,e)}};var D=class{constructor(e){this.config=e}recoveryTimer=null;start(){this.recoveryTimer||(this.config.logger?.info?.("[EpsonPrinter] Starting recovery polling..."),this.recoveryTimer=setInterval(async()=>{try{let e=await this.config.getStatus();if(e.online&&!e.coverOpen&&e.errorStatus==="NONE")this.config.logger?.info?.("[EpsonPrinter] Poller detected healthy status. Requesting queue resume."),this.config.onRecovered();else{let n=`[EpsonPrinter] Polling... Status: Online=${e.online}, Cover=${e.coverOpen}, Err=${e.errorStatus}`;this.config.logger?.info?.(n)}}catch(e){this.config.logger?.error("[EpsonPrinter] Recovery poll failed",e)}},2e3))}stop(){this.recoveryTimer&&(this.config.logger?.info?.("[EpsonPrinter] Stopping recovery polling."),clearInterval(this.recoveryTimer),this.recoveryTimer=null)}};var M=class{constructor(e){this.logger=e}sessionId=null;sessionInitPromise=null;getSessionId(){return this.sessionId}async ensureSession(){if(!g)throw new Error("Native module not found");return this.sessionId?this.sessionId:this.sessionInitPromise?this.sessionInitPromise:(this.sessionInitPromise=g.initSession().then(e=>(this.sessionId=e,e)).finally(()=>{this.sessionInitPromise=null}),this.sessionInitPromise)}async disposeSession(){if(!g||!this.sessionId){this.sessionId=null;return}let e=this.sessionId;this.sessionId=null;try{await g.disposeSession(e)}catch(r){this.logger?.error("[EpsonPrinter] Dispose session failed",r)}}};var w=class{constructor(e){this.config=e}statusCallbacks=new Set;latestHardwareStatus=null;statusRefreshPromise=null;statusRefreshTimer=null;subscribe(e){return this.statusCallbacks.add(e),this.latestHardwareStatus?e(this.latestHardwareStatus):this.config.getConnectionStatus()!=="CONNECTED"?e(this.config.defaultStatus):this.refreshNow(),()=>{this.statusCallbacks.delete(e)}}handleStatusEvent(){this.scheduleRefresh()}handleConnected(){this.scheduleRefresh(0)}handleDisconnected(){this.clearRefreshTimer(),this.setHardwareStatus(this.config.defaultStatus)}clear(){this.clearRefreshTimer()}async refreshNow(){if(this.config.getConnectionStatus()!=="CONNECTED"){this.setHardwareStatus(this.config.defaultStatus);return}if(this.statusRefreshPromise){await this.statusRefreshPromise;return}this.statusRefreshPromise=(async()=>{try{let e=await this.config.getStatus();this.setHardwareStatus(e)}catch(e){this.config.logger?.error("[EpsonPrinter] Status refresh failed",e),this.latestHardwareStatus||this.setHardwareStatus(this.config.defaultStatus)}})().finally(()=>{this.statusRefreshPromise=null}),await this.statusRefreshPromise}emitHardwareStatus(e){for(let r of this.statusCallbacks)r(e)}setHardwareStatus(e){this.latestHardwareStatus=e,this.emitHardwareStatus(e)}clearRefreshTimer(){this.statusRefreshTimer&&(clearTimeout(this.statusRefreshTimer),this.statusRefreshTimer=null)}scheduleRefresh(e=150){this.config.getConnectionStatus()==="CONNECTED"&&(this.statusRefreshTimer||(this.statusRefreshTimer=setTimeout(()=>{this.statusRefreshTimer=null,this.refreshNow()},e)))}};var F={online:!1,coverOpen:!1,paperEmpty:!1,paper:"EMPTY",errorStatus:"NONE",drawerOpen:!1},Re=5e3,b=class{disconnectTimer=null;connectionStatus="DISCONNECTED";isConnecting=!1;target=null;defaultTarget;model;lang;logger;sessionManager;queueController;recoveryController;statusController;eventRouter;constructor(e){this.logger=e.logger,this.defaultTarget=e.target??null,this.model=e.model,this.lang=e.lang??0,this.sessionManager=new M(this.logger),this.queueController=new v(this.logger),this.recoveryController=new D({logger:this.logger,getStatus:()=>this.getStatus(),onRecovered:()=>this.resumeQueue()}),this.statusController=new w({logger:this.logger,defaultStatus:F,getConnectionStatus:()=>this.connectionStatus,getStatus:()=>this.getStatus()}),this.eventRouter=new y({logger:this.logger,getSessionId:()=>this.sessionManager.getSessionId(),getConnectionStatus:()=>this.connectionStatus,onEvent:r=>{r.statusEventType!==null&&(this.statusController.handleStatusEvent(),r.isRecoveryEvent&&this.queueController.isPaused()&&(this.logger?.info?.("[EpsonPrinter] Printer recovered (Event). Resuming queue..."),this.resumeQueue())),r.connectionStatus!==null&&(r.connectionStatus==="CONNECTED"&&(this.connectionStatus="CONNECTED"),r.connectionStatus==="CONNECTED"&&this.statusController.handleConnected(),r.connectionStatus==="DISCONNECTED"&&(this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected()))}})}resumeQueue(){if(!this.queueController.isPaused())return;this.logger?.info?.("[EpsonPrinter] Resuming queue..."),this.recoveryController.stop(),this.queueController.resume()}isIosConnectionRecoveryCandidate(e){return j.OS!=="ios"?!1:e.code==="CONNECTION_FAILED"||e.code==="TIMEOUT"&&e.translationCode==="printer.error.connection_timeout"||e.translationCode==="printer.error.offline"}hasTargetInUseToken(e){if(!e||typeof e!="object")return!1;let r=String(e.code??"").toUpperCase(),n=String(e.message??"").toUpperCase();return r.includes("TARGET_IN_USE")||n.includes("TARGET_IN_USE")}isIosConnectBusyRecoveryCandidate(e){return j.OS!=="ios"||e.translationCode!=="printer.error.busy"?!1:!this.hasTargetInUseToken(e.originalError)}async recoverIosSessionForPrint(){if(!g)throw new Error("Native module not found");let e=this.target??this.defaultTarget;if(!e)throw new Error("Printer target is required");let r=this.sessionManager.getSessionId();if(r)try{await g.disconnect(r)}catch(i){this.logger?.error("[EpsonPrinter] iOS recovery disconnect failed (best-effort)",i)}await this.sessionManager.disposeSession(),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED");let n=await this.sessionManager.ensureSession();return await P(()=>g.connect(n,e,Re,this.model,this.lang)),this.target=e,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected(),n}async recoverIosSessionForConnect(e,r){if(!g)throw new Error("Native module not found");let n=this.sessionManager.getSessionId();if(n)try{await g.disconnect(n)}catch(o){this.logger?.error("[EpsonPrinter] iOS connect recovery disconnect failed (best-effort)",o)}await this.sessionManager.disposeSession(),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED");let i=await this.sessionManager.ensureSession();await P(()=>g.connect(i,e,r,this.model,this.lang)),this.target=e,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected()}async connect(e,r=5e3){let n=e??this.defaultTarget;if(!n){let i=new Error("Printer target is required");throw this.logger?.error("[EpsonPrinter] Connect failed: missing target",i),i}return this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.queueController.enqueue(async()=>{if(!g){this.logger?.error("[EpsonPrinter] Native module not found");return}if(this.connectionStatus==="CONNECTED"&&this.target===n)return;let i=await this.sessionManager.ensureSession();this.isConnecting=!0,this.connectionStatus="CONNECTING",this.eventRouter.emitStatus("CONNECTING");let o=!1;try{await P(()=>g.connect(i,n,r,this.model,this.lang)),this.target=n,this.connectionStatus="CONNECTED",this.eventRouter.emitStatus("CONNECTED"),this.isConnecting=!1,this.eventRouter.ensureStatusListener(),this.eventRouter.ensureConnectionListener(),this.statusController.handleConnected()}catch(T){let c=C(T);if(!o&&this.isIosConnectBusyRecoveryCandidate(c)){o=!0,this.logger?.error("[EpsonPrinter] iOS busy connect suspected stale session. Recovering connection and retrying connect once...",c);try{await this.recoverIosSessionForConnect(n,r),this.logger?.info?.("[EpsonPrinter] iOS connect recovery succeeded."),this.isConnecting=!1;return}catch(l){let N=C(l);throw this.logger?.error("[EpsonPrinter] iOS connect recovery failed",N),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,N}}throw this.logger?.error(`[EpsonPrinter] Connect failed for ${n}`,c),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,c}})}async print(e){if(!g)return;this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null);let r=0,n=!1;return this.queueController.enqueue(async()=>{let i=await this.sessionManager.ensureSession(),o=$(e);for(;;){r++,this.logger?.info?.(`[EpsonPrinter] Print attempt ${r}`);try{await P(()=>g.print(i,o));break}catch(T){let c=K(T);if(!n&&this.isIosConnectionRecoveryCandidate(c)){n=!0,this.logger?.error("[EpsonPrinter] iOS stale session suspected. Recovering connection and retrying print once...",c);try{i=await this.recoverIosSessionForPrint(),this.logger?.info?.("[EpsonPrinter] iOS session recovery succeeded. Retrying print...");continue}catch(l){let N=C(l);throw this.logger?.error("[EpsonPrinter] iOS session recovery failed",N),N}}if(c.isHardwareError){this.queueController.pause(),this.logger?.error(`[EpsonPrinter] Hardware error on attempt ${r}. Waiting for recovery before retry...`,c),this.recoveryController.start(),await this.queueController.waitUntilResumed(),this.logger?.info?.("[EpsonPrinter] Recovery detected. Sending cut to flush partial print before retry...");try{await g.print(i,[{cmd:"addCut"}])}catch{}this.logger?.info?.("[EpsonPrinter] Retrying print job...")}else throw this.logger?.error(`[EpsonPrinter] Print failed (non-recoverable) on attempt ${r}`,c),c}}}).finally(()=>{this.disconnectTimer&&clearTimeout(this.disconnectTimer),this.disconnectTimer=setTimeout(()=>{this.performDisconnect()},5e3)})}performDisconnect(){this.queueController.enqueueBackground(async()=>{if(!this.disconnectTimer)return;let e=this.sessionManager.getSessionId();if(g&&this.connectionStatus==="CONNECTED"&&e)try{await g.disconnect(e),this.connectionStatus="DISCONNECTED",this.target=null,this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),await this.sessionManager.disposeSession()}catch(r){this.logger?.error("[EpsonPrinter] Auto-Disconnect failed",r)}this.disconnectTimer=null})}async printEmbedded(e){try{let r=J(e);return await this.print(r)}catch(r){let n=C(r);throw String(r).includes("MunchiPrinterError")||this.logger?.error("[EpsonPrinter] Embedded print failed",n),n}}async disconnect(){return this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.queueController.enqueue(async()=>{if(!g)return;let e=this.sessionManager.getSessionId();if(e)try{await g.disconnect(e),this.connectionStatus="DISCONNECTED",this.target=null,this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.eventRouter.removeStatusListener(),this.recoveryController.stop()}catch(r){this.logger?.error("[EpsonPrinter] Disconnect failed",r)}finally{this.statusController.clear(),await this.sessionManager.disposeSession()}})}async getStatus(){if(!g)return F;let e=this.sessionManager.getSessionId();if(!e)return F;try{return await g.getStatus(e)}catch(r){throw this.logger?.error("[EpsonPrinter] GetStatus failed",r),C(r)}}async openDrawer(){let e={commands:[{type:"drawer"}]};return this.print(e)}onConnectionChange(e){return this.eventRouter.onConnectionChange(e)}onStatusChange(e){return this.statusController.subscribe(e)}};import{NativeModules as fe}from"react-native";var Z=fe.MunchiEpsonModule,Ne={bluetooth:["BT:"],tcp:["TCP:","TCPS:"],usb:["USB:"]},pe=t=>t.includes("[local_"),Pe=(t,e)=>e?Ne[e].some(n=>t.startsWith(n)):!0,Ie=async t=>{if(!Z)return console.warn("[EpsonDiscovery] Native module not found"),[];try{return(await Z.discover({timeout:t.timeout})).map(X).filter(r=>!r||pe(r.target)?!1:Pe(r.target,t.connectionType))}catch(e){let r=C(e);throw console.error("[EpsonDiscovery] Discovery failed",r),r}};var Se=[[/^TM-m10/i,0],[/^TM-m30III/i,29],[/^TM-m30II/i,21],[/^TM-m30/i,1],[/^TM-m50II/i,30],[/^TM-m50/i,23],[/^TM-m55/i,31],[/^TM-P20II/i,27],[/^TM-P20/i,2],[/^TM-P60II/i,4],[/^TM-P60/i,3],[/^TM-P80II/i,28],[/^TM-P80/i,5],[/^TM-T20/i,6],[/^TM-T60/i,7],[/^TM-T70/i,8],[/^TM-T81/i,9],[/^TM-T82/i,10],[/^TM-T83III/i,19],[/^TM-T83/i,11],[/^TM-T88VII/i,24],[/^TM-T88/i,12],[/^TM-T90KP/i,14],[/^TM-T90/i,13],[/^TM-T100/i,20],[/^TM-U220II/i,32],[/^TM-U220/i,15],[/^TM-U330/i,16],[/^TM-L90LFC/i,25],[/^TM-L90/i,17],[/^TM-L100/i,26],[/^TM-H6000/i,18]],_e=t=>{for(let[e,r]of Se)if(e.test(t))return r;return null};var Oe=t=>{let e=C(t),r=e.message.toUpperCase(),n=t instanceof f?t.isHardwareError:A(t),i=e.translationCode==="printer.error.busy"||r.includes("BUSY")||r.includes("IN_USE")||r.includes("PRINT_BUSY"),o=e.code==="TIMEOUT"||e.translationCode==="printer.error.connection_timeout"||r.includes("TIMEOUT"),T=e.code==="CONNECTION_FAILED"||e.translationCode==="printer.error.offline",c=e.code==="DISCOVERY_FAILED",l="UNKNOWN";return n?l="HARDWARE":i?l="BUSY":o?l="TIMEOUT":T?l="CONNECTION":c?l="DISCOVERY":e.code==="PRINT_FAILED"&&(l="PRINT"),{code:e.code,translationCode:e.translationCode,message:e.message,category:l,isHardwareError:n,retryable:i||o,shouldPauseQueue:n,raw:t}},Ot=t=>Oe(t);import ye from"react";import{createContext as ve,useContext as De,useEffect as Me,useMemo as ee,useState as we}from"react";var te=ve(void 0),bt=({config:t,logger:e,children:r})=>{let[n,i]=we();Me(()=>{e&&q(e)},[e]);let o=ee(()=>{let c={...t,logger:e??t.logger};return ne(c)},[t.lang,t.model,t.target,e]),T=ee(()=>({printer:o,config:t,isReady:!0,error:n}),[o,t,n]);return ye.createElement(te.Provider,{value:T},r)},re=()=>{let t=De(te);if(!t)throw new Error("usePrinter must be used within a PrinterProvider");return t};import{useCallback as B,useEffect as x,useRef as ie,useState as Y}from"react";function Ut(t={}){let{enabled:e=!0}=t,{printer:r,config:n}=re(),[i,o]=Y("DISCONNECTED"),[T,c]=Y(null),[l,N]=Y(null),H=ie("DISCONNECTED"),V=ie(null),m=l!==null,p=B(E=>{V.current=E,N(E)},[]),I=B(E=>{if(E instanceof Error&&E.message){p(E.message);return}if(typeof E=="string"&&E.length>0){p(E);return}p("Unknown printer error")},[p]),a=B(()=>{V.current!==null&&p(null)},[p]);return x(()=>r.onConnectionChange(S=>{H.current=S,o(S),S==="CONNECTED"&&a()}),[a,r]),x(()=>e?r.onStatusChange(S=>{c(S),a()}):void 0,[a,e,r]),x(()=>{e&&i==="CONNECTED"&&r.getStatus().then(E=>{c(E),a()}).catch(I)},[I,a,i,e,r]),x(()=>{let E=H.current;if(e){E==="DISCONNECTED"&&i==="DISCONNECTED"&&r.connect(n.target,5e3).then(a).catch(I);return}(i==="CONNECTED"||i==="CONNECTING")&&r.disconnect().then(a).catch(I)},[I,a,n.target,i,e,r]),{connectionStatus:i,hardwareStatus:T,isError:m,errorMessage:l}}var be="1.0.9";function ne(t){return new b({...t,logger:t.logger??z()})}export{ce as Epos2CallbackCode,ae as Epos2ConnectionEvent,ue as Epos2ErrorStatus,le as Epos2Font,W as Epos2Lang,Q as Epos2StatusEvent,k as EpsonModel,U as FontSize,h as MunchiPrinterError,f as PrinterError,_ as PrinterErrorCode,bt as PrinterProvider,O as PrinterTranslationCode,be as VERSION,Ie as discoverPrinters,z as getGlobalLogger,ne as getPrinter,Ot as resolveEpsonError,_e as resolveModelFromBluetoothName,Oe as resolvePrinterError,q as setGlobalLogger,re as usePrinter,Ut as usePrinterStatus};
@@ -1 +1 @@
1
- {"version":3,"file":"errorUtils.d.ts","sourceRoot":"","sources":["../../src/utils/errorUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA4C,MAAM,WAAW,CAAC;AAuCzF,eAAO,MAAM,cAAc,GAAI,OAAO,OAAO,KAAG,kBAgK/C,CAAC"}
1
+ {"version":3,"file":"errorUtils.d.ts","sourceRoot":"","sources":["../../src/utils/errorUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA4C,MAAM,WAAW,CAAC;AAuCzF,eAAO,MAAM,cAAc,GAAI,OAAO,OAAO,KAAG,kBAqK/C,CAAC"}
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "1.0.7";
1
+ export declare const VERSION = "1.0.9";
2
2
  //# sourceMappingURL=version.d.ts.map
@@ -24,6 +24,7 @@ class MunchiEpsonModule: RCTEventEmitter, Epos2ConnectionDelegate, Epos2PtrStatu
24
24
  private var discoveryResolve: RCTPromiseResolveBlock?
25
25
  private var discoveryReject: RCTPromiseRejectBlock?
26
26
  private var foundPrinters: [NSDictionary] = []
27
+ private let printerQueue = DispatchQueue(label: "com.munchiepsonprinter.epson")
27
28
 
28
29
  override func supportedEvents() -> [String]! {
29
30
  return ["onPrinterStatusChange", "onPrinterConnectionChange", "onPrinterReceive"]
@@ -95,319 +96,363 @@ class MunchiEpsonModule: RCTEventEmitter, Epos2ConnectionDelegate, Epos2PtrStatu
95
96
 
96
97
  @objc(initSession:rejecter:)
97
98
  func initSession(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
98
- let sessionId = UUID().uuidString
99
- sessions[sessionId] = EpsonSession(sessionId: sessionId)
100
- resolve(sessionId)
99
+ printerQueue.async { [weak self] in
100
+ guard let self = self else { return }
101
+
102
+ let sessionId = UUID().uuidString
103
+ self.sessions[sessionId] = EpsonSession(sessionId: sessionId)
104
+ resolve(sessionId)
105
+ }
101
106
  }
102
107
 
103
108
  @objc(disposeSession:resolver:rejecter:)
104
109
  func disposeSession(_ sessionId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
105
- guard let session = sessions.removeValue(forKey: sessionId) else {
110
+ printerQueue.async { [weak self] in
111
+ guard let self = self else { return }
112
+
113
+ guard let session = self.sessions.removeValue(forKey: sessionId) else {
114
+ resolve(true)
115
+ return
116
+ }
117
+
118
+ self.teardownPrinter(session, disconnect: true)
106
119
  resolve(true)
107
- return
108
120
  }
109
-
110
- teardownPrinter(session, disconnect: true)
111
- resolve(true)
112
121
  }
113
122
 
114
123
  @objc(connect:target:timeout:model:lang:resolver:rejecter:)
115
124
  func connect(_ sessionId: String, target: String, timeout: Double, model: Int, lang: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
116
- guard let session = sessions[sessionId] else {
117
- reject("SESSION_ERROR", "Session not found", nil)
118
- return
119
- }
125
+ printerQueue.async { [weak self] in
126
+ guard let self = self else { return }
120
127
 
121
- var connectionDelay = 0.0
128
+ guard let session = self.sessions[sessionId] else {
129
+ reject("SESSION_ERROR", "Session not found", nil)
130
+ return
131
+ }
122
132
 
123
- if session.printer != nil {
124
- teardownPrinter(session, disconnect: true)
125
- connectionDelay = 0.5
126
- }
133
+ var connectionDelay = 0.0
127
134
 
128
- DispatchQueue.main.asyncAfter(deadline: .now() + connectionDelay) { [weak self] in
129
- guard let self = self else { return }
130
- guard let latestSession = self.sessions[sessionId] else {
131
- reject("SESSION_ERROR", "Session disposed while connecting", nil)
132
- return
135
+ if session.printer != nil {
136
+ self.teardownPrinter(session, disconnect: true)
137
+ connectionDelay = 0.5
133
138
  }
134
139
 
135
- if let ownerSessionId = self.targetToSessionId[target], ownerSessionId != sessionId {
136
- if let ownerSession = self.sessions[ownerSessionId] {
137
- if ownerSession.printResolve != nil {
138
- reject("TARGET_IN_USE", "Target is busy with an active print job: \(target)", nil)
139
- return
140
+ self.printerQueue.asyncAfter(deadline: .now() + connectionDelay) { [weak self] in
141
+ guard let self = self else { return }
142
+ guard let latestSession = self.sessions[sessionId] else {
143
+ reject("SESSION_ERROR", "Session disposed while connecting", nil)
144
+ return
145
+ }
146
+
147
+ if let ownerSessionId = self.targetToSessionId[target], ownerSessionId != sessionId {
148
+ if let ownerSession = self.sessions[ownerSessionId] {
149
+ if ownerSession.printResolve != nil {
150
+ reject("TARGET_IN_USE", "Target is busy with an active print job: \(target)", nil)
151
+ return
152
+ }
153
+ _ = self.teardownPrinter(ownerSession, disconnect: true)
154
+ } else {
155
+ self.targetToSessionId.removeValue(forKey: target)
140
156
  }
141
- _ = self.teardownPrinter(ownerSession, disconnect: true)
142
- } else {
143
- self.targetToSessionId.removeValue(forKey: target)
144
157
  }
145
- }
146
158
 
147
- let safeLang = self.normalizedLanguage(lang)
148
- let safeModel = self.normalizedModel(model)
159
+ let safeLang = self.normalizedLanguage(lang)
160
+ let safeModel = self.normalizedModel(model)
149
161
 
150
- latestSession.printer = Epos2Printer(printerSeries: safeModel, lang: safeLang)
162
+ latestSession.printer = Epos2Printer(printerSeries: safeModel, lang: safeLang)
151
163
 
152
- guard let p = latestSession.printer else {
153
- reject("INIT_ERROR", "Failed to initialize printer object", nil)
154
- return
155
- }
156
-
157
- p.setConnectionEventDelegate(self)
158
- p.setStatusChangeEventDelegate(self)
159
- p.setReceiveEventDelegate(self)
160
- self.printerToSessionId[ObjectIdentifier(p)] = sessionId
161
- latestSession.target = target
162
-
163
- let connectTimeout = Int(timeout)
164
- let result = p.connect(target, timeout: connectTimeout)
164
+ guard let p = latestSession.printer else {
165
+ reject("INIT_ERROR", "Failed to initialize printer object", nil)
166
+ return
167
+ }
165
168
 
166
- if result == EPOS2_SUCCESS.rawValue {
167
- _ = p.startMonitor()
168
- self.targetToSessionId[target] = sessionId
169
- resolve("Connected to \(target)")
170
- } else {
171
- self.teardownPrinter(latestSession, disconnect: false)
172
- reject("CONNECT_ERROR", "Failed to connect: \(result)", nil)
169
+ p.setConnectionEventDelegate(self)
170
+ p.setStatusChangeEventDelegate(self)
171
+ p.setReceiveEventDelegate(self)
172
+ self.printerToSessionId[ObjectIdentifier(p)] = sessionId
173
+ latestSession.target = target
174
+
175
+ let connectTimeout = Int(timeout)
176
+ let result = p.connect(target, timeout: connectTimeout)
177
+
178
+ if result == EPOS2_SUCCESS.rawValue {
179
+ _ = p.startMonitor()
180
+ self.targetToSessionId[target] = sessionId
181
+ resolve("Connected to \(target)")
182
+ } else if self.isBusyError(result) {
183
+ self.teardownPrinter(latestSession, disconnect: false)
184
+ reject("PRINT_BUSY", "Printer is busy: \(result) (\(self.getErrorDescription(result)))", nil)
185
+ } else {
186
+ self.teardownPrinter(latestSession, disconnect: false)
187
+ reject("CONNECT_ERROR", "Failed to connect: \(result)", nil)
188
+ }
173
189
  }
174
190
  }
175
191
  }
176
192
 
177
193
  @objc(disconnect:resolver:rejecter:)
178
194
  func disconnect(_ sessionId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
179
- guard let session = sessions[sessionId] else {
180
- resolve(true)
181
- return
182
- }
195
+ printerQueue.async { [weak self] in
196
+ guard let self = self else { return }
197
+
198
+ guard let session = self.sessions[sessionId] else {
199
+ resolve(true)
200
+ return
201
+ }
183
202
 
184
- let result = teardownPrinter(session, disconnect: true)
203
+ let result = self.teardownPrinter(session, disconnect: true)
185
204
 
186
- if result == nil || result == EPOS2_SUCCESS.rawValue {
187
- resolve(true)
188
- } else {
189
- resolve(false)
205
+ if result == nil || result == EPOS2_SUCCESS.rawValue {
206
+ resolve(true)
207
+ } else {
208
+ resolve(false)
209
+ }
190
210
  }
191
211
  }
192
212
 
193
213
  // --- Delegate Methods ---
194
214
 
195
215
  func onConnection(_ deviceObj: Any!, eventType: Int32) {
196
- guard
197
- let obj = deviceObj as AnyObject?,
198
- let sessionId = printerToSessionId[ObjectIdentifier(obj)],
199
- let session = sessions[sessionId]
200
- else { return }
201
-
202
- var status = "UNKNOWN"
203
- switch eventType {
204
- case EPOS2_EVENT_RECONNECTING.rawValue:
205
- status = "RECONNECTING"
206
- case EPOS2_EVENT_RECONNECT.rawValue:
207
- status = "RECONNECTED"
208
- case EPOS2_EVENT_DISCONNECT.rawValue:
209
- status = "DISCONNECTED"
210
- default:
211
- break
212
- }
216
+ printerQueue.async { [weak self] in
217
+ guard let self = self else { return }
218
+ guard
219
+ let obj = deviceObj as AnyObject?,
220
+ let sessionId = self.printerToSessionId[ObjectIdentifier(obj)],
221
+ let session = self.sessions[sessionId]
222
+ else { return }
223
+
224
+ var status = "UNKNOWN"
225
+ switch eventType {
226
+ case EPOS2_EVENT_RECONNECTING.rawValue:
227
+ status = "RECONNECTING"
228
+ case EPOS2_EVENT_RECONNECT.rawValue:
229
+ status = "RECONNECTED"
230
+ case EPOS2_EVENT_DISCONNECT.rawValue:
231
+ status = "DISCONNECTED"
232
+ default:
233
+ break
234
+ }
213
235
 
214
- sendEvent(withName: "onPrinterConnectionChange", body: [
215
- "sessionId": session.sessionId,
216
- "status": status,
217
- "target": session.target ?? ""
218
- ])
236
+ self.sendEvent(withName: "onPrinterConnectionChange", body: [
237
+ "sessionId": session.sessionId,
238
+ "status": status,
239
+ "target": session.target ?? ""
240
+ ])
241
+ }
219
242
  }
220
243
 
221
244
  func onPtrStatusChange(_ printerObj: Epos2Printer!, eventType: Int32) {
222
- guard
223
- let sessionId = printerToSessionId[ObjectIdentifier(printerObj)],
224
- let session = sessions[sessionId]
225
- else { return }
226
-
227
- sendEvent(withName: "onPrinterStatusChange", body: [
228
- "sessionId": session.sessionId,
229
- "target": session.target ?? "",
230
- "eventType": eventType
231
- ])
245
+ printerQueue.async { [weak self] in
246
+ guard let self = self else { return }
247
+ guard
248
+ let sessionId = self.printerToSessionId[ObjectIdentifier(printerObj)],
249
+ let session = self.sessions[sessionId]
250
+ else { return }
251
+
252
+ self.sendEvent(withName: "onPrinterStatusChange", body: [
253
+ "sessionId": session.sessionId,
254
+ "target": session.target ?? "",
255
+ "eventType": eventType
256
+ ])
257
+ }
232
258
  }
233
259
 
234
260
  func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
235
- guard
236
- let sessionId = printerToSessionId[ObjectIdentifier(printerObj)],
237
- let session = sessions[sessionId]
238
- else { return }
239
-
240
- session.printTimeoutWork?.cancel()
241
- session.printTimeoutWork = nil
242
-
243
- if let resolve = session.printResolve, let reject = session.printReject {
244
- if code == EPOS2_CODE_SUCCESS.rawValue {
245
- resolve(true)
246
- } else {
247
- reject("PRINT_FAILURE", "Print finished with error code: \(code)", nil)
261
+ printerQueue.async { [weak self] in
262
+ guard let self = self else { return }
263
+ guard
264
+ let sessionId = self.printerToSessionId[ObjectIdentifier(printerObj)],
265
+ let session = self.sessions[sessionId]
266
+ else { return }
267
+
268
+ session.printTimeoutWork?.cancel()
269
+ session.printTimeoutWork = nil
270
+
271
+ if let resolve = session.printResolve, let reject = session.printReject {
272
+ if code == EPOS2_CODE_SUCCESS.rawValue {
273
+ resolve(true)
274
+ } else if self.isBusyCallbackCode(code) {
275
+ reject("PRINT_BUSY", "Printer is busy with error code: \(code)", nil)
276
+ } else {
277
+ reject("PRINT_FAILURE", "Print finished with error code: \(code)", nil)
278
+ }
279
+ session.printResolve = nil
280
+ session.printReject = nil
248
281
  }
249
- session.printResolve = nil
250
- session.printReject = nil
251
- }
252
282
 
253
- sendEvent(withName: "onPrinterReceive", body: [
254
- "sessionId": session.sessionId,
255
- "target": session.target ?? "",
256
- "code": code
257
- ])
283
+ self.sendEvent(withName: "onPrinterReceive", body: [
284
+ "sessionId": session.sessionId,
285
+ "target": session.target ?? "",
286
+ "code": code
287
+ ])
288
+ }
258
289
  }
259
290
 
260
291
  @objc(print:commands:resolver:rejecter:)
261
292
  func print(_ sessionId: String, commands: NSArray, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
262
- guard let session = sessions[sessionId], let p = session.printer else {
263
- reject("PRINT_ERROR", "Printer not connected", nil)
264
- return
265
- }
293
+ printerQueue.async { [weak self] in
294
+ guard let self = self else { return }
295
+ guard let session = self.sessions[sessionId], let p = session.printer else {
296
+ reject("PRINT_ERROR", "Printer not connected", nil)
297
+ return
298
+ }
266
299
 
267
- if session.printResolve != nil {
268
- reject("PRINT_BUSY", "Another print job is in progress", nil)
269
- return
270
- }
300
+ if session.printResolve != nil {
301
+ reject("PRINT_BUSY", "Another print job is in progress", nil)
302
+ return
303
+ }
271
304
 
272
- p.clearCommandBuffer()
273
-
274
- for command in commands {
275
- if let cmd = command as? NSDictionary, let type = cmd["cmd"] as? String {
276
- switch type {
277
- case "addText":
278
- if let data = cmd["data"] as? String {
279
- if let align = cmd["align"] as? String {
280
- var alignVal = EPOS2_ALIGN_LEFT.rawValue
281
- if align == "center" { alignVal = EPOS2_ALIGN_CENTER.rawValue }
282
- else if align == "right" { alignVal = EPOS2_ALIGN_RIGHT.rawValue }
283
- p.addTextAlign(alignVal)
284
- }
285
- if let bold = cmd["bold"] as? Bool {
286
- p.addTextStyle(EPOS2_PARAM_DEFAULT, ul: EPOS2_PARAM_DEFAULT, em: bold ? EPOS2_TRUE : EPOS2_FALSE, color: EPOS2_PARAM_DEFAULT)
287
- }
288
- if let size = cmd["size"] as? String {
289
- }
290
- p.addText(data)
291
- }
292
- case "addTextAlign":
293
- if let align = cmd["align"] as? String {
294
- var alignVal = EPOS2_ALIGN_LEFT.rawValue
295
- if align == "center" { alignVal = EPOS2_ALIGN_CENTER.rawValue }
296
- else if align == "right" { alignVal = EPOS2_ALIGN_RIGHT.rawValue }
297
- p.addTextAlign(alignVal)
298
- }
299
- case "addTextStyle":
300
- if let bold = cmd["bold"] as? Bool {
301
- p.addTextStyle(EPOS2_PARAM_DEFAULT, ul: EPOS2_PARAM_DEFAULT, em: bold ? EPOS2_TRUE : EPOS2_FALSE, color: EPOS2_PARAM_DEFAULT)
302
- }
303
- case "addTextSize":
304
- if let w = cmd["width"] as? Int, let h = cmd["height"] as? Int {
305
- p.addTextSize(Int(w), height: Int(h))
306
- }
307
- case "addBarcode":
308
- if let data = cmd["data"] as? String, let typeStr = cmd["type"] as? String {
309
- var type = EPOS2_BARCODE_CODE39.rawValue
310
- if typeStr == "UPC-A" { type = EPOS2_BARCODE_UPC_A.rawValue }
311
- else if typeStr == "UPC-E" { type = EPOS2_BARCODE_UPC_E.rawValue }
312
- else if typeStr == "EAN13" { type = EPOS2_BARCODE_EAN13.rawValue }
313
- else if typeStr == "EAN8" { type = EPOS2_BARCODE_EAN8.rawValue }
314
- else if typeStr == "CODE39" { type = EPOS2_BARCODE_CODE39.rawValue }
315
- else if typeStr == "ITF" { type = EPOS2_BARCODE_ITF.rawValue }
316
- else if typeStr == "CODABAR" { type = EPOS2_BARCODE_CODABAR.rawValue }
317
-
318
- p.addBarcode(data, type: type, hri: EPOS2_HRI_BELOW.rawValue, font: EPOS2_FONT_A.rawValue, width: 2, height: 100)
319
- }
320
- case "addSymbol":
321
- if let data = cmd["data"] as? String {
322
- p.addSymbol(data, type: EPOS2_SYMBOL_QRCODE_MODEL_2.rawValue, level: EPOS2_LEVEL_M.rawValue, width: 3, height: 3, size: 0)
323
- }
324
- case "addCut":
325
- p.addCut(EPOS2_CUT_FEED.rawValue)
326
- case "addFeedLine":
327
- if let line = cmd["line"] as? Int {
328
- p.addFeedLine(Int(line))
329
- }
330
- case "addPulse":
331
- p.addPulse(EPOS2_DRAWER_2PIN.rawValue, time: EPOS2_PULSE_100.rawValue)
332
- case "addTextLang":
333
- if let lang = cmd["lang"] as? Int {
334
- p.addTextLang(Int32(lang))
335
- }
336
- case "addTextFont":
337
- if let font = cmd["font"] as? Int {
338
- p.addTextFont(Int32(font))
339
- }
340
- case "addImage":
341
- if let image = image(for: cmd) {
342
- let width = Int(image.size.width.rounded(.up))
343
- let height = Int(image.size.height.rounded(.up))
344
- p.add(
345
- image,
346
- x: 0,
347
- y: 0,
348
- width: width,
349
- height: height,
350
- color: EPOS2_COLOR_1.rawValue,
351
- mode: EPOS2_MODE_MONO.rawValue,
352
- halftone: EPOS2_HALFTONE_DITHER.rawValue,
353
- brightness: Double(EPOS2_PARAM_DEFAULT),
354
- compress: EPOS2_COMPRESS_AUTO.rawValue
355
- )
305
+ p.clearCommandBuffer()
306
+
307
+ for command in commands {
308
+ if let cmd = command as? NSDictionary, let type = cmd["cmd"] as? String {
309
+ switch type {
310
+ case "addText":
311
+ if let data = cmd["data"] as? String {
312
+ if let align = cmd["align"] as? String {
313
+ var alignVal = EPOS2_ALIGN_LEFT.rawValue
314
+ if align == "center" { alignVal = EPOS2_ALIGN_CENTER.rawValue }
315
+ else if align == "right" { alignVal = EPOS2_ALIGN_RIGHT.rawValue }
316
+ p.addTextAlign(alignVal)
317
+ }
318
+ if let bold = cmd["bold"] as? Bool {
319
+ p.addTextStyle(EPOS2_PARAM_DEFAULT, ul: EPOS2_PARAM_DEFAULT, em: bold ? EPOS2_TRUE : EPOS2_FALSE, color: EPOS2_PARAM_DEFAULT)
320
+ }
321
+ if let size = cmd["size"] as? String {
322
+ }
323
+ p.addText(data)
324
+ }
325
+ case "addTextAlign":
326
+ if let align = cmd["align"] as? String {
327
+ var alignVal = EPOS2_ALIGN_LEFT.rawValue
328
+ if align == "center" { alignVal = EPOS2_ALIGN_CENTER.rawValue }
329
+ else if align == "right" { alignVal = EPOS2_ALIGN_RIGHT.rawValue }
330
+ p.addTextAlign(alignVal)
331
+ }
332
+ case "addTextStyle":
333
+ if let bold = cmd["bold"] as? Bool {
334
+ p.addTextStyle(EPOS2_PARAM_DEFAULT, ul: EPOS2_PARAM_DEFAULT, em: bold ? EPOS2_TRUE : EPOS2_FALSE, color: EPOS2_PARAM_DEFAULT)
335
+ }
336
+ case "addTextSize":
337
+ if let w = cmd["width"] as? Int, let h = cmd["height"] as? Int {
338
+ p.addTextSize(Int(w), height: Int(h))
339
+ }
340
+ case "addBarcode":
341
+ if let data = cmd["data"] as? String, let typeStr = cmd["type"] as? String {
342
+ var type = EPOS2_BARCODE_CODE39.rawValue
343
+ if typeStr == "UPC-A" { type = EPOS2_BARCODE_UPC_A.rawValue }
344
+ else if typeStr == "UPC-E" { type = EPOS2_BARCODE_UPC_E.rawValue }
345
+ else if typeStr == "EAN13" { type = EPOS2_BARCODE_EAN13.rawValue }
346
+ else if typeStr == "EAN8" { type = EPOS2_BARCODE_EAN8.rawValue }
347
+ else if typeStr == "CODE39" { type = EPOS2_BARCODE_CODE39.rawValue }
348
+ else if typeStr == "ITF" { type = EPOS2_BARCODE_ITF.rawValue }
349
+ else if typeStr == "CODABAR" { type = EPOS2_BARCODE_CODABAR.rawValue }
350
+
351
+ p.addBarcode(data, type: type, hri: EPOS2_HRI_BELOW.rawValue, font: EPOS2_FONT_A.rawValue, width: 2, height: 100)
352
+ }
353
+ case "addSymbol":
354
+ if let data = cmd["data"] as? String {
355
+ p.addSymbol(data, type: EPOS2_SYMBOL_QRCODE_MODEL_2.rawValue, level: EPOS2_LEVEL_M.rawValue, width: 3, height: 3, size: 0)
356
+ }
357
+ case "addCut":
358
+ p.addCut(EPOS2_CUT_FEED.rawValue)
359
+ case "addFeedLine":
360
+ if let line = cmd["line"] as? Int {
361
+ p.addFeedLine(Int(line))
362
+ }
363
+ case "addPulse":
364
+ p.addPulse(EPOS2_DRAWER_2PIN.rawValue, time: EPOS2_PULSE_100.rawValue)
365
+ case "addTextLang":
366
+ if let lang = cmd["lang"] as? Int {
367
+ p.addTextLang(Int32(lang))
368
+ }
369
+ case "addTextFont":
370
+ if let font = cmd["font"] as? Int {
371
+ p.addTextFont(Int32(font))
372
+ }
373
+ case "addImage":
374
+ if let image = self.image(for: cmd) {
375
+ let width = Int(image.size.width.rounded(.up))
376
+ let height = Int(image.size.height.rounded(.up))
377
+ p.add(
378
+ image,
379
+ x: 0,
380
+ y: 0,
381
+ width: width,
382
+ height: height,
383
+ color: EPOS2_COLOR_1.rawValue,
384
+ mode: EPOS2_MODE_MONO.rawValue,
385
+ halftone: EPOS2_HALFTONE_DITHER.rawValue,
386
+ brightness: Double(EPOS2_PARAM_DEFAULT),
387
+ compress: EPOS2_COMPRESS_AUTO.rawValue
388
+ )
389
+ }
390
+ default:
391
+ break
356
392
  }
357
- default:
358
- break
359
393
  }
360
394
  }
361
- }
362
395
 
363
- session.printResolve = resolve
364
- session.printReject = reject
365
-
366
- let result = p.sendData(Int(EPOS2_PARAM_DEFAULT))
367
- if result != EPOS2_SUCCESS.rawValue {
368
- let errorMsg = getErrorDescription(result)
369
- session.printResolve = nil
370
- session.printReject = nil
371
- reject("PRINT_ERROR", "Failed to send data: \(result) (\(errorMsg))", nil)
372
- } else {
373
- let timeoutWork = DispatchWorkItem { [weak self] in
374
- guard
375
- let self = self,
376
- let timedSession = self.sessions[sessionId]
377
- else { return }
378
-
379
- if let reject = timedSession.printReject {
380
- reject("PRINT_TIMEOUT", "Print timed out waiting for printer response", nil)
396
+ session.printResolve = resolve
397
+ session.printReject = reject
398
+
399
+ let result = p.sendData(Int(EPOS2_PARAM_DEFAULT))
400
+ if result != EPOS2_SUCCESS.rawValue {
401
+ let errorMsg = self.getErrorDescription(result)
402
+ session.printResolve = nil
403
+ session.printReject = nil
404
+ if self.isBusyError(result) {
405
+ reject("PRINT_BUSY", "Printer is busy: \(result) (\(errorMsg))", nil)
406
+ } else {
407
+ reject("PRINT_ERROR", "Failed to send data: \(result) (\(errorMsg))", nil)
408
+ }
409
+ } else {
410
+ let timeoutWork = DispatchWorkItem { [weak self] in
411
+ guard let self = self else { return }
412
+
413
+ self.printerQueue.async { [weak self] in
414
+ guard
415
+ let self = self,
416
+ let timedSession = self.sessions[sessionId]
417
+ else { return }
418
+
419
+ if let reject = timedSession.printReject {
420
+ reject("PRINT_TIMEOUT", "Print timed out waiting for printer response", nil)
421
+ }
422
+ timedSession.printResolve = nil
423
+ timedSession.printReject = nil
424
+ timedSession.printTimeoutWork = nil
425
+ timedSession.printer?.clearCommandBuffer()
426
+ }
381
427
  }
382
- timedSession.printResolve = nil
383
- timedSession.printReject = nil
384
- timedSession.printTimeoutWork = nil
385
- timedSession.printer?.clearCommandBuffer()
428
+ session.printTimeoutWork = timeoutWork
429
+ DispatchQueue.main.asyncAfter(deadline: .now() + 30, execute: timeoutWork)
386
430
  }
387
- session.printTimeoutWork = timeoutWork
388
- DispatchQueue.main.asyncAfter(deadline: .now() + 30, execute: timeoutWork)
389
431
  }
390
432
  }
391
433
 
392
434
  @objc(getStatus:resolver:rejecter:)
393
435
  func getStatus(_ sessionId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
394
- guard let session = sessions[sessionId], let p = session.printer else {
395
- reject("STATUS_ERROR", "Printer not connected", nil)
396
- return
397
- }
436
+ printerQueue.async { [weak self] in
437
+ guard let self = self else { return }
438
+ guard let session = self.sessions[sessionId], let p = session.printer else {
439
+ reject("STATUS_ERROR", "Printer not connected", nil)
440
+ return
441
+ }
398
442
 
399
- if let status = p.getStatus() {
400
- let statusDict: [String: Any] = [
401
- "online": status.connection == EPOS2_TRUE,
402
- "coverOpen": status.coverOpen == EPOS2_TRUE,
403
- "paper": status.paper == EPOS2_PAPER_EMPTY.rawValue ? "EMPTY" : (status.paper == EPOS2_PAPER_NEAR_END.rawValue ? "NEAR_EMPTY" : "OK"),
404
- "paperEmpty": status.paper == EPOS2_PAPER_EMPTY.rawValue, // legacy compat
405
- "drawerOpen": status.drawer == EPOS2_DRAWER_HIGH.rawValue, // HIGH typically means open/signal high depending on wiring, assuming HIGH = Open for now based on common usage
406
- "errorStatus": status.errorStatus == EPOS2_MECHANICAL_ERR.rawValue ? "MECHANICAL_ERR" : (status.errorStatus == EPOS2_AUTOCUTTER_ERR.rawValue ? "AUTOCUTTER_ERR" : "NONE")
407
- ]
408
- resolve(statusDict)
409
- } else {
410
- reject("STATUS_ERROR", "Failed to retrieve status", nil)
443
+ if let status = p.getStatus() {
444
+ let statusDict: [String: Any] = [
445
+ "online": status.connection == EPOS2_TRUE,
446
+ "coverOpen": status.coverOpen == EPOS2_TRUE,
447
+ "paper": status.paper == EPOS2_PAPER_EMPTY.rawValue ? "EMPTY" : (status.paper == EPOS2_PAPER_NEAR_END.rawValue ? "NEAR_EMPTY" : "OK"),
448
+ "paperEmpty": status.paper == EPOS2_PAPER_EMPTY.rawValue,
449
+ "drawerOpen": status.drawer == EPOS2_DRAWER_HIGH.rawValue,
450
+ "errorStatus": status.errorStatus == EPOS2_MECHANICAL_ERR.rawValue ? "MECHANICAL_ERR" : (status.errorStatus == EPOS2_AUTOCUTTER_ERR.rawValue ? "AUTOCUTTER_ERR" : "NONE")
451
+ ]
452
+ resolve(statusDict)
453
+ } else {
454
+ reject("STATUS_ERROR", "Failed to retrieve status", nil)
455
+ }
411
456
  }
412
457
  }
413
458
 
@@ -423,6 +468,7 @@ class MunchiEpsonModule: RCTEventEmitter, Epos2ConnectionDelegate, Epos2PtrStatu
423
468
  case EPOS2_ERR_PROCESSING.rawValue: return "ERR_PROCESSING"
424
469
  case EPOS2_ERR_NOT_FOUND.rawValue: return "ERR_NOT_FOUND"
425
470
  case EPOS2_ERR_IN_USE.rawValue: return "ERR_IN_USE"
471
+ case EPOS2_ERR_DEVICE_BUSY.rawValue: return "ERR_DEVICE_BUSY"
426
472
  case EPOS2_ERR_TYPE_INVALID.rawValue: return "ERR_TYPE_INVALID"
427
473
  case EPOS2_ERR_DISCONNECT.rawValue: return "ERR_DISCONNECT"
428
474
  case EPOS2_ERR_ALREADY_OPENED.rawValue: return "ERR_ALREADY_OPENED"
@@ -435,6 +481,30 @@ class MunchiEpsonModule: RCTEventEmitter, Epos2ConnectionDelegate, Epos2PtrStatu
435
481
  }
436
482
  }
437
483
 
484
+ private func isBusyError(_ code: Int32) -> Bool {
485
+ switch code {
486
+ case EPOS2_ERR_PROCESSING.rawValue,
487
+ EPOS2_ERR_IN_USE.rawValue,
488
+ EPOS2_ERR_DEVICE_BUSY.rawValue:
489
+ return true
490
+ default:
491
+ return false
492
+ }
493
+ }
494
+
495
+ private func isBusyCallbackCode(_ code: Int32) -> Bool {
496
+ switch code {
497
+ case EPOS2_CODE_PRINTING.rawValue,
498
+ EPOS2_CODE_ERR_DEVICE_BUSY.rawValue,
499
+ EPOS2_CODE_ERR_IN_USE.rawValue,
500
+ EPOS2_CODE_ERR_PROCESSING.rawValue,
501
+ EPOS2_CODE_RETRY.rawValue:
502
+ return true
503
+ default:
504
+ return false
505
+ }
506
+ }
507
+
438
508
  private func normalizedLanguage(_ lang: Int) -> Int32 {
439
509
  if lang >= 0 && lang <= 7 {
440
510
  return Int32(lang)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@munchi_oy/react-native-epson-printer",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Munchi Printer SDK Bridge",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",