@vicacha-devs/medusa-plugin-barcode 0.0.0-stage → 0.1.0

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.
Files changed (176) hide show
  1. package/.medusa/server/src/admin/index.cjs +3960 -0
  2. package/.medusa/server/src/admin/index.js +3938 -0
  3. package/.medusa/server/src/api/admin/barcodes/[barcode]/route.js +43 -0
  4. package/.medusa/server/src/api/admin/barcodes/batch/route.js +9 -0
  5. package/.medusa/server/src/api/admin/barcodes/bulk-generate/route.js +9 -0
  6. package/.medusa/server/src/api/admin/barcodes/cache/route.js +25 -0
  7. package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/apply/route.js +13 -0
  8. package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/scan/route.js +13 -0
  9. package/.medusa/server/src/api/admin/barcodes/count-sessions/route.js +27 -0
  10. package/.medusa/server/src/api/admin/barcodes/create-from-barcode/route.js +9 -0
  11. package/.medusa/server/src/api/admin/barcodes/lookup/route.js +12 -0
  12. package/.medusa/server/src/api/admin/barcodes/print/route.js +11 -0
  13. package/.medusa/server/src/api/admin/barcodes/route.js +27 -0
  14. package/.medusa/server/src/api/admin/barcodes/scan/route.js +23 -0
  15. package/.medusa/server/src/api/admin/barcodes/tags/[id]/route.js +28 -0
  16. package/.medusa/server/src/api/admin/barcodes/tags/route.js +25 -0
  17. package/.medusa/server/src/api/admin/barcodes/templates/route.js +40 -0
  18. package/.medusa/server/src/api/admin/plugin/route.js +4 -0
  19. package/.medusa/server/src/api/admin/variants/barcode/[code]/route.js +28 -0
  20. package/.medusa/server/src/api/middlewares.js +73 -0
  21. package/.medusa/server/src/api/store/barcodes/[code]/route.js +28 -0
  22. package/.medusa/server/src/api/store/barcodes/[code]/verify/route.js +20 -0
  23. package/.medusa/server/src/api/store/plugin/route.js +4 -0
  24. package/.medusa/server/src/api/store/variants/barcode/[code]/route.js +28 -0
  25. package/.medusa/server/src/api/validators.js +173 -0
  26. package/.medusa/server/src/hooks/index.js +202 -0
  27. package/.medusa/server/src/index.js +5 -0
  28. package/.medusa/server/src/jobs/purge-expired-barcode-cache.js +10 -0
  29. package/.medusa/server/src/modules/barcode/index.js +16 -0
  30. package/.medusa/server/src/modules/barcode/migrations/Migration20260522000000.js +165 -0
  31. package/.medusa/server/src/modules/barcode/models/index.js +162 -0
  32. package/.medusa/server/src/modules/barcode/operations/context.js +2 -0
  33. package/.medusa/server/src/modules/barcode/operations/data-service.js +10 -0
  34. package/.medusa/server/src/modules/barcode/operations/generation.js +33 -0
  35. package/.medusa/server/src/modules/barcode/operations/index.js +9 -0
  36. package/.medusa/server/src/modules/barcode/operations/labels.js +57 -0
  37. package/.medusa/server/src/modules/barcode/operations/lookup.js +135 -0
  38. package/.medusa/server/src/modules/barcode/operations/maintenance.js +37 -0
  39. package/.medusa/server/src/modules/barcode/operations/stock-count.js +129 -0
  40. package/.medusa/server/src/modules/barcode/operations/tags.js +298 -0
  41. package/.medusa/server/src/modules/barcode/providers/index.js +721 -0
  42. package/.medusa/server/src/modules/barcode/service.js +161 -0
  43. package/.medusa/server/src/modules/barcode/types.js +21 -0
  44. package/.medusa/server/src/modules/barcode/utils/barcode-parser.js +399 -0
  45. package/.medusa/server/src/modules/barcode/utils/label-printer.js +533 -0
  46. package/.medusa/server/src/modules/barcode/utils/normalize-product.js +443 -0
  47. package/.medusa/server/src/modules/barcode/utils/security.js +114 -0
  48. package/.medusa/server/src/modules/barcode/utils/svg-renderer.js +492 -0
  49. package/.medusa/server/src/rbac.js +130 -0
  50. package/.medusa/server/src/sdk/index.js +164 -0
  51. package/.medusa/server/src/subscribers/product-created.js +34 -0
  52. package/.medusa/server/src/types/index.js +2 -0
  53. package/.medusa/server/src/ui/index.js +138 -0
  54. package/.medusa/server/src/workflows/apply-inventory-count.js +68 -0
  55. package/.medusa/server/src/workflows/batch-barcode-import.js +153 -0
  56. package/.medusa/server/src/workflows/bulk-generate-variant-barcodes.js +143 -0
  57. package/.medusa/server/src/workflows/generate-barcode-tag.js +44 -0
  58. package/.medusa/server/src/workflows/helpers.js +15 -0
  59. package/.medusa/server/src/workflows/index.js +7 -0
  60. package/.medusa/server/src/workflows/lookup-and-create-product.js +215 -0
  61. package/.medusa/server/src/workflows/scan-inventory-count.js +15 -0
  62. package/LICENSE +21 -0
  63. package/README.md +844 -2
  64. package/package.json +134 -3
  65. package/src/__tests__/barcode.spec.ts +836 -0
  66. package/src/__tests__/medusa-shim.mjs +331 -0
  67. package/src/__tests__/register-ts-resolver.mjs +135 -0
  68. package/src/admin/README.md +134 -0
  69. package/src/admin/components/barcode-studio-header.tsx +73 -0
  70. package/src/admin/components/batch-import-section.tsx +99 -0
  71. package/src/admin/components/dynamic-qr-tag-drawer-form.tsx +230 -0
  72. package/src/admin/components/dynamic-qr-tag-form.tsx +129 -0
  73. package/src/admin/components/dynamic-qr-tags-table.tsx +136 -0
  74. package/src/admin/components/gtin-lookup-section.tsx +155 -0
  75. package/src/admin/components/index.tsx +14 -0
  76. package/src/admin/components/label-designer-section.tsx +151 -0
  77. package/src/admin/components/label-output-section.tsx +63 -0
  78. package/src/admin/components/overview-cards.tsx +98 -0
  79. package/src/admin/components/product-barcode-card.tsx +127 -0
  80. package/src/admin/components/providers-cache-table.tsx +61 -0
  81. package/src/admin/components/providers-overview-section.tsx +49 -0
  82. package/src/admin/components/rbac-guard.tsx +153 -0
  83. package/src/admin/components/stock-take-scanner-section.tsx +116 -0
  84. package/src/admin/components/stock-take-sessions-section.tsx +109 -0
  85. package/src/admin/hooks/use-barcode-studio.ts +466 -0
  86. package/src/admin/hooks/use-product-barcodes.ts +139 -0
  87. package/src/admin/i18n/README.md +130 -0
  88. package/src/admin/i18n/index.ts +36 -0
  89. package/src/admin/i18n/json/ar.json +157 -0
  90. package/src/admin/i18n/json/de.json +157 -0
  91. package/src/admin/i18n/json/en.json +157 -0
  92. package/src/admin/i18n/json/es.json +157 -0
  93. package/src/admin/i18n/json/fr.json +157 -0
  94. package/src/admin/i18n/json/it.json +157 -0
  95. package/src/admin/i18n/json/nl.json +157 -0
  96. package/src/admin/i18n/json/pl.json +157 -0
  97. package/src/admin/i18n/json/pt.json +157 -0
  98. package/src/admin/i18n/json/tr.json +157 -0
  99. package/src/admin/routes/barcodes/lookup/page.tsx +75 -0
  100. package/src/admin/routes/barcodes/page.tsx +212 -0
  101. package/src/admin/routes/barcodes/print/page.tsx +67 -0
  102. package/src/admin/routes/barcodes/providers/page.tsx +66 -0
  103. package/src/admin/routes/barcodes/stock-take/page.tsx +74 -0
  104. package/src/admin/routes/barcodes/tags/@create/page.tsx +56 -0
  105. package/src/admin/routes/barcodes/tags/page.tsx +72 -0
  106. package/src/admin/tsconfig.json +20 -0
  107. package/src/admin/vite-env.d.ts +1 -0
  108. package/src/admin/widgets/product-barcode-widget.tsx +43 -0
  109. package/src/api/README.md +117 -0
  110. package/src/api/admin/barcodes/[barcode]/route.ts +64 -0
  111. package/src/api/admin/barcodes/batch/route.ts +12 -0
  112. package/src/api/admin/barcodes/bulk-generate/route.ts +12 -0
  113. package/src/api/admin/barcodes/cache/route.ts +35 -0
  114. package/src/api/admin/barcodes/count-sessions/[id]/apply/route.ts +17 -0
  115. package/src/api/admin/barcodes/count-sessions/[id]/scan/route.ts +17 -0
  116. package/src/api/admin/barcodes/count-sessions/route.ts +43 -0
  117. package/src/api/admin/barcodes/create-from-barcode/route.ts +16 -0
  118. package/src/api/admin/barcodes/lookup/route.ts +19 -0
  119. package/src/api/admin/barcodes/print/route.ts +18 -0
  120. package/src/api/admin/barcodes/route.ts +36 -0
  121. package/src/api/admin/barcodes/scan/route.ts +32 -0
  122. package/src/api/admin/barcodes/tags/[id]/route.ts +41 -0
  123. package/src/api/admin/barcodes/tags/route.ts +36 -0
  124. package/src/api/admin/barcodes/templates/route.ts +51 -0
  125. package/src/api/admin/plugin/route.ts +5 -0
  126. package/src/api/admin/variants/barcode/[code]/route.ts +36 -0
  127. package/src/api/middlewares.ts +91 -0
  128. package/src/api/store/barcodes/[code]/route.ts +43 -0
  129. package/src/api/store/barcodes/[code]/verify/route.ts +35 -0
  130. package/src/api/store/plugin/route.ts +5 -0
  131. package/src/api/store/variants/barcode/[code]/route.ts +36 -0
  132. package/src/api/validators.ts +217 -0
  133. package/src/hooks/index.ts +270 -0
  134. package/src/index.ts +4 -0
  135. package/src/jobs/README.md +54 -0
  136. package/src/jobs/purge-expired-barcode-cache.ts +18 -0
  137. package/src/links/README.md +44 -0
  138. package/src/modules/README.md +85 -0
  139. package/src/modules/barcode/index.ts +18 -0
  140. package/src/modules/barcode/migrations/Migration20260522000000.ts +225 -0
  141. package/src/modules/barcode/models/index.ts +168 -0
  142. package/src/modules/barcode/operations/context.ts +14 -0
  143. package/src/modules/barcode/operations/data-service.ts +10 -0
  144. package/src/modules/barcode/operations/generation.ts +57 -0
  145. package/src/modules/barcode/operations/index.ts +8 -0
  146. package/src/modules/barcode/operations/labels.ts +90 -0
  147. package/src/modules/barcode/operations/lookup.ts +202 -0
  148. package/src/modules/barcode/operations/maintenance.ts +53 -0
  149. package/src/modules/barcode/operations/stock-count.ts +217 -0
  150. package/src/modules/barcode/operations/tags.ts +425 -0
  151. package/src/modules/barcode/providers/index.ts +958 -0
  152. package/src/modules/barcode/service.ts +243 -0
  153. package/src/modules/barcode/types.ts +297 -0
  154. package/src/modules/barcode/utils/barcode-parser.ts +453 -0
  155. package/src/modules/barcode/utils/label-printer.ts +641 -0
  156. package/src/modules/barcode/utils/normalize-product.ts +524 -0
  157. package/src/modules/barcode/utils/security.ts +136 -0
  158. package/src/modules/barcode/utils/svg-renderer.ts +574 -0
  159. package/src/providers/README.md +105 -0
  160. package/src/rbac.ts +144 -0
  161. package/src/sdk/index.ts +253 -0
  162. package/src/subscribers/README.md +73 -0
  163. package/src/subscribers/product-created.ts +45 -0
  164. package/src/types/index.ts +1 -0
  165. package/src/types/medusa-ui.d.ts +118 -0
  166. package/src/ui/index.tsx +311 -0
  167. package/src/workflows/README.md +56 -0
  168. package/src/workflows/apply-inventory-count.ts +115 -0
  169. package/src/workflows/batch-barcode-import.ts +218 -0
  170. package/src/workflows/bulk-generate-variant-barcodes.ts +214 -0
  171. package/src/workflows/generate-barcode-tag.ts +85 -0
  172. package/src/workflows/helpers.ts +21 -0
  173. package/src/workflows/index.ts +6 -0
  174. package/src/workflows/lookup-and-create-product.ts +298 -0
  175. package/src/workflows/scan-inventory-count.ts +45 -0
  176. package/tsconfig.json +40 -0
@@ -0,0 +1,453 @@
1
+ import crypto from "node:crypto"
2
+
3
+ import type {
4
+ BarcodeSymbology,
5
+ Gs1ApplicationIdentifiers,
6
+ ParsedBarcode,
7
+ } from "../types"
8
+
9
+ /**
10
+ * Computes the GS1 Modulo-10 check digit for any standard GTIN data payload
11
+ * (EAN-8: 7 data digits, UPC-A: 11 data digits, EAN-13: 12 data digits, GTIN-14: 13 data digits).
12
+ *
13
+ * IMPORTANT: Weights alternate `3, 1, 3, 1...` starting from the RIGHTMOST data digit
14
+ * (immediately adjacent to the check digit) per official GS1 specification.
15
+ */
16
+ export function computeGs1CheckDigit(dataDigits: string): number {
17
+ const clean = dataDigits.replace(/\D/g, "")
18
+ if (!clean) {
19
+ return 0
20
+ }
21
+ let sum = 0
22
+ for (
23
+ let i = clean.length - 1, posFromRight = 0;
24
+ i >= 0;
25
+ i--, posFromRight++
26
+ ) {
27
+ const digit = Number(clean[i])
28
+ const weight = posFromRight % 2 === 0 ? 3 : 1
29
+ sum += digit * weight
30
+ }
31
+ return (10 - (sum % 10)) % 10
32
+ }
33
+
34
+ /**
35
+ * Computes the EAN-13 check digit for a 12-digit payload (compatible with `@bambiste/medusa-barcode`).
36
+ */
37
+ export function computeEan13CheckDigit(twelveDigits: string): number {
38
+ return computeGs1CheckDigit(twelveDigits.slice(0, 12))
39
+ }
40
+
41
+ /**
42
+ * Validates the GS1 Modulo-10 checksum of an 8, 12, 13, or 14 digit GTIN string.
43
+ */
44
+ export function validateGs1Checksum(digits: string): boolean {
45
+ if (!/^\d+$/.test(digits)) {
46
+ return false
47
+ }
48
+ if (![8, 12, 13, 14].includes(digits.length)) {
49
+ return false
50
+ }
51
+ const payload = digits.slice(0, -1)
52
+ const actualCheck = Number(digits[digits.length - 1])
53
+ return computeGs1CheckDigit(payload) === actualCheck
54
+ }
55
+
56
+ /**
57
+ * Converts a 12-digit UPC-A code to its 13-digit EAN-13 representation (`0` + UPC-A).
58
+ */
59
+ export function upcToEan13(upcA: string): string {
60
+ const digits = upcA.replace(/\D/g, "")
61
+ if (digits.length === 12) {
62
+ return `0${digits}`
63
+ }
64
+ return digits
65
+ }
66
+
67
+ /**
68
+ * Converts an 8-digit EAN-8 code to a 13-digit padded EAN-13 string.
69
+ */
70
+ export function ean8ToEan13(ean8: string): string {
71
+ const digits = ean8.replace(/\D/g, "")
72
+ if (digits.length === 8) {
73
+ const base12 = digits.slice(0, 7).padStart(12, "0")
74
+ return `${base12}${computeGs1CheckDigit(base12)}`
75
+ }
76
+ return digits
77
+ }
78
+
79
+ /**
80
+ * Expands a 6- or 8-digit UPC-E zero-suppressed barcode into a 12-digit UPC-A code.
81
+ */
82
+ export function expandUpcEToUpcA(upce: string): string {
83
+ const digits = upce.replace(/\D/g, "")
84
+ let numberSystem = "0"
85
+ let sixDigits = ""
86
+
87
+ if (digits.length === 6) {
88
+ sixDigits = digits
89
+ } else if (digits.length === 8 && (digits[0] === "0" || digits[0] === "1")) {
90
+ numberSystem = digits[0]
91
+ sixDigits = digits.slice(1, 7)
92
+ } else {
93
+ return digits
94
+ }
95
+
96
+ const [d1, d2, d3, d4, d5, d6] = sixDigits.split("")
97
+ let elevenDigits = ""
98
+
99
+ if (d6 === "0" || d6 === "1" || d6 === "2") {
100
+ elevenDigits = `${numberSystem}${d1}${d2}${d6}0000${d3}${d4}${d5}`
101
+ } else if (d6 === "3") {
102
+ elevenDigits = `${numberSystem}${d1}${d2}${d3}00000${d4}${d5}`
103
+ } else if (d6 === "4") {
104
+ elevenDigits = `${numberSystem}${d1}${d2}${d3}${d4}00000${d5}`
105
+ } else {
106
+ elevenDigits = `${numberSystem}${d1}${d2}${d3}${d4}${d5}0000${d6}`
107
+ }
108
+
109
+ const check = computeGs1CheckDigit(elevenDigits)
110
+ return `${elevenDigits}${check}`
111
+ }
112
+
113
+ /**
114
+ * Parses GS1-128 Application Identifiers from either parenthesized format
115
+ * `(01)09506000134352(10)BATCH1(17)261231(21)SN123` or GS1 Digital Link URLs.
116
+ */
117
+ export function parseGs1ApplicationIdentifiers(
118
+ raw: string
119
+ ): Gs1ApplicationIdentifiers | null {
120
+ const trimmed = raw.trim()
121
+ const rawAis: Record<string, string> = {}
122
+
123
+ if (trimmed.includes("(") && trimmed.includes(")")) {
124
+ const regex = /\((\d{2,4})\)\s*([^()]+)/g
125
+ let match: RegExpExecArray | null = regex.exec(trimmed)
126
+ while (match !== null) {
127
+ const ai = match[1]
128
+ const val = match[2]?.trim()
129
+ if (ai && val) {
130
+ rawAis[ai] = val
131
+ }
132
+ match = regex.exec(trimmed)
133
+ }
134
+ } else if (trimmed.includes("/01/")) {
135
+ try {
136
+ const url = trimmed.startsWith("http")
137
+ ? new URL(trimmed)
138
+ : new URL(trimmed, "https://gs1.local")
139
+ const parts = url.pathname.split("/").filter(Boolean)
140
+ for (let i = 0; i < parts.length - 1; i += 2) {
141
+ const key = parts[i]
142
+ const val = parts[i + 1]
143
+ if (key && val && /^\d{2,4}$/.test(key)) {
144
+ rawAis[key] = decodeURIComponent(val)
145
+ }
146
+ }
147
+ url.searchParams.forEach((val, key) => {
148
+ if (/^\d{2,4}$/.test(key)) {
149
+ rawAis[key] = val
150
+ }
151
+ })
152
+ } catch {
153
+ return null
154
+ }
155
+ }
156
+
157
+ if (Object.keys(rawAis).length === 0) {
158
+ return null
159
+ }
160
+
161
+ const result: Gs1ApplicationIdentifiers = { rawAis }
162
+ if (rawAis["01"]) {
163
+ result.gtin = rawAis["01"].replace(/\D/g, "")
164
+ if (result.gtin.length === 14 && result.gtin.startsWith("0")) {
165
+ result.ean13 = result.gtin.slice(1)
166
+ }
167
+ }
168
+ if (rawAis["10"]) {
169
+ result.batchLot = rawAis["10"]
170
+ }
171
+ if (rawAis["11"]) {
172
+ result.productionDate = formatGs1Date(rawAis["11"])
173
+ }
174
+ if (rawAis["15"]) {
175
+ result.bestBeforeDate = formatGs1Date(rawAis["15"])
176
+ }
177
+ if (rawAis["17"]) {
178
+ result.expiryDate = formatGs1Date(rawAis["17"])
179
+ }
180
+ if (rawAis["21"]) {
181
+ result.serialNumber = rawAis["21"]
182
+ }
183
+ if (rawAis["30"] || rawAis["37"]) {
184
+ const qty = Number(rawAis["30"] || rawAis["37"])
185
+ if (Number.isFinite(qty)) {
186
+ result.quantity = qty
187
+ }
188
+ }
189
+
190
+ for (const key of Object.keys(rawAis)) {
191
+ if (/^310[0-5]$/.test(key)) {
192
+ const decimals = Number(key[3])
193
+ const rawNum = Number(rawAis[key])
194
+ if (Number.isFinite(rawNum)) {
195
+ result.netWeightKg = rawNum / 10 ** decimals
196
+ }
197
+ }
198
+ }
199
+
200
+ return result
201
+ }
202
+
203
+ function formatGs1Date(yymmdd: string): string {
204
+ const clean = yymmdd.replace(/\D/g, "")
205
+ if (clean.length !== 6) {
206
+ return yymmdd
207
+ }
208
+ const yy = Number(clean.slice(0, 2))
209
+ const mm = clean.slice(2, 4)
210
+ const dd = clean.slice(4, 6) === "00" ? "01" : clean.slice(4, 6)
211
+ const fullYear = yy >= 50 ? 1900 + yy : 2000 + yy
212
+ return `${fullYear}-${mm}-${dd}`
213
+ }
214
+
215
+ /**
216
+ * Builds a standard GS1 Digital Link URI:
217
+ * `{baseUrl}/01/{14-digit-gtin}[/10/{batchLot}][/21/{serial}][?17={expiry}]`
218
+ */
219
+ export function buildGs1DigitalLink(options: {
220
+ baseUrl: string
221
+ gtin: string
222
+ batchLot?: string | null
223
+ serial?: string | null
224
+ expiry?: string | null
225
+ }): string {
226
+ const base = options.baseUrl.replace(/\/$/, "")
227
+ const gtin14 = options.gtin.replace(/\D/g, "").padStart(14, "0").slice(0, 14)
228
+ let path = `${base}/01/${gtin14}`
229
+ if (options.batchLot) {
230
+ path += `/10/${encodeURIComponent(options.batchLot.trim())}`
231
+ }
232
+ if (options.serial) {
233
+ path += `/21/${encodeURIComponent(options.serial.trim())}`
234
+ }
235
+ if (options.expiry) {
236
+ path += `?17=${encodeURIComponent(options.expiry.trim())}`
237
+ }
238
+ return path
239
+ }
240
+
241
+ /**
242
+ * Generates a valid GS1 in-store EAN-13 barcode using prefix `200`–`299`.
243
+ * Supports either deterministic hashing from a variant ID/SKU or sequential numbering.
244
+ */
245
+ export function generateInternalEan13(
246
+ seedOrSequence: string | number,
247
+ prefix = "200",
248
+ sequence?: number
249
+ ): string {
250
+ const cleanPrefix =
251
+ /^\d{3}$/.test(prefix) && prefix.startsWith("2") ? prefix : "200"
252
+
253
+ let nineDigits: string
254
+ if (
255
+ typeof sequence === "number" &&
256
+ Number.isFinite(sequence) &&
257
+ sequence >= 0
258
+ ) {
259
+ nineDigits = String(Math.floor(sequence) % 1_000_000_000).padStart(9, "0")
260
+ } else if (typeof seedOrSequence === "number") {
261
+ nineDigits = String(Math.floor(seedOrSequence) % 1_000_000_000).padStart(
262
+ 9,
263
+ "0"
264
+ )
265
+ } else {
266
+ const hash = crypto
267
+ .createHash("sha256")
268
+ .update(String(seedOrSequence), "utf8")
269
+ .digest("hex")
270
+ const numeric = BigInt(`0x${hash.slice(0, 14)}`) % 1_000_000_000n
271
+ nineDigits = numeric.toString().padStart(9, "0")
272
+ }
273
+
274
+ const twelve = `${cleanPrefix}${nineDigits}`
275
+ const check = computeGs1CheckDigit(twelve)
276
+ return `${twelve}${check}`
277
+ }
278
+
279
+ /**
280
+ * Parses and validates any 1D/2D barcode string (EAN-13, ISBN-13, UPC-A, UPC-E, EAN-8, GTIN-14, GS1-128, Code128, Code39, QR).
281
+ */
282
+ export function parseBarcode(rawInput: string): ParsedBarcode {
283
+ const raw = String(rawInput ?? "").trim()
284
+ if (!raw) {
285
+ return {
286
+ raw: "",
287
+ normalized: "",
288
+ type: "unknown",
289
+ isValid: false,
290
+ isIsbn: false,
291
+ checkDigit: null,
292
+ expectedCheckDigit: null,
293
+ ean13Equivalent: null,
294
+ gs1: null,
295
+ }
296
+ }
297
+
298
+ // Check for GS1-128 parenthesized or Digital Link format first
299
+ const gs1 = parseGs1ApplicationIdentifiers(raw)
300
+ if (gs1) {
301
+ const gtin = gs1.gtin || ""
302
+ const isValidGtin = gtin.length === 14 ? validateGs1Checksum(gtin) : true
303
+ const ean13 =
304
+ gtin.length === 14 && gtin.startsWith("0")
305
+ ? gtin.slice(1)
306
+ : gtin.length === 13
307
+ ? gtin
308
+ : null
309
+ return {
310
+ raw,
311
+ normalized: ean13 || gtin || raw,
312
+ type: "gs1-128",
313
+ isValid: isValidGtin,
314
+ isIsbn: Boolean(
315
+ ean13 && (ean13.startsWith("978") || ean13.startsWith("979"))
316
+ ),
317
+ checkDigit: gtin ? Number(gtin[gtin.length - 1]) : null,
318
+ expectedCheckDigit: gtin ? computeGs1CheckDigit(gtin.slice(0, -1)) : null,
319
+ ean13Equivalent: ean13,
320
+ gs1,
321
+ }
322
+ }
323
+
324
+ // Strip hyphens/spaces if remainder is purely digits (e.g. ISBN 978-0-14-032872-1)
325
+ const digitsOnly = raw.replace(/[\s-]/g, "")
326
+ if (/^\d+$/.test(digitsOnly)) {
327
+ const len = digitsOnly.length
328
+
329
+ if (len === 13) {
330
+ const expected = computeGs1CheckDigit(digitsOnly.slice(0, 12))
331
+ const actual = Number(digitsOnly[12])
332
+ const isValid = expected === actual
333
+ const isIsbn =
334
+ digitsOnly.startsWith("978") || digitsOnly.startsWith("979")
335
+ return {
336
+ raw,
337
+ normalized: digitsOnly,
338
+ type: isIsbn ? "isbn13" : "ean13",
339
+ isValid,
340
+ isIsbn,
341
+ checkDigit: actual,
342
+ expectedCheckDigit: expected,
343
+ ean13Equivalent: digitsOnly,
344
+ gs1: null,
345
+ }
346
+ }
347
+
348
+ if (len === 12) {
349
+ const expected = computeGs1CheckDigit(digitsOnly.slice(0, 11))
350
+ const actual = Number(digitsOnly[11])
351
+ const isValid = expected === actual
352
+ const ean13 = upcToEan13(digitsOnly)
353
+ return {
354
+ raw,
355
+ normalized: ean13,
356
+ type: "upca",
357
+ isValid,
358
+ isIsbn: false,
359
+ checkDigit: actual,
360
+ expectedCheckDigit: expected,
361
+ ean13Equivalent: ean13,
362
+ expandedUpcA: digitsOnly,
363
+ gs1: null,
364
+ }
365
+ }
366
+
367
+ if (len === 8) {
368
+ const expected = computeGs1CheckDigit(digitsOnly.slice(0, 7))
369
+ const actual = Number(digitsOnly[7])
370
+ const isValid = expected === actual
371
+ return {
372
+ raw,
373
+ normalized: digitsOnly,
374
+ type: "ean8",
375
+ isValid,
376
+ isIsbn: false,
377
+ checkDigit: actual,
378
+ expectedCheckDigit: expected,
379
+ ean13Equivalent: ean8ToEan13(digitsOnly),
380
+ gs1: null,
381
+ }
382
+ }
383
+
384
+ if (len === 6) {
385
+ const expanded = expandUpcEToUpcA(digitsOnly)
386
+ const ean13 = upcToEan13(expanded)
387
+ return {
388
+ raw,
389
+ normalized: ean13,
390
+ type: "upce",
391
+ isValid: validateGs1Checksum(expanded),
392
+ isIsbn: false,
393
+ checkDigit: Number(expanded[11]),
394
+ expectedCheckDigit: computeGs1CheckDigit(expanded.slice(0, 11)),
395
+ ean13Equivalent: ean13,
396
+ expandedUpcA: expanded,
397
+ gs1: null,
398
+ }
399
+ }
400
+
401
+ if (len === 14) {
402
+ const expected = computeGs1CheckDigit(digitsOnly.slice(0, 13))
403
+ const actual = Number(digitsOnly[13])
404
+ const isValid = expected === actual
405
+ const ean13 = digitsOnly.startsWith("0") ? digitsOnly.slice(1) : null
406
+ return {
407
+ raw,
408
+ normalized: digitsOnly,
409
+ type: "gtin14",
410
+ isValid,
411
+ isIsbn: false,
412
+ checkDigit: actual,
413
+ expectedCheckDigit: expected,
414
+ ean13Equivalent: ean13,
415
+ gs1: null,
416
+ }
417
+ }
418
+ }
419
+
420
+ // URL or URI -> QR
421
+ if (/^https?:\/\//i.test(raw)) {
422
+ return {
423
+ raw,
424
+ normalized: raw,
425
+ type: "qr",
426
+ isValid: true,
427
+ isIsbn: false,
428
+ checkDigit: null,
429
+ expectedCheckDigit: null,
430
+ ean13Equivalent: null,
431
+ gs1: null,
432
+ }
433
+ }
434
+
435
+ // Code39 vs Code128 alphanumeric fallback
436
+ const symbology: BarcodeSymbology = /^[A-Z0-9\-.\s$/+%]+$/.test(raw)
437
+ ? "code39"
438
+ : /^[\x20-\x7E]+$/.test(raw)
439
+ ? "code128"
440
+ : "unknown"
441
+
442
+ return {
443
+ raw,
444
+ normalized: raw,
445
+ type: symbology,
446
+ isValid: symbology !== "unknown" && raw.length <= 128,
447
+ isIsbn: false,
448
+ checkDigit: null,
449
+ expectedCheckDigit: null,
450
+ ean13Equivalent: null,
451
+ gs1: null,
452
+ }
453
+ }