@delopay/sdk 0.74.0 → 0.76.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.
@@ -3911,7 +3911,11 @@ function cloneCustomField(f) {
3911
3911
  labelTranslations: { ...f.labelTranslations },
3912
3912
  placeholderTranslations: { ...f.placeholderTranslations },
3913
3913
  helpTextTranslations: { ...f.helpTextTranslations },
3914
- options: f.options.map((o) => ({ ...o, labelTranslations: { ...o.labelTranslations } }))
3914
+ options: f.options.map((o) => ({ ...o, labelTranslations: { ...o.labelTranslations } })),
3915
+ visibility: {
3916
+ ...f.visibility,
3917
+ conditions: f.visibility.conditions.map((c) => ({ ...c }))
3918
+ }
3915
3919
  };
3916
3920
  }
3917
3921
  var CUSTOM_CSS_MAX_LENGTH = 5e4;
@@ -3997,8 +4001,57 @@ var ALL_CUSTOM_FIELD_TYPES = [
3997
4001
  "textarea",
3998
4002
  "password",
3999
4003
  "email",
4000
- "select"
4004
+ "select",
4005
+ "checkbox"
4006
+ ];
4007
+ var CHECKBOX_CHECKED = "true";
4008
+ var CHECKBOX_UNCHECKED = "false";
4009
+ function isCheckboxChecked(value) {
4010
+ return typeof value === "string" && value.trim().toLowerCase() === CHECKBOX_CHECKED;
4011
+ }
4012
+ function customFieldIsTextLike(type) {
4013
+ return type !== "select" && type !== "checkbox";
4014
+ }
4015
+ var CUSTOM_FIELD_CONDITIONS_MAX = 10;
4016
+ var ALL_CUSTOM_FIELD_CONDITION_SOURCES = [
4017
+ "metadata",
4018
+ "currency",
4019
+ "amount"
4020
+ ];
4021
+ var ALL_CUSTOM_FIELD_OPERATORS = [
4022
+ "equals",
4023
+ "not_equals",
4024
+ "contains",
4025
+ "not_contains",
4026
+ "starts_with",
4027
+ "ends_with",
4028
+ "in",
4029
+ "not_in",
4030
+ "exists",
4031
+ "not_exists",
4032
+ "gt",
4033
+ "gte",
4034
+ "lt",
4035
+ "lte"
4001
4036
  ];
4037
+ var CUSTOM_FIELD_OPERATORS_BY_SOURCE = {
4038
+ metadata: ALL_CUSTOM_FIELD_OPERATORS,
4039
+ currency: ["equals", "not_equals", "in", "not_in"],
4040
+ amount: ["equals", "not_equals", "gt", "gte", "lt", "lte"]
4041
+ };
4042
+ var CUSTOM_FIELD_VALUELESS_OPERATORS = [
4043
+ "exists",
4044
+ "not_exists"
4045
+ ];
4046
+ function customFieldOperatorTakesValue(operator) {
4047
+ return !CUSTOM_FIELD_VALUELESS_OPERATORS.includes(operator);
4048
+ }
4049
+ function defaultOperatorForSource(source) {
4050
+ return CUSTOM_FIELD_OPERATORS_BY_SOURCE[source][0] ?? "equals";
4051
+ }
4052
+ function defaultCustomFieldVisibility() {
4053
+ return { mode: "always", match: "all", conditions: [] };
4054
+ }
4002
4055
  function parseTranslations(raw) {
4003
4056
  if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
4004
4057
  const out = {};
@@ -4007,6 +4060,41 @@ function parseTranslations(raw) {
4007
4060
  }
4008
4061
  return out;
4009
4062
  }
4063
+ function normalizeCondition(raw, index) {
4064
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
4065
+ const c = raw;
4066
+ const source = pickEnum(
4067
+ c["source"],
4068
+ ALL_CUSTOM_FIELD_CONDITION_SOURCES,
4069
+ "metadata"
4070
+ );
4071
+ const allowed = CUSTOM_FIELD_OPERATORS_BY_SOURCE[source];
4072
+ const operator = pickEnum(
4073
+ c["operator"],
4074
+ allowed,
4075
+ defaultOperatorForSource(source)
4076
+ );
4077
+ const rawValue = c["value"];
4078
+ return {
4079
+ id: typeof c["id"] === "string" && c["id"] ? c["id"] : `cond-${index}`,
4080
+ source,
4081
+ // Only metadata conditions carry a key; drop anything else so the
4082
+ // encoded form stays canonical.
4083
+ key: source === "metadata" && typeof c["key"] === "string" ? c["key"].trim() : "",
4084
+ operator,
4085
+ value: typeof rawValue === "string" ? rawValue : typeof rawValue === "number" || typeof rawValue === "boolean" ? String(rawValue) : ""
4086
+ };
4087
+ }
4088
+ function normalizeVisibility(raw) {
4089
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return defaultCustomFieldVisibility();
4090
+ const v = raw;
4091
+ const conditions = Array.isArray(v["conditions"]) ? v["conditions"].slice(0, CUSTOM_FIELD_CONDITIONS_MAX).map(normalizeCondition).filter((c) => c !== null) : [];
4092
+ return {
4093
+ mode: pickEnum(v["mode"], ["always", "match"], "always"),
4094
+ match: pickEnum(v["match"], ["all", "any"], "all"),
4095
+ conditions
4096
+ };
4097
+ }
4010
4098
  function parseBoundedInt(raw) {
4011
4099
  const n = typeof raw === "number" ? raw : typeof raw === "string" ? Number(raw) : NaN;
4012
4100
  if (!Number.isInteger(n) || n < 0) return null;
@@ -4026,8 +4114,11 @@ function normalizeCustomField(raw, index) {
4026
4114
  labelTranslations: parseTranslations(o["labelTranslations"])
4027
4115
  };
4028
4116
  }).filter((o) => o.value.length > 0) : [];
4029
- const minLength = parseBoundedInt(f["minLength"]);
4030
- const maxLength = parseBoundedInt(f["maxLength"]);
4117
+ const textLike = customFieldIsTextLike(type);
4118
+ const minLength = textLike ? parseBoundedInt(f["minLength"]) : null;
4119
+ const maxLength = textLike ? parseBoundedInt(f["maxLength"]) : null;
4120
+ const rawDefault = typeof f["defaultValue"] === "string" ? f["defaultValue"] : "";
4121
+ const defaultValue = type === "checkbox" ? isCheckboxChecked(rawDefault) ? CHECKBOX_CHECKED : CHECKBOX_UNCHECKED : rawDefault;
4031
4122
  return {
4032
4123
  id: typeof f["id"] === "string" && f["id"] ? f["id"] : `field-${index}`,
4033
4124
  key,
@@ -4043,8 +4134,9 @@ function normalizeCustomField(raw, index) {
4043
4134
  minLength,
4044
4135
  // Guard inverted bounds at decode so consumers never see min > max.
4045
4136
  maxLength: maxLength !== null && minLength !== null && maxLength < minLength ? null : maxLength,
4046
- defaultValue: typeof f["defaultValue"] === "string" ? f["defaultValue"] : "",
4047
- options
4137
+ defaultValue,
4138
+ options,
4139
+ visibility: normalizeVisibility(f["visibility"])
4048
4140
  };
4049
4141
  }
4050
4142
  function parseCustomFieldsLoose(raw) {
@@ -4085,13 +4177,125 @@ function encodeCustomFields(fields) {
4085
4177
  ...nonEmpty(f.helpTextTranslations) ? { helpTextTranslations: nonEmpty(f.helpTextTranslations) } : {},
4086
4178
  ...f.required ? { required: true } : {},
4087
4179
  ...f.enabled ? {} : { enabled: false },
4088
- ...f.minLength !== null ? { minLength: f.minLength } : {},
4089
- ...f.maxLength !== null ? { maxLength: f.maxLength } : {},
4090
- ...f.defaultValue ? { defaultValue: f.defaultValue } : {},
4091
- ...f.type === "select" ? { options: f.options } : {}
4180
+ // Length bounds only exist for free-text types; a choice control that
4181
+ // still carries them is stale state the decoder would drop anyway.
4182
+ ...customFieldIsTextLike(f.type) && f.minLength !== null ? { minLength: f.minLength } : {},
4183
+ ...customFieldIsTextLike(f.type) && f.maxLength !== null ? { maxLength: f.maxLength } : {},
4184
+ // A checkbox persists only "starts ticked"; unticked is the decoder's
4185
+ // default, so writing 'false' would be noise on every such field.
4186
+ ...f.type === "checkbox" ? isCheckboxChecked(f.defaultValue) ? { defaultValue: CHECKBOX_CHECKED } : {} : f.defaultValue ? { defaultValue: f.defaultValue } : {},
4187
+ ...f.type === "select" ? { options: f.options } : {},
4188
+ // Omitted for unconditional fields so the stored blob (and every
4189
+ // pre-feature payload) stays byte-identical to what it was.
4190
+ ...f.visibility.mode === "match" ? { visibility: encodeVisibility(f.visibility) } : {}
4092
4191
  }))
4093
4192
  );
4094
4193
  }
4194
+ function encodeVisibility(v) {
4195
+ return {
4196
+ mode: v.mode,
4197
+ match: v.match,
4198
+ conditions: v.conditions.map((c) => ({
4199
+ id: c.id,
4200
+ source: c.source,
4201
+ ...c.source === "metadata" && c.key ? { key: c.key } : {},
4202
+ operator: c.operator,
4203
+ ...customFieldOperatorTakesValue(c.operator) && c.value ? { value: c.value } : {}
4204
+ }))
4205
+ };
4206
+ }
4207
+ function customFieldContextFromMetadata(raw) {
4208
+ const out = {};
4209
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return out;
4210
+ for (const [key, value] of Object.entries(raw)) {
4211
+ const flat = flattenMetadataValue(value);
4212
+ if (flat !== null) out[key] = flat;
4213
+ }
4214
+ return out;
4215
+ }
4216
+ function flattenMetadataValue(value) {
4217
+ if (typeof value === "string") return value;
4218
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
4219
+ if (value === null || value === void 0) return null;
4220
+ if (Array.isArray(value)) {
4221
+ return value.map((v) => flattenMetadataValue(v)).filter((v) => v !== null).join(",");
4222
+ }
4223
+ try {
4224
+ return JSON.stringify(value);
4225
+ } catch {
4226
+ return null;
4227
+ }
4228
+ }
4229
+ function norm(value) {
4230
+ return value.trim().toLowerCase();
4231
+ }
4232
+ var DECIMAL_NUMBER_PATTERN = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/;
4233
+ function numeric(value) {
4234
+ const trimmed = value.trim();
4235
+ if (!DECIMAL_NUMBER_PATTERN.test(trimmed)) return null;
4236
+ const n = Number(trimmed);
4237
+ return Number.isFinite(n) ? n : null;
4238
+ }
4239
+ function splitList(value) {
4240
+ return value.split(",").map((part) => norm(part)).filter((part) => part.length > 0);
4241
+ }
4242
+ function operandFor(condition, ctx) {
4243
+ switch (condition.source) {
4244
+ case "currency":
4245
+ return ctx.currency;
4246
+ case "amount":
4247
+ return String(ctx.amount);
4248
+ case "metadata":
4249
+ return Object.prototype.hasOwnProperty.call(ctx.metadata, condition.key) ? ctx.metadata[condition.key] : void 0;
4250
+ }
4251
+ }
4252
+ function evaluateCustomFieldCondition(condition, ctx) {
4253
+ const raw = operandFor(condition, ctx);
4254
+ const actual = raw ?? "";
4255
+ const expected = condition.value;
4256
+ switch (condition.operator) {
4257
+ case "exists":
4258
+ return raw !== void 0 && raw.trim().length > 0;
4259
+ case "not_exists":
4260
+ return raw === void 0 || raw.trim().length === 0;
4261
+ case "equals":
4262
+ return norm(actual) === norm(expected);
4263
+ case "not_equals":
4264
+ return norm(actual) !== norm(expected);
4265
+ case "contains":
4266
+ return norm(actual).includes(norm(expected));
4267
+ case "not_contains":
4268
+ return !norm(actual).includes(norm(expected));
4269
+ case "starts_with":
4270
+ return norm(actual).startsWith(norm(expected));
4271
+ case "ends_with":
4272
+ return norm(actual).endsWith(norm(expected));
4273
+ case "in":
4274
+ return splitList(expected).includes(norm(actual));
4275
+ case "not_in":
4276
+ return !splitList(expected).includes(norm(actual));
4277
+ case "gt":
4278
+ case "gte":
4279
+ case "lt":
4280
+ case "lte": {
4281
+ const a = numeric(actual);
4282
+ const b = numeric(expected);
4283
+ if (a === null || b === null) return false;
4284
+ if (condition.operator === "gt") return a > b;
4285
+ if (condition.operator === "gte") return a >= b;
4286
+ if (condition.operator === "lt") return a < b;
4287
+ return a <= b;
4288
+ }
4289
+ }
4290
+ }
4291
+ function evaluateCustomFieldVisibility(field, ctx) {
4292
+ const { mode, match, conditions } = field.visibility;
4293
+ if (mode !== "match" || conditions.length === 0) return true;
4294
+ return match === "any" ? conditions.some((c) => evaluateCustomFieldCondition(c, ctx)) : conditions.every((c) => evaluateCustomFieldCondition(c, ctx));
4295
+ }
4296
+ function visibleCustomFields(fields, ctx) {
4297
+ return fields.filter((f) => f.enabled && evaluateCustomFieldVisibility(f, ctx));
4298
+ }
4095
4299
  function translationFor(map, locale) {
4096
4300
  if (!locale) return null;
4097
4301
  const own = (k) => {
@@ -4515,9 +4719,25 @@ export {
4515
4719
  CUSTOM_FIELDS_MAX,
4516
4720
  CUSTOM_FIELD_KEY_PATTERN,
4517
4721
  ALL_CUSTOM_FIELD_TYPES,
4722
+ CHECKBOX_CHECKED,
4723
+ CHECKBOX_UNCHECKED,
4724
+ isCheckboxChecked,
4725
+ customFieldIsTextLike,
4726
+ CUSTOM_FIELD_CONDITIONS_MAX,
4727
+ ALL_CUSTOM_FIELD_CONDITION_SOURCES,
4728
+ ALL_CUSTOM_FIELD_OPERATORS,
4729
+ CUSTOM_FIELD_OPERATORS_BY_SOURCE,
4730
+ CUSTOM_FIELD_VALUELESS_OPERATORS,
4731
+ customFieldOperatorTakesValue,
4732
+ defaultOperatorForSource,
4733
+ defaultCustomFieldVisibility,
4518
4734
  parseCustomFieldsLoose,
4519
4735
  decodeCustomFields,
4520
4736
  encodeCustomFields,
4737
+ customFieldContextFromMetadata,
4738
+ evaluateCustomFieldCondition,
4739
+ evaluateCustomFieldVisibility,
4740
+ visibleCustomFields,
4521
4741
  customFieldText,
4522
4742
  customFieldOptionLabel,
4523
4743
  decodeBranding,
@@ -4529,4 +4749,4 @@ export {
4529
4749
  applyBrandingVariables,
4530
4750
  shadowFor
4531
4751
  };
4532
- //# sourceMappingURL=chunk-5FA2IFIU.js.map
4752
+ //# sourceMappingURL=chunk-D7DPAKI2.js.map