@munchi_oy/react-native-epson-printer 1.0.6 → 1.0.8

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/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
- "use strict";var he=Object.create;var D=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var Ce=Object.getOwnPropertyNames;var Ee=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=fe(e,i))||n.enumerable});return t};var Pe=(t,e,r)=>(r=t!=null?he(Ee(t)):{},J(e||!t||!t.__esModule?D(r,"default",{value:t,enumerable:!0}):r,t)),Ne=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=Ne(Be);var j={},B=t=>{j.logger=t},Y=()=>j.logger;var W=require("react-native");var y=(l=>(l.CONNECTION_FAILED="CONNECTION_FAILED",l.PRINT_FAILED="PRINT_FAILED",l.DISCOVERY_FAILED="DISCOVERY_FAILED",l.TIMEOUT="TIMEOUT",l.NATIVE_MODULE_MISSING="NATIVE_MODULE_MISSING",l.UNKNOWN="UNKNOWN",l))(y||{}),v=(l=>(l.ERR_PRINTER_OFFLINE="printer.error.offline",l.ERR_PRINTER_BUSY="printer.error.busy",l.ERR_PRINTER_COVER_OPEN="printer.error.cover_open",l.ERR_PRINTER_PAPER_EMPTY="printer.error.paper_empty",l.ERR_CONNECTION_TIMEOUT="printer.error.connection_timeout",l.ERR_UNKNOWN="printer.error.unknown",l))(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)},T=(t,e,r)=>t.includes(r)||e===r||e.includes(r),E=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):T(n,i,"TARGET_IN_USE")||T(n,i,"BUSY")||T(n,i,"IN_USE")||T(n,i,"PRINT_BUSY")||T(n,i,"DISCOVERY_BUSY")?new h("TIMEOUT","printer.error.busy","Printer is busy",t):T(n,i,"TIMEOUT")||T(n,i,"ERR_TIMEOUT")||T(n,i,"PRINT_TIMEOUT")?new h("TIMEOUT","printer.error.connection_timeout","Operation timed out",t):T(n,i,"PERMISSION_DENIED")||T(n,i,"LOCATION_DISABLED")||T(n,i,"BLUETOOTH_DISABLED")?new h("CONNECTION_FAILED","printer.error.offline",e,t):T(n,i,"DISCOVERY_ERROR")||n.includes("DISCOVERY")?new h("DISCOVERY_FAILED","printer.error.unknown",e,t):T(n,i,"PRINTER_OFFLINE")||T(n,i,"SESSION_ERROR")||T(n,i,"STATUS_ERROR")||T(n,i,"INIT_ERROR")||T(n,i,"PRINT_CANCELED")||T(n,i,"DISCONNECT")||T(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):T(n,i,"CONNECT_ERROR")||n.includes("FAILED TO CONNECT")||T(n,i,"ERR_CONNECT")?new h("CONNECTION_FAILED","printer.error.offline","Failed to connect to printer",t):T(n,i,"PRINT_ERROR")||T(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=(c=>(c[c.EN=0]="EN",c[c.JA=1]="JA",c[c.ZH_CN=2]="ZH_CN",c[c.ZH_TW=3]="ZH_TW",c[c.KO=4]="KO",c[c.TH=5]="TH",c[c.VI=6]="VI",c[c.MULTI=7]="MULTI",c))(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,l){super(n,i,o,l);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=E(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()},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=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 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:`------------------
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}
5
5
  `}),e.push({type:"text",text:`--------------------------------
6
- `}),e.push({type:"align",align:"left"});for(let r of t.lines){let n=r.t,i=r.s,o=r.i||0,g=`${" ".repeat(o)}${n}`;if(i==="C"){if(n.includes("---")){e.push({type:"text",text:`--------------------------------
7
- `,align:"center"});continue}e.push({type:"text",text:g+`
8
- `,align:"center",bold:!0})}else i==="B"?e.push({type:"text",text:g+`
9
- `,bold:!0}):i==="R"?e.push({type:"text",text:g+`
10
- `,bold:!0}):e.push({type:"text",text:g+`
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(l){let g=E(l);if(!o&&this.isIosConnectBusyRecoveryCandidate(g)){o=!0,this.logger?.error("[EpsonPrinter] iOS busy connect suspected stale session. Recovering connection and retrying connect once...",g);try{await this.recoverIosSessionForConnect(n,r),this.logger?.info?.("[EpsonPrinter] iOS connect recovery succeeded."),this.isConnecting=!1;return}catch(c){let N=E(c);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}`,g),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,g}})}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(l){let g=ne(l);if(!n&&this.isIosConnectionRecoveryCandidate(g)){n=!0,this.logger?.error("[EpsonPrinter] iOS stale session suspected. Recovering connection and retrying print once...",g);try{i=await this.recoverIosSessionForPrint(),this.logger?.info?.("[EpsonPrinter] iOS session recovery succeeded. Retrying print...");continue}catch(c){let N=E(c);throw this.logger?.error("[EpsonPrinter] iOS session recovery failed",N),N}}if(g.isHardwareError){this.queueController.pause(),this.logger?.error(`[EpsonPrinter] Hardware error on attempt ${r}. Waiting for recovery before retry...`,g),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}`,g),g}}}).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=E(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),E(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=E(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=E(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"),l=e.code==="CONNECTION_FAILED"||e.translationCode==="printer.error.offline",g=e.code==="DISCOVERY_FAILED",c="UNKNOWN";return n?c="HARDWARE":i?c="BUSY":o?c="TIMEOUT":l?c="CONNECTION":g?c="DISCOVERY":e.code==="PRINT_FAILED"&&(c="PRINT"),{code:e.code,translationCode:e.translationCode,message:e.message,category:c,isHardwareError:n,retryable:i||o,shouldPauseQueue:n,raw:t}},Le=t=>de(t);var me=Pe(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 g={...t,logger:e??t.logger};return K(g)},[t.lang,t.model,t.target,e]),l=(0,R.useMemo)(()=>({printer:o,config:t,isReady:!0,error:n}),[o,t,n]);return me.default.createElement(ge.Provider,{value:l},r)},k=()=>{let t=(0,R.useContext)(ge);if(!t)throw new Error("usePrinter must be used within a PrinterProvider");return t};var f=require("react");function Fe(t={}){let{enabled:e=!0}=t,{printer:r,config:n}=k(),[i,o]=(0,f.useState)("DISCONNECTED"),[l,g]=(0,f.useState)(null),[c,N]=(0,f.useState)(null),X=(0,f.useRef)("DISCONNECTED"),$=(0,f.useRef)(null),d=c!==null,I=(0,f.useCallback)(C=>{$.current=C,N(C)},[]),_=(0,f.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,f.useCallback)(()=>{$.current!==null&&I(null)},[I]);return(0,f.useEffect)(()=>r.onConnectionChange(O=>{X.current=O,o(O),O==="CONNECTED"&&a()}),[a,r]),(0,f.useEffect)(()=>e?r.onStatusChange(O=>{g(O),a()}):void 0,[a,e,r]),(0,f.useEffect)(()=>{e&&i==="CONNECTED"&&r.getStatus().then(C=>{g(C),a()}).catch(_)},[_,a,i,e,r]),(0,f.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:l,isError:d,errorMessage:c}}var Te="1.0.6";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});
6
+ `}),e.push({type:"align",align:"left"});for(let r of t.lines){let n=r.t,i=r.s,o=r.i||0,c=`${" ".repeat(o)}${n}`;if(i==="C"){if(n.includes("---")){e.push({type:"text",text:`--------------------------------
7
+ `,align:"center"});continue}e.push({type:"text",text:c+`
8
+ `,align:"center",bold:!0})}else i==="B"?e.push({type:"text",text:c+`
9
+ `,bold:!0}):i==="R"?e.push({type:"text",text:c+`
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.8";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,11 +1,11 @@
1
- var q={},z=t=>{q.logger=t},G=()=>q.logger;import{Platform as j}from"react-native";var _=(l=>(l.CONNECTION_FAILED="CONNECTION_FAILED",l.PRINT_FAILED="PRINT_FAILED",l.DISCOVERY_FAILED="DISCOVERY_FAILED",l.TIMEOUT="TIMEOUT",l.NATIVE_MODULE_MISSING="NATIVE_MODULE_MISSING",l.UNKNOWN="UNKNOWN",l))(_||{}),O=(l=>(l.ERR_PRINTER_OFFLINE="printer.error.offline",l.ERR_PRINTER_BUSY="printer.error.busy",l.ERR_PRINTER_COVER_OPEN="printer.error.cover_open",l.ERR_PRINTER_PAPER_EMPTY="printer.error.paper_empty",l.ERR_CONNECTION_TIMEOUT="printer.error.connection_timeout",l.ERR_UNKNOWN="printer.error.unknown",l))(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)},T=(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):T(n,i,"TARGET_IN_USE")||T(n,i,"BUSY")||T(n,i,"IN_USE")||T(n,i,"PRINT_BUSY")||T(n,i,"DISCOVERY_BUSY")?new h("TIMEOUT","printer.error.busy","Printer is busy",t):T(n,i,"TIMEOUT")||T(n,i,"ERR_TIMEOUT")||T(n,i,"PRINT_TIMEOUT")?new h("TIMEOUT","printer.error.connection_timeout","Operation timed out",t):T(n,i,"PERMISSION_DENIED")||T(n,i,"LOCATION_DISABLED")||T(n,i,"BLUETOOTH_DISABLED")?new h("CONNECTION_FAILED","printer.error.offline",e,t):T(n,i,"DISCOVERY_ERROR")||n.includes("DISCOVERY")?new h("DISCOVERY_FAILED","printer.error.unknown",e,t):T(n,i,"PRINTER_OFFLINE")||T(n,i,"SESSION_ERROR")||T(n,i,"STATUS_ERROR")||T(n,i,"INIT_ERROR")||T(n,i,"PRINT_CANCELED")||T(n,i,"DISCONNECT")||T(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):T(n,i,"CONNECT_ERROR")||n.includes("FAILED TO CONNECT")||T(n,i,"ERR_CONNECT")?new h("CONNECTION_FAILED","printer.error.offline","Failed to connect to printer",t):T(n,i,"PRINT_ERROR")||T(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=(c=>(c[c.EN=0]="EN",c[c.JA=1]="JA",c[c.ZH_CN=2]="ZH_CN",c[c.ZH_TW=3]="ZH_TW",c[c.KO=4]="KO",c[c.TH=5]="TH",c[c.VI=6]="VI",c[c.MULTI=7]="MULTI",c))(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,l){super(n,i,o,l);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()},E=(t,e,r)=>t.includes(r)||e===r||e.includes(r),N=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(E(i,o,"TARGET_IN_USE"))throw n;if(E(i,o,"BUSY")||E(i,o,"IN_USE")||E(i,o,"ERR_IN_USE")||E(i,o,"PRINT_BUSY")||E(i,o,"ERR_CONNECT")||E(i,o,"CONNECT_ERROR")||E(i,o,"ERR_TIMEOUT")||E(i,o,"PRINT_TIMEOUT"))return await ge(r),N(t,e-1,r);throw n}};var fe=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:fe.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 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:`------------------
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}
5
5
  `}),e.push({type:"text",text:`--------------------------------
6
- `}),e.push({type:"align",align:"left"});for(let r of t.lines){let n=r.t,i=r.s,o=r.i||0,g=`${" ".repeat(o)}${n}`;if(i==="C"){if(n.includes("---")){e.push({type:"text",text:`--------------------------------
7
- `,align:"center"});continue}e.push({type:"text",text:g+`
8
- `,align:"center",bold:!0})}else i==="B"?e.push({type:"text",text:g+`
9
- `,bold:!0}):i==="R"?e.push({type:"text",text:g+`
10
- `,bold:!0}):e.push({type:"text",text:g+`
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},Ee=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 N(()=>m.connect(n,e,Ee,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 N(()=>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 N(()=>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(l){let g=C(l);if(!o&&this.isIosConnectBusyRecoveryCandidate(g)){o=!0,this.logger?.error("[EpsonPrinter] iOS busy connect suspected stale session. Recovering connection and retrying connect once...",g);try{await this.recoverIosSessionForConnect(n,r),this.logger?.info?.("[EpsonPrinter] iOS connect recovery succeeded."),this.isConnecting=!1;return}catch(c){let p=C(c);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}`,g),this.connectionStatus="DISCONNECTED",this.statusController.handleDisconnected(),this.eventRouter.emitStatus("DISCONNECTED"),this.isConnecting=!1,g}})}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 N(()=>m.print(i,o));break}catch(l){let g=K(l);if(!n&&this.isIosConnectionRecoveryCandidate(g)){n=!0,this.logger?.error("[EpsonPrinter] iOS stale session suspected. Recovering connection and retrying print once...",g);try{i=await this.recoverIosSessionForPrint(),this.logger?.info?.("[EpsonPrinter] iOS session recovery succeeded. Retrying print...");continue}catch(c){let p=C(c);throw this.logger?.error("[EpsonPrinter] iOS session recovery failed",p),p}}if(g.isHardwareError){this.queueController.pause(),this.logger?.error(`[EpsonPrinter] Hardware error on attempt ${r}. Waiting for recovery before retry...`,g),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}`,g),g}}}).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:"]},Pe=t=>t.includes("[local_"),Ne=(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||Pe(r.target)?!1:Ne(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"),l=e.code==="CONNECTION_FAILED"||e.translationCode==="printer.error.offline",g=e.code==="DISCOVERY_FAILED",c="UNKNOWN";return n?c="HARDWARE":i?c="BUSY":o?c="TIMEOUT":l?c="CONNECTION":g?c="DISCOVERY":e.code==="PRINT_FAILED"&&(c="PRINT"),{code:e.code,translationCode:e.translationCode,message:e.message,category:c,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 g={...t,logger:e??t.logger};return ne(g)},[t.lang,t.model,t.target,e]),l=ee(()=>({printer:o,config:t,isReady:!0,error:n}),[o,t,n]);return ye.createElement(te.Provider,{value:l},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"),[l,g]=Y(null),[c,p]=Y(null),H=ie("DISCONNECTED"),V=ie(null),d=c!==null,P=B(f=>{V.current=f,p(f)},[]),I=B(f=>{if(f instanceof Error&&f.message){P(f.message);return}if(typeof f=="string"&&f.length>0){P(f);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=>{g(S),a()}):void 0,[a,e,r]),x(()=>{e&&i==="CONNECTED"&&r.getStatus().then(f=>{g(f),a()}).catch(I)},[I,a,i,e,r]),x(()=>{let f=H.current;if(e){f==="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:l,isError:d,errorMessage:c}}var be="1.0.6";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};
6
+ `}),e.push({type:"align",align:"left"});for(let r of t.lines){let n=r.t,i=r.s,o=r.i||0,c=`${" ".repeat(o)}${n}`;if(i==="C"){if(n.includes("---")){e.push({type:"text",text:`--------------------------------
7
+ `,align:"center"});continue}e.push({type:"text",text:c+`
8
+ `,align:"center",bold:!0})}else i==="B"?e.push({type:"text",text:c+`
9
+ `,bold:!0}):i==="R"?e.push({type:"text",text:c+`
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.8";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};
@@ -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,kBAoJ/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,kBAgK/C,CAAC"}
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "1.0.6";
1
+ export declare const VERSION = "1.0.8";
2
2
  //# sourceMappingURL=version.d.ts.map