@promr-acorda/core 0.0.1 → 0.1.1

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,99 +1,99 @@
1
- import { PDFDocument as I, StandardFonts as N, rgb as p } from "pdf-lib";
2
- const g = (e) => typeof e == "object" && e !== null && "type" in e && e.type === "signature", ie = (e) => typeof e != "object", S = (e) => Object.prototype.toString.call(e) === "[object Uint8Array]", u = (e) => !g(e) || !S(e.image) ? e : {
1
+ import { PDFDocument as T, StandardFonts as N, rgb as w } from "pdf-lib";
2
+ const g = (e) => typeof e == "object" && e !== null && "type" in e && e.type === "signature", de = (e) => typeof e != "object", M = (e) => Object.prototype.toString.call(e) === "[object Uint8Array]", u = (e) => !g(e) || !M(e.image) ? e : {
3
3
  ...e,
4
4
  image: Uint8Array.from(e.image)
5
- }, D = /* @__PURE__ */ new Set([
5
+ }, $ = /* @__PURE__ */ new Set([
6
6
  "image/png",
7
7
  "image/jpeg",
8
8
  "image/jpg"
9
- ]), $ = (e) => {
9
+ ]), C = (e) => {
10
10
  const t = e == null ? void 0 : e.trim().toLowerCase();
11
- if (t && D.has(t))
11
+ if (t && $.has(t))
12
12
  return t === "image/jpg" ? "image/jpeg" : t;
13
- }, C = (e) => {
13
+ }, R = (e) => {
14
14
  if (e.length >= 8 && e[0] === 137 && e[1] === 80 && e[2] === 78 && e[3] === 71 && e[4] === 13 && e[5] === 10 && e[6] === 26 && e[7] === 10)
15
15
  return "image/png";
16
16
  if (e.length >= 3 && e[0] === 255 && e[1] === 216 && e[2] === 255)
17
17
  return "image/jpeg";
18
18
  }, v = (e) => {
19
- const t = C(e.image), r = $(e.mimeType);
19
+ const t = R(e.image), r = C(e.mimeType);
20
20
  if (!(r && t && r !== t))
21
21
  return r ?? t;
22
- }, K = 5e-3, R = 0, w = 1, L = (e, t) => Math.max(0, Math.min(t - 1, Math.floor(e))), b = (e) => Math.max(R, Math.min(w, e)), m = (e) => Math.max(K, e), T = (e, t) => {
23
- const r = L(e.page, t), s = b(e.x), n = b(e.y), o = m(e.width), i = m(e.height), a = Math.min(o, w - s), d = Math.min(i, w - n);
22
+ }, K = 5e-3, L = 0, x = 1, k = (e, t) => Math.max(0, Math.min(t - 1, Math.floor(e))), A = (e) => Math.max(L, Math.min(x, e)), m = (e) => Math.max(K, e), y = (e, t) => {
23
+ const r = k(e.page, t), n = A(e.x), i = A(e.y), s = m(e.width), o = m(e.height), a = Math.min(s, x - n), d = Math.min(o, x - i);
24
24
  return {
25
25
  page: r,
26
- x: s,
27
- y: n,
26
+ x: n,
27
+ y: i,
28
28
  width: m(a),
29
29
  height: m(d)
30
30
  };
31
- }, M = (e, t, r) => ({
31
+ }, S = (e, t, r) => ({
32
32
  x: e.x * t,
33
33
  y: e.y * r,
34
34
  width: e.width * t,
35
35
  height: e.height * r
36
- }), ae = (e, t, r) => ({
36
+ }), he = (e, t, r) => ({
37
37
  page: e.page,
38
38
  x: e.x / t,
39
39
  y: e.y / r,
40
40
  width: e.width / t,
41
41
  height: e.height / r
42
- }), de = (e, t) => e.find((r) => r.sharedKey === t && r.sharedMode === "source"), ce = (e, t) => e.filter((r) => r.sharedKey === t && r.sharedMode === "mirror"), f = (e, t, r) => {
42
+ }), ce = (e, t) => e.find((r) => r.sharedKey === t && r.sharedMode === "source"), le = (e, t) => e.filter((r) => r.sharedKey === t && r.sharedMode === "mirror"), f = (e, t, r) => {
43
43
  if (e.sharedKey) {
44
- const o = r[e.sharedKey];
45
- if (o !== void 0) return u(o);
44
+ const s = r[e.sharedKey];
45
+ if (s !== void 0) return u(s);
46
46
  }
47
- const s = t[e.id];
48
- if (s !== void 0) return u(s);
49
- const n = e.defaultValue;
50
- return n !== void 0 ? u(n) : void 0;
51
- }, k = (e, t, r, s) => {
47
+ const n = t[e.id];
48
+ if (n !== void 0) return u(n);
49
+ const i = e.defaultValue;
50
+ return i !== void 0 ? u(i) : void 0;
51
+ }, j = (e, t, r, n) => {
52
52
  if (!e.some(
53
- (o) => o.sharedKey === r && o.sharedMode === "source"
53
+ (s) => s.sharedKey === r && s.sharedMode === "source"
54
54
  ))
55
55
  throw new Error(
56
56
  `Cannot set shared value: no source field exists for key "${r}".`
57
57
  );
58
- return { ...t, [r]: u(s) };
59
- }, he = (e, t, r) => {
60
- const s = Object.fromEntries(
61
- Object.entries(t).map(([n, o]) => [
62
- n,
63
- u(o)
58
+ return { ...t, [r]: u(n) };
59
+ }, ue = (e, t, r) => {
60
+ const n = Object.fromEntries(
61
+ Object.entries(t).map(([i, s]) => [
62
+ i,
63
+ u(s)
64
64
  ])
65
65
  );
66
- for (const n of e) {
67
- const o = f(n, t, r);
68
- o !== void 0 && (s[n.id] = o);
66
+ for (const i of e) {
67
+ const s = f(i, t, r);
68
+ s !== void 0 && (n[i.id] = s);
69
69
  }
70
- return s;
70
+ return n;
71
71
  }, V = 5 * 1024 * 1024, l = (e, t, r) => {
72
- var s;
72
+ var n;
73
73
  return {
74
74
  fieldId: e.id,
75
75
  fieldName: e.name,
76
- message: ((s = e.validation) == null ? void 0 : s.customMessage) ?? t,
76
+ message: ((n = e.validation) == null ? void 0 : n.customMessage) ?? t,
77
77
  code: r
78
78
  };
79
- }, j = (e, t) => {
80
- const r = [], { validation: s } = e;
81
- if (!s) return r;
82
- if (s.minLength !== void 0 && t.length < s.minLength && r.push(
79
+ }, z = (e, t) => {
80
+ const r = [], { validation: n } = e;
81
+ if (!n) return r;
82
+ if (n.minLength !== void 0 && t.length < n.minLength && r.push(
83
83
  l(
84
84
  e,
85
- `Must be at least ${s.minLength} characters.`,
85
+ `Must be at least ${n.minLength} characters.`,
86
86
  "MIN_LENGTH"
87
87
  )
88
- ), s.maxLength !== void 0 && t.length > s.maxLength && r.push(
88
+ ), n.maxLength !== void 0 && t.length > n.maxLength && r.push(
89
89
  l(
90
90
  e,
91
- `Must be at most ${s.maxLength} characters.`,
91
+ `Must be at most ${n.maxLength} characters.`,
92
92
  "MAX_LENGTH"
93
93
  )
94
- ), s.pattern !== void 0)
94
+ ), n.pattern !== void 0)
95
95
  try {
96
- new RegExp(s.pattern).test(t) || r.push(
96
+ new RegExp(n.pattern).test(t) || r.push(
97
97
  l(
98
98
  e,
99
99
  "The value does not match the required format.",
@@ -110,29 +110,29 @@ const g = (e) => typeof e == "object" && e !== null && "type" in e && e.type ===
110
110
  );
111
111
  }
112
112
  return r;
113
- }, z = (e, t) => {
114
- const r = [], s = e;
115
- return s.min !== void 0 && t < s.min && r.push(
116
- l(e, `Minimum value is ${s.min}.`, "MIN_VALUE")
117
- ), s.max !== void 0 && t > s.max && r.push(
118
- l(e, `Maximum value is ${s.max}.`, "MAX_VALUE")
113
+ }, U = (e, t) => {
114
+ const r = [], n = e;
115
+ return n.min !== void 0 && t < n.min && r.push(
116
+ l(e, `Minimum value is ${n.min}.`, "MIN_VALUE")
117
+ ), n.max !== void 0 && t > n.max && r.push(
118
+ l(e, `Maximum value is ${n.max}.`, "MAX_VALUE")
119
119
  ), r;
120
- }, x = (e, t) => {
120
+ }, E = (e, t) => {
121
121
  if (e.sharedMode === "mirror")
122
122
  return { valid: !0, errors: [] };
123
- const r = [], s = t === void 0 || t === "" || t === null || typeof t == "boolean" && !t && e.type !== "checkbox";
124
- if (e.required && s)
123
+ const r = [], n = t === void 0 || t === "" || t === null || typeof t == "boolean" && !t && e.type !== "checkbox";
124
+ if (e.required && n)
125
125
  return r.push(l(e, "This field is required.", "REQUIRED")), { valid: !1, errors: r };
126
126
  if (t === void 0 || t === "")
127
127
  return { valid: !0, errors: [] };
128
128
  if (e.type === "checkbox" && typeof t != "boolean" || e.type === "number" && (typeof t != "number" || !Number.isFinite(t)) || e.type === "signature" && !g(t) || e.type !== "checkbox" && e.type !== "number" && e.type !== "signature" && typeof t != "string")
129
129
  return r.push(l(e, "The value type is invalid for this field.", "INVALID_TYPE")), { valid: !1, errors: r };
130
130
  if (typeof t == "string")
131
- r.push(...j(e, t));
132
- else if (typeof t == "number")
133
131
  r.push(...z(e, t));
132
+ else if (typeof t == "number")
133
+ r.push(...U(e, t));
134
134
  else if (g(t)) {
135
- if (!S(t.image) || t.image.length === 0)
135
+ if (!M(t.image) || t.image.length === 0)
136
136
  return r.push(l(e, "Image data is invalid.", "INVALID_TYPE")), { valid: !1, errors: r };
137
137
  t.image.length > V && r.push(
138
138
  l(
@@ -149,93 +149,93 @@ const g = (e) => typeof e == "object" && e !== null && "type" in e && e.type ===
149
149
  );
150
150
  }
151
151
  return { valid: r.length === 0, errors: r };
152
- }, U = (e, t, r, s) => {
153
- const o = e.filter((a) => a.sharedKey === s).find((a) => a.sharedMode === "source");
154
- if (!o)
152
+ }, G = (e, t, r, n) => {
153
+ const s = e.filter((a) => a.sharedKey === n).find((a) => a.sharedMode === "source");
154
+ if (!s)
155
155
  return {
156
156
  valid: !1,
157
157
  errors: [
158
158
  {
159
- fieldId: s,
160
- fieldName: s,
161
- message: `No source field found for shared key "${s}".`,
159
+ fieldId: n,
160
+ fieldName: n,
161
+ message: `No source field found for shared key "${n}".`,
162
162
  code: "SHARED_SOURCE_NOT_FOUND"
163
163
  }
164
164
  ]
165
165
  };
166
- const i = f(o, t, r);
167
- return x(o, i);
168
- }, le = (e) => {
169
- const { fields: t, fieldValues: r, sharedValues: s } = e, n = /* @__PURE__ */ new Set(), o = [];
170
- for (const i of t)
171
- if (i.sharedMode !== "mirror")
172
- if (i.sharedKey) {
173
- if (n.has(i.sharedKey)) continue;
174
- n.add(i.sharedKey);
175
- const a = U(
166
+ const o = f(s, t, r);
167
+ return E(s, o);
168
+ }, fe = (e) => {
169
+ const { fields: t, fieldValues: r, sharedValues: n } = e, i = /* @__PURE__ */ new Set(), s = [];
170
+ for (const o of t)
171
+ if (o.sharedMode !== "mirror")
172
+ if (o.sharedKey) {
173
+ if (i.has(o.sharedKey)) continue;
174
+ i.add(o.sharedKey);
175
+ const a = G(
176
176
  t,
177
177
  r,
178
- s,
179
- i.sharedKey
178
+ n,
179
+ o.sharedKey
180
180
  );
181
- o.push(...a.errors);
181
+ s.push(...a.errors);
182
182
  } else {
183
- const a = f(i, r, s), d = x(i, a);
184
- o.push(...d.errors);
183
+ const a = f(o, r, n), d = E(o, a);
184
+ s.push(...d.errors);
185
185
  }
186
- return { valid: o.length === 0, errors: o };
187
- }, G = /* @__PURE__ */ new Set(["y", "Y", "M", "m", "d", "D", "h", "H", "s", "S"]), H = /* @__PURE__ */ new Set(["\\", "^", "$", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", "|"]), Y = (e) => H.has(e) ? `\\${e}` : e, q = (e) => {
186
+ return { valid: s.length === 0, errors: s };
187
+ }, H = /* @__PURE__ */ new Set(["y", "Y", "M", "m", "d", "D", "h", "H", "s", "S"]), Y = /* @__PURE__ */ new Set(["\\", "^", "$", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", "|"]), q = (e) => Y.has(e) ? `\\${e}` : e, W = (e) => {
188
188
  let t = "";
189
189
  for (let r = 0; r < e.length; ) {
190
- const s = e[r];
191
- if (s === void 0) break;
192
- if (G.has(s)) {
193
- let n = 1;
194
- for (; e[r + n] === s; )
195
- n += 1;
196
- t += `\\d{${n}}`, r += n;
190
+ const n = e[r];
191
+ if (n === void 0) break;
192
+ if (H.has(n)) {
193
+ let i = 1;
194
+ for (; e[r + i] === n; )
195
+ i += 1;
196
+ t += `\\d{${i}}`, r += i;
197
197
  continue;
198
198
  }
199
- t += Y(s), r += 1;
199
+ t += q(n), r += 1;
200
200
  }
201
201
  return t;
202
- }, ue = (e, t) => {
202
+ }, ge = (e, t) => {
203
203
  const r = e.trim();
204
- return r ? new RegExp(`^(?:${q(r)})$`).test(t) : !0;
205
- }, O = /^(\d{4})-(\d{2})-(\d{2})$/, fe = (e) => O.test(e), X = (e, t) => {
206
- const r = O.exec(e), s = t == null ? void 0 : t.trim();
207
- if (!r || !s) return e;
208
- const [, n = "", o = "", i = ""] = r;
204
+ return r ? new RegExp(`^(?:${W(r)})$`).test(t) : !0;
205
+ }, O = /^(\d{4})-(\d{2})-(\d{2})$/, pe = (e) => O.test(e), X = (e, t) => {
206
+ const r = O.exec(e), n = t == null ? void 0 : t.trim();
207
+ if (!r || !n) return e;
208
+ const [, i = "", s = "", o = ""] = r;
209
209
  let a = "";
210
- for (let d = 0; d < s.length; ) {
211
- const h = s[d];
212
- if (h === void 0) break;
213
- let c = 1;
214
- for (; s[d + c] === h; )
215
- c += 1;
216
- if (h === "y" || h === "Y") {
217
- a += c === 2 ? n.slice(-2) : n.padStart(c, "0"), d += c;
210
+ for (let d = 0; d < n.length; ) {
211
+ const c = n[d];
212
+ if (c === void 0) break;
213
+ let h = 1;
214
+ for (; n[d + h] === c; )
215
+ h += 1;
216
+ if (c === "y" || c === "Y") {
217
+ a += h === 2 ? i.slice(-2) : i.padStart(h, "0"), d += h;
218
218
  continue;
219
219
  }
220
- if (h === "M" || h === "m") {
221
- a += c === 1 ? String(Number(o)) : o.padStart(c, "0"), d += c;
220
+ if (c === "M" || c === "m") {
221
+ a += h === 1 ? String(Number(s)) : s.padStart(h, "0"), d += h;
222
222
  continue;
223
223
  }
224
- if (h === "d" || h === "D") {
225
- a += c === 1 ? String(Number(i)) : i.padStart(c, "0"), d += c;
224
+ if (c === "d" || c === "D") {
225
+ a += h === 1 ? String(Number(o)) : o.padStart(h, "0"), d += h;
226
226
  continue;
227
227
  }
228
- a += h, d += 1;
228
+ a += c, d += 1;
229
229
  }
230
230
  return a;
231
231
  }, _ = (e) => {
232
232
  const t = /* @__PURE__ */ new Map();
233
233
  for (const r of e) {
234
234
  if (!r.sharedKey || r.sharedMode !== "source") continue;
235
- const s = t.get(r.sharedKey);
236
- if (s !== void 0)
235
+ const n = t.get(r.sharedKey);
236
+ if (n !== void 0)
237
237
  throw new Error(
238
- `Shared key "${r.sharedKey}" allows only one source field. (Conflict: "${s}" vs "${r.id}")`
238
+ `Shared key "${r.sharedKey}" allows only one source field. (Conflict: "${n}" vs "${r.id}")`
239
239
  );
240
240
  t.set(r.sharedKey, r.id);
241
241
  }
@@ -249,7 +249,7 @@ const g = (e) => typeof e == "object" && e !== null && "type" in e && e.type ===
249
249
  case "number":
250
250
  return typeof t == "number" && Number.isFinite(t);
251
251
  case "signature":
252
- return g(t) && S(t.image);
252
+ return g(t) && M(t.image);
253
253
  case "text":
254
254
  case "date":
255
255
  case "email":
@@ -259,93 +259,113 @@ const g = (e) => typeof e == "object" && e !== null && "type" in e && e.type ===
259
259
  default:
260
260
  return !1;
261
261
  }
262
- }, W = (e, t) => !(t.page !== void 0 && e.page !== t.page || t.type !== void 0 && e.type !== t.type || t.required !== void 0 && (e.required ?? !1) !== t.required || t.hidden !== void 0 && (e.hidden ?? !1) !== t.hidden || t.readonly !== void 0 && (e.readonly ?? !1) !== t.readonly || t.sharedKey !== void 0 && e.sharedKey !== t.sharedKey || t.sharedMode !== void 0 && e.sharedMode !== t.sharedMode), Q = (e) => ({
262
+ }, Q = (e, t) => !(t.page !== void 0 && e.page !== t.page || t.type !== void 0 && e.type !== t.type || t.required !== void 0 && (e.required ?? !1) !== t.required || t.hidden !== void 0 && (e.hidden ?? !1) !== t.hidden || t.readonly !== void 0 && (e.readonly ?? !1) !== t.readonly || t.sharedKey !== void 0 && e.sharedKey !== t.sharedKey || t.sharedMode !== void 0 && e.sharedMode !== t.sharedMode), D = (e, t) => {
263
+ const r = y(e, t.pageCount);
264
+ if (e.type !== "checkbox") return r;
265
+ const n = t.pages.find((s) => s.index === r.page);
266
+ if (!n || n.width <= 0 || n.height <= 0) {
267
+ const s = Math.min(r.width, r.height);
268
+ return y({ ...r, width: s, height: s }, t.pageCount);
269
+ }
270
+ const i = Math.min(
271
+ r.width * n.width,
272
+ r.height * n.height
273
+ );
274
+ return y(
275
+ {
276
+ ...r,
277
+ width: i / n.width,
278
+ height: i / n.height
279
+ },
280
+ t.pageCount
281
+ );
282
+ }, Z = (e) => ({
263
283
  ...e,
264
284
  fields: [],
265
285
  fieldValues: {},
266
286
  sharedValues: {},
267
287
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
268
288
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
269
- }), Z = async (e) => (await I.load(e)).getPages().map((r, s) => ({
270
- index: s,
289
+ }), ee = async (e) => (await T.load(e)).getPages().map((r, n) => ({
290
+ index: n,
271
291
  width: r.getWidth(),
272
292
  height: r.getHeight()
273
- })), ge = async (e) => {
274
- const t = await Z(e.pdfData);
275
- return Q({
293
+ })), me = async (e) => {
294
+ const t = await ee(e.pdfData);
295
+ return Z({
276
296
  ...e,
277
297
  pageCount: t.length,
278
298
  pages: t
279
299
  });
280
300
  }, ye = (e, t) => {
281
301
  B(e.fields, t.id);
282
- const r = T(t, e.pageCount), s = { ...t, ...r }, n = [...e.fields, s];
283
- return _(n), {
302
+ const r = D(t, e), n = { ...t, ...r }, i = [...e.fields, n];
303
+ return _(i), {
284
304
  ...e,
285
- fields: n,
305
+ fields: i,
286
306
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
287
307
  };
288
308
  }, P = (e, t, r) => {
289
- const s = e.fields.map((n) => {
290
- if (n.id !== t) return n;
291
- const o = { ...n, ...r }, i = T(o, e.pageCount);
292
- return { ...o, ...i };
309
+ const n = e.fields.map((i) => {
310
+ if (i.id !== t) return i;
311
+ const s = { ...i, ...r }, o = D(s, e);
312
+ return { ...s, ...o };
293
313
  });
294
- return _(s), {
314
+ return _(n), {
295
315
  ...e,
296
- fields: s,
316
+ fields: n,
297
317
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
298
318
  };
299
- }, me = (e, t) => {
300
- const r = e.fields.find((i) => i.id === t), s = e.fields.filter((i) => i.id !== t), n = Object.fromEntries(
301
- Object.entries(e.fieldValues).filter(([i]) => i !== t)
319
+ }, we = (e, t) => {
320
+ const r = e.fields.find((o) => o.id === t), n = e.fields.filter((o) => o.id !== t), i = Object.fromEntries(
321
+ Object.entries(e.fieldValues).filter(([o]) => o !== t)
302
322
  );
303
- let { sharedValues: o } = e;
304
- return r != null && r.sharedKey && r.sharedMode === "source" && (s.some(
323
+ let { sharedValues: s } = e;
324
+ return r != null && r.sharedKey && r.sharedMode === "source" && (n.some(
305
325
  (a) => a.sharedKey === r.sharedKey && a.sharedMode === "source"
306
- ) || (o = Object.fromEntries(
307
- Object.entries(o).filter(([a]) => a !== r.sharedKey)
326
+ ) || (s = Object.fromEntries(
327
+ Object.entries(s).filter(([a]) => a !== r.sharedKey)
308
328
  ))), {
309
329
  ...e,
310
- fields: s,
311
- fieldValues: n,
312
- sharedValues: o,
330
+ fields: n,
331
+ fieldValues: i,
332
+ sharedValues: s,
313
333
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
314
334
  };
315
- }, pe = (e, t, r) => {
316
- const { page: s, x: n, y: o } = r;
335
+ }, xe = (e, t, r) => {
336
+ const { page: n, x: i, y: s } = r;
317
337
  return P(
318
338
  e,
319
339
  t,
320
- s !== void 0 ? { page: s, x: n, y: o } : { x: n, y: o }
340
+ n !== void 0 ? { page: n, x: i, y: s } : { x: i, y: s }
321
341
  );
322
- }, we = (e, t, r) => P(e, t, {
342
+ }, Me = (e, t, r) => P(e, t, {
323
343
  width: r.width,
324
344
  height: r.height
325
345
  }), Se = (e, t, r) => {
326
- const s = e.fields.find((n) => n.id === t);
327
- if (!s)
346
+ const n = e.fields.find((i) => i.id === t);
347
+ if (!n)
328
348
  throw new Error(`Field ID "${t}" was not found.`);
329
- if (s.sharedMode === "mirror")
349
+ if (n.sharedMode === "mirror")
330
350
  throw new Error(
331
351
  `Cannot set value on mirror field "${t}". Set the value on the source field instead.`
332
352
  );
333
- if (s.readonly)
353
+ if (n.readonly)
334
354
  throw new Error(`Cannot set value on read-only field "${t}".`);
335
- if (!J(s, r))
355
+ if (!J(n, r))
336
356
  throw new Error(
337
- `Value for field "${t}" is incompatible with field type "${s.type}".`
357
+ `Value for field "${t}" is incompatible with field type "${n.type}".`
338
358
  );
339
- if (s.sharedKey && s.sharedMode === "source") {
340
- const n = k(
359
+ if (n.sharedKey && n.sharedMode === "source") {
360
+ const i = j(
341
361
  e.fields,
342
362
  e.sharedValues,
343
- s.sharedKey,
363
+ n.sharedKey,
344
364
  r
345
365
  );
346
366
  return {
347
367
  ...e,
348
- sharedValues: n,
368
+ sharedValues: i,
349
369
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
350
370
  };
351
371
  }
@@ -357,8 +377,8 @@ const g = (e) => typeof e == "object" && e !== null && "type" in e && e.type ===
357
377
  },
358
378
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
359
379
  };
360
- }, Me = (e, t) => {
361
- const r = e.fields.find((o) => o.id === t);
380
+ }, Ee = (e, t) => {
381
+ const r = e.fields.find((s) => s.id === t);
362
382
  if (!r)
363
383
  throw new Error(`Field ID "${t}" was not found.`);
364
384
  if (r.sharedMode === "mirror")
@@ -368,182 +388,197 @@ const g = (e) => typeof e == "object" && e !== null && "type" in e && e.type ===
368
388
  if (r.readonly)
369
389
  throw new Error(`Cannot clear value on read-only field "${t}".`);
370
390
  if (r.sharedKey && r.sharedMode === "source") {
371
- const { [r.sharedKey]: o, ...i } = e.sharedValues;
391
+ const { [r.sharedKey]: s, ...o } = e.sharedValues;
372
392
  return {
373
393
  ...e,
374
- sharedValues: i,
394
+ sharedValues: o,
375
395
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
376
396
  };
377
397
  }
378
- const { [t]: s, ...n } = e.fieldValues;
398
+ const { [t]: n, ...i } = e.fieldValues;
379
399
  return {
380
400
  ...e,
381
- fieldValues: n,
401
+ fieldValues: i,
382
402
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
383
403
  };
384
- }, xe = (e, t) => {
385
- const r = e.fields.find((s) => s.id === t);
404
+ }, Fe = (e, t) => {
405
+ const r = e.fields.find((n) => n.id === t);
386
406
  return r ? { ...r } : void 0;
387
- }, Ee = (e, t = {}) => e.fields.filter((r) => W(r, t)).map((r) => ({ ...r })), Fe = (e, t) => {
388
- const r = e.fields.find((s) => s.id === t);
407
+ }, be = (e, t = {}) => e.fields.filter((r) => Q(r, t)).map((r) => ({ ...r })), Ae = (e, t) => {
408
+ const r = e.fields.find((n) => n.id === t);
389
409
  if (r)
390
410
  return f(r, e.fieldValues, e.sharedValues);
391
411
  };
392
- function be(e, t = {}) {
412
+ function Ve(e, t = {}) {
393
413
  const r = {};
394
- for (const s of e.fields) {
395
- const n = f(
396
- s,
414
+ for (const n of e.fields) {
415
+ const i = f(
416
+ n,
397
417
  e.fieldValues,
398
418
  e.sharedValues
399
419
  );
400
- (n !== void 0 || t.includeEmpty) && (r[s.id] = n);
420
+ (i !== void 0 || t.includeEmpty) && (r[n.id] = i);
401
421
  }
402
422
  return r;
403
423
  }
404
- const ee = (e) => ({
424
+ const te = (e, t, r, n, i, s) => {
425
+ const a = Math.max(0, s - 4), d = Math.min(r.widthOfTextAtSize(t, n), a);
426
+ switch (e.textAlign) {
427
+ case "center":
428
+ return i + (s - d) / 2;
429
+ case "right":
430
+ return i + s - 2 - d;
431
+ case "left":
432
+ default:
433
+ return i + 2;
434
+ }
435
+ }, re = (e) => ({
405
436
  width: e.getWidth(),
406
437
  height: e.getHeight()
407
- }), A = (e, t, r, s, n) => {
408
- const o = M(t, n.width, n.height), i = t.textSize ?? 10, a = n.height - o.y - o.height / 2 - i / 2;
438
+ }), I = (e, t, r, n, i) => {
439
+ const s = S(t, i.width, i.height), o = t.textSize ?? 10, a = i.height - s.y - s.height / 2 - o / 2, d = te(t, r, n, o, s.x, s.width);
409
440
  e.drawText(r, {
410
- x: o.x + 2,
441
+ x: d,
411
442
  y: Math.max(0, a),
412
- size: i,
413
- font: s,
414
- color: p(0, 0, 0),
415
- maxWidth: o.width - 4
443
+ size: o,
444
+ font: n,
445
+ color: w(0, 0, 0),
446
+ maxWidth: s.width - 4
416
447
  });
417
- }, te = (e, t, r, s) => {
448
+ }, ne = (e, t, r, n) => {
418
449
  if (!r) return;
419
- const n = M(t, s.width, s.height), o = n.x + n.width / 2, i = s.height - n.y - n.height / 2;
450
+ const i = S(t, n.width, n.height), s = Math.min(i.width, i.height), o = {
451
+ ...i,
452
+ x: i.x + (i.width - s) / 2,
453
+ y: i.y + (i.height - s) / 2
454
+ }, a = n.height - o.y - s, d = Math.max(1.2, s * 0.11);
420
455
  e.drawLine({
421
- start: { x: n.x + 2, y: i },
422
- end: { x: o, y: s.height - n.y - n.height + 2 },
423
- thickness: 1.5,
424
- color: p(0, 0, 0)
456
+ start: { x: o.x + s * 0.22, y: a + s * 0.49 },
457
+ end: { x: o.x + s * 0.42, y: a + s * 0.31 },
458
+ thickness: d,
459
+ color: w(0, 0, 0)
425
460
  }), e.drawLine({
426
- start: { x: o, y: s.height - n.y - n.height + 2 },
427
- end: { x: n.x + n.width - 2, y: s.height - n.y - 2 },
428
- thickness: 1.5,
429
- color: p(0, 0, 0)
461
+ start: { x: o.x + s * 0.42, y: a + s * 0.31 },
462
+ end: { x: o.x + s * 0.78, y: a + s * 0.68 },
463
+ thickness: d,
464
+ color: w(0, 0, 0)
430
465
  });
431
- }, re = async (e, t, r, s, n, o) => {
432
- const i = M(r, o.width, o.height), a = o.height - i.y - i.height, d = v(
433
- n !== void 0 ? {
434
- image: s,
435
- mimeType: n
466
+ }, se = async (e, t, r, n, i, s) => {
467
+ const o = S(r, s.width, s.height), a = s.height - o.y - o.height, d = v(
468
+ i !== void 0 ? {
469
+ image: n,
470
+ mimeType: i
436
471
  } : {
437
- image: s
472
+ image: n
438
473
  }
439
474
  );
440
475
  if (d === void 0)
441
476
  throw new Error(
442
477
  `Field "${r.id}" contains an unsupported signature image format for PDF export.`
443
478
  );
444
- const c = d === "image/jpeg" ? await t.embedJpg(s) : await t.embedPng(s);
445
- e.drawImage(c, {
446
- x: i.x,
479
+ const h = d === "image/jpeg" ? await t.embedJpg(n) : await t.embedPng(n);
480
+ e.drawImage(h, {
481
+ x: o.x,
447
482
  y: Math.max(0, a),
448
- width: i.width,
449
- height: i.height
483
+ width: o.width,
484
+ height: o.height
450
485
  });
451
- }, se = async (e, t, r, s, n, o) => {
452
- if (!(s === void 0 || r.hidden)) {
453
- if (g(s)) {
454
- await re(
486
+ }, ie = async (e, t, r, n, i, s) => {
487
+ if (!(n === void 0 || r.hidden)) {
488
+ if (g(n)) {
489
+ await se(
455
490
  e,
456
491
  t,
457
492
  r,
458
- s.image,
459
- s.mimeType,
460
- o
493
+ n.image,
494
+ n.mimeType,
495
+ s
461
496
  );
462
497
  return;
463
498
  }
464
- if (typeof s == "boolean") {
465
- r.type === "checkbox" && te(e, r, s, o);
499
+ if (typeof n == "boolean") {
500
+ r.type === "checkbox" && ne(e, r, n, s);
466
501
  return;
467
502
  }
468
- if (typeof s == "string" && s.trim() !== "") {
469
- A(
503
+ if (typeof n == "string" && n.trim() !== "") {
504
+ I(
470
505
  e,
471
506
  r,
472
- r.type === "date" ? X(s, r.dateFormat) : s,
473
- n,
474
- o
507
+ r.type === "date" ? X(n, r.dateFormat) : n,
508
+ i,
509
+ s
475
510
  );
476
511
  return;
477
512
  }
478
- typeof s == "number" && A(e, r, String(s), n, o);
513
+ typeof n == "number" && I(e, r, String(n), i, s);
479
514
  }
480
- }, ne = (e) => {
515
+ }, oe = (e) => {
481
516
  if (!(e.byteLength > 0))
482
517
  throw new Error(
483
518
  "exportToPdf requires a non-empty source PDF in document.pdfData."
484
519
  );
485
- }, Ve = async (e, t = {}) => {
486
- var i;
487
- ne(e.pdfData);
488
- const r = await I.load(e.pdfData), s = await r.embedFont(N.Helvetica), n = r.getPages(), o = /* @__PURE__ */ new Map();
520
+ }, Ie = async (e, t = {}) => {
521
+ var o;
522
+ oe(e.pdfData);
523
+ const r = await T.load(e.pdfData), n = await r.embedFont(N.Helvetica), i = r.getPages(), s = /* @__PURE__ */ new Map();
489
524
  for (const a of e.fields) {
490
- const d = o.get(a.page) ?? [];
491
- o.set(a.page, [...d, a]);
525
+ const d = s.get(a.page) ?? [];
526
+ s.set(a.page, [...d, a]);
492
527
  }
493
- for (const [a, d] of o) {
494
- const h = n[a];
495
- if (!h) continue;
496
- const c = ee(h);
497
- for (const y of d) {
498
- const E = f(
499
- y,
528
+ for (const [a, d] of s) {
529
+ const c = i[a];
530
+ if (!c) continue;
531
+ const h = re(c);
532
+ for (const p of d) {
533
+ const F = f(
534
+ p,
500
535
  e.fieldValues,
501
536
  e.sharedValues
502
- ), F = x(y, E);
503
- if (!F.valid)
537
+ ), b = E(p, F);
538
+ if (!b.valid)
504
539
  throw new Error(
505
- `Field "${y.id}" is invalid for PDF export: ${((i = F.errors[0]) == null ? void 0 : i.message) ?? "Unknown validation error."}`
540
+ `Field "${p.id}" is invalid for PDF export: ${((o = b.errors[0]) == null ? void 0 : o.message) ?? "Unknown validation error."}`
506
541
  );
507
- await se(h, r, y, E, s, c);
542
+ await ie(c, r, p, F, n, h);
508
543
  }
509
544
  }
510
545
  return r.save();
511
546
  };
512
547
  export {
513
- b as clampCoord,
514
- L as clampToPage,
515
- Me as clearFieldValue,
516
- Q as createDocument,
517
- ge as createDocumentFromPdfData,
548
+ A as clampCoord,
549
+ k as clampToPage,
550
+ Ee as clearFieldValue,
551
+ Z as createDocument,
552
+ me as createDocumentFromPdfData,
518
553
  ye as createField,
519
- q as dateFormatToRegexPattern,
554
+ W as dateFormatToRegexPattern,
520
555
  m as ensureMinSize,
521
- Ve as exportToPdf,
556
+ Ie as exportToPdf,
522
557
  X as formatDateValue,
523
- xe as getField,
524
- Ee as getFields,
525
- ce as getMirrorFields,
526
- Z as getPdfPageInfo,
527
- Fe as getResolvedFieldValue,
528
- be as getResolvedValues,
529
- de as getSourceField,
530
- fe as isIsoDateString,
531
- ie as isPrimitiveValue,
558
+ Fe as getField,
559
+ be as getFields,
560
+ le as getMirrorFields,
561
+ ee as getPdfPageInfo,
562
+ Ae as getResolvedFieldValue,
563
+ Ve as getResolvedValues,
564
+ ce as getSourceField,
565
+ pe as isIsoDateString,
566
+ de as isPrimitiveValue,
532
567
  g as isSignatureValue,
533
- ue as matchesDateFormat,
534
- pe as moveField,
535
- T as normalizeRect,
536
- me as removeField,
537
- we as resizeField,
538
- he as resolveAllSharedValues,
568
+ ge as matchesDateFormat,
569
+ xe as moveField,
570
+ y as normalizeRect,
571
+ we as removeField,
572
+ Me as resizeField,
573
+ ue as resolveAllSharedValues,
539
574
  f as resolveFieldValue,
540
575
  Se as setFieldValue,
541
- k as setSharedFieldValue,
542
- M as toAbsoluteRect,
543
- ae as toNormalizedRect,
576
+ j as setSharedFieldValue,
577
+ S as toAbsoluteRect,
578
+ he as toNormalizedRect,
544
579
  P as updateField,
545
- le as validateDocument,
546
- x as validateField,
547
- U as validateSharedFieldGroup
580
+ fe as validateDocument,
581
+ E as validateField,
582
+ G as validateSharedFieldGroup
548
583
  };
549
584
  //# sourceMappingURL=index.js.map