@timeax/digital-service-engine 0.0.2 → 0.0.4

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.
@@ -520,13 +520,10 @@ function visibleFieldIdsUnder(props, tagId, opts = {}) {
520
520
  if (order && order.length) {
521
521
  const ordered = order.filter((fid) => visible.has(fid));
522
522
  const orderedSet = new Set(ordered);
523
- const rest2 = base.filter((fid) => !orderedSet.has(fid));
524
- return [...ordered, ...rest2];
523
+ const rest = base.filter((fid) => !orderedSet.has(fid));
524
+ return [...ordered, ...rest];
525
525
  }
526
- const promoted = revealedOrder.filter((fid) => visible.has(fid));
527
- const promotedSet = new Set(promoted);
528
- const rest = base.filter((fid) => !promotedSet.has(fid));
529
- return [...promoted, ...rest];
526
+ return base;
530
527
  }
531
528
  function visibleFieldsUnder(props, tagId, opts = {}) {
532
529
  var _a;
@@ -2559,6 +2556,10 @@ function getEligibleFallbacks(params) {
2559
2556
  }
2560
2557
  return eligible;
2561
2558
  }
2559
+ function getFallbackRegistrationInfo(props, nodeId) {
2560
+ const { primary, tagContexts } = primaryForNode(props, nodeId);
2561
+ return { primary, tagContexts };
2562
+ }
2562
2563
 
2563
2564
  // src/core/rate-coherence.ts
2564
2565
  function validateRateCoherenceDeep(params) {
@@ -3228,7 +3229,7 @@ function rateOk(svcMap, candidate, primary, policy) {
3228
3229
 
3229
3230
  // src/utils/build-order-snapshot.ts
3230
3231
  function buildOrderSnapshot(props, builder, selection, services, settings = {}) {
3231
- var _a, _b, _c, _d, _e, _f, _g;
3232
+ var _a, _b, _c, _d, _e, _f, _g, _h;
3232
3233
  const mode = (_a = settings.mode) != null ? _a : "prod";
3233
3234
  const hostDefaultQty = Number.isFinite(
3234
3235
  (_b = settings.hostDefaultQuantity) != null ? _b : 1
@@ -3242,20 +3243,18 @@ function buildOrderSnapshot(props, builder, selection, services, settings = {})
3242
3243
  };
3243
3244
  const builtAt = (/* @__PURE__ */ new Date()).toISOString();
3244
3245
  const tagId = selection.activeTagId;
3245
- const selectedOptionKeys = toSelectedOptionKeys(
3246
- selection.optionSelectionsByFieldId
3247
- );
3246
+ const selectedButtonKeys = (_d = selection.selectedKeys) != null ? _d : toSelectedOptionKeys(selection.optionSelectionsByFieldId);
3248
3247
  const visibleFieldIds = builder.visibleFields(
3249
3248
  tagId,
3250
- selectedOptionKeys
3249
+ selectedButtonKeys
3251
3250
  );
3252
3251
  const tagById = new Map(
3253
- ((_d = props.filters) != null ? _d : []).map((t) => [t.id, t])
3252
+ ((_e = props.filters) != null ? _e : []).map((t) => [t.id, t])
3254
3253
  );
3255
3254
  const fieldById = new Map(
3256
- ((_e = props.fields) != null ? _e : []).map((f) => [f.id, f])
3255
+ ((_f = props.fields) != null ? _f : []).map((f) => [f.id, f])
3257
3256
  );
3258
- const tagConstraints = (_g = (_f = tagById.get(tagId)) == null ? void 0 : _f.constraints) != null ? _g : void 0;
3257
+ const tagConstraints = (_h = (_g = tagById.get(tagId)) == null ? void 0 : _g.constraints) != null ? _h : void 0;
3259
3258
  const selectionFields = visibleFieldIds.map((fid) => fieldById.get(fid)).filter((f) => !!f).map((f) => {
3260
3259
  var _a2;
3261
3260
  const optIds = isOptionBased(f) ? (_a2 = selection.optionSelectionsByFieldId[f.id]) != null ? _a2 : [] : void 0;
@@ -3332,6 +3331,7 @@ function buildOrderSnapshot(props, builder, selection, services, settings = {})
3332
3331
  builtAt,
3333
3332
  selection: {
3334
3333
  tag: tagId,
3334
+ buttons: selectedButtonKeys,
3335
3335
  fields: selectionFields
3336
3336
  },
3337
3337
  inputs: {
@@ -3364,6 +3364,10 @@ function toSelectedOptionKeys(byField) {
3364
3364
  }
3365
3365
  return keys;
3366
3366
  }
3367
+ function isServicedBased(field) {
3368
+ if (field.service_id) return true;
3369
+ return !!(field.options && field.options.some((item) => item.service_id));
3370
+ }
3367
3371
  function buildInputs(visibleFieldIds, fieldById, selection) {
3368
3372
  const formValues = {};
3369
3373
  const selections = {};
@@ -3374,7 +3378,7 @@ function buildInputs(visibleFieldIds, fieldById, selection) {
3374
3378
  if (selOptIds && selOptIds.length) {
3375
3379
  selections[fid] = [...selOptIds];
3376
3380
  }
3377
- if (!isOptionBased(f)) {
3381
+ if (!isServicedBased(f)) {
3378
3382
  const name = f.name;
3379
3383
  const val = selection.formValuesByFieldId[fid];
3380
3384
  if (!name || val === void 0) continue;
@@ -3786,12 +3790,355 @@ function resolveMinMax(servicesList, services) {
3786
3790
  }
3787
3791
  return { min: min != null ? min : 1, ...max !== void 0 ? { max } : {} };
3788
3792
  }
3793
+
3794
+ // src/core/fallback-editor.ts
3795
+ function createFallbackEditor(options = {}) {
3796
+ var _a, _b;
3797
+ const original = cloneFallbacks(options.fallbacks);
3798
+ let current = cloneFallbacks(options.fallbacks);
3799
+ const props = options.props;
3800
+ const services = (_a = options.services) != null ? _a : {};
3801
+ const settings = (_b = options.settings) != null ? _b : {};
3802
+ function state() {
3803
+ return {
3804
+ original: cloneFallbacks(original),
3805
+ current: cloneFallbacks(current),
3806
+ changed: !sameFallbacks(original, current)
3807
+ };
3808
+ }
3809
+ function value() {
3810
+ return cloneFallbacks(current);
3811
+ }
3812
+ function reset() {
3813
+ current = cloneFallbacks(original);
3814
+ return state();
3815
+ }
3816
+ function get(serviceId) {
3817
+ var _a2, _b2;
3818
+ const out = [];
3819
+ for (const [primary, list] of Object.entries((_a2 = current.global) != null ? _a2 : {})) {
3820
+ if (String(primary) !== String(serviceId)) continue;
3821
+ out.push({
3822
+ scope: "global",
3823
+ primary,
3824
+ services: [...list != null ? list : []]
3825
+ });
3826
+ }
3827
+ if (!props) return out;
3828
+ for (const [nodeId, list] of Object.entries((_b2 = current.nodes) != null ? _b2 : {})) {
3829
+ const info = getFallbackRegistrationInfo(props, nodeId);
3830
+ if (String(info.primary) !== String(serviceId)) continue;
3831
+ out.push({
3832
+ scope: "node",
3833
+ scopeId: nodeId,
3834
+ primary: info.primary,
3835
+ services: [...list != null ? list : []]
3836
+ });
3837
+ }
3838
+ return out;
3839
+ }
3840
+ function getScope(context) {
3841
+ var _a2, _b2, _c, _d;
3842
+ if (context.scope === "global") {
3843
+ return [...(_b2 = (_a2 = current.global) == null ? void 0 : _a2[context.primary]) != null ? _b2 : []];
3844
+ }
3845
+ return [...(_d = (_c = current.nodes) == null ? void 0 : _c[context.nodeId]) != null ? _d : []];
3846
+ }
3847
+ function check(context, candidates) {
3848
+ var _a2, _b2;
3849
+ const normalized = normalizeCandidateList(
3850
+ candidates != null ? candidates : getScope(context),
3851
+ true
3852
+ );
3853
+ if (context.scope === "node" && !props) {
3854
+ return {
3855
+ context,
3856
+ allowed: [],
3857
+ rejected: normalized.map((candidate) => ({
3858
+ candidate,
3859
+ ok: false,
3860
+ reasons: ["missing_service_props"]
3861
+ })),
3862
+ warnings: ["missing_service_props"]
3863
+ };
3864
+ }
3865
+ const tempFallbacks = cloneFallbacks(current);
3866
+ if (context.scope === "global") {
3867
+ (_a2 = tempFallbacks.global) != null ? _a2 : tempFallbacks.global = {};
3868
+ if (normalized.length)
3869
+ tempFallbacks.global[context.primary] = normalized;
3870
+ else delete tempFallbacks.global[context.primary];
3871
+ } else {
3872
+ (_b2 = tempFallbacks.nodes) != null ? _b2 : tempFallbacks.nodes = {};
3873
+ if (normalized.length)
3874
+ tempFallbacks.nodes[context.nodeId] = normalized;
3875
+ else delete tempFallbacks.nodes[context.nodeId];
3876
+ }
3877
+ if (!props) {
3878
+ if (context.scope !== "global") {
3879
+ return {
3880
+ context,
3881
+ allowed: [],
3882
+ rejected: normalized.map((candidate) => ({
3883
+ candidate,
3884
+ ok: false,
3885
+ reasons: ["missing_service_props"]
3886
+ })),
3887
+ warnings: ["missing_service_props"]
3888
+ };
3889
+ }
3890
+ const rejected2 = [];
3891
+ const allowed2 = [];
3892
+ for (const candidate of normalized) {
3893
+ const reasons = [];
3894
+ if (String(candidate) === String(context.primary)) {
3895
+ reasons.push("self_reference");
3896
+ }
3897
+ if (reasons.length) {
3898
+ rejected2.push({ candidate, ok: false, reasons });
3899
+ } else {
3900
+ allowed2.push(candidate);
3901
+ }
3902
+ }
3903
+ return {
3904
+ context,
3905
+ primary: context.primary,
3906
+ allowed: allowed2,
3907
+ rejected: rejected2,
3908
+ warnings: []
3909
+ };
3910
+ }
3911
+ const fakeProps = {
3912
+ ...props,
3913
+ fallbacks: tempFallbacks
3914
+ };
3915
+ const diags = collectFailedFallbacks(fakeProps, services, {
3916
+ ...settings,
3917
+ mode: "dev"
3918
+ });
3919
+ const scoped = diags.filter((d) => {
3920
+ if (context.scope === "global") {
3921
+ return d.scope === "global" && String(d.primary) === String(context.primary);
3922
+ }
3923
+ return d.scope === "node" && String(d.nodeId) === String(context.nodeId);
3924
+ });
3925
+ const rejected = normalized.map((candidate) => {
3926
+ const reasons = scoped.filter((d) => String(d.candidate) === String(candidate)).map((d) => mapDiagReason(d.reason));
3927
+ return {
3928
+ candidate,
3929
+ ok: reasons.length === 0,
3930
+ reasons
3931
+ };
3932
+ }).filter((row) => !row.ok);
3933
+ const allowed = normalized.filter(
3934
+ (candidate) => !rejected.some(
3935
+ (r) => String(r.candidate) === String(candidate)
3936
+ )
3937
+ );
3938
+ const info = context.scope === "global" ? { ok: true, primary: context.primary } : getNodeRegistrationInfo(props, context.nodeId);
3939
+ const primary = (info == null ? void 0 : info.ok) ? info.primary : void 0;
3940
+ return {
3941
+ context,
3942
+ primary,
3943
+ allowed,
3944
+ rejected,
3945
+ warnings: []
3946
+ };
3947
+ }
3948
+ function add(context, candidate, options2) {
3949
+ return addMany(context, [candidate], options2);
3950
+ }
3951
+ function addMany(context, candidates, options2) {
3952
+ const existing = getScope(context);
3953
+ const merged = [...existing];
3954
+ const insertAt = typeof (options2 == null ? void 0 : options2.index) === "number" ? clamp(options2.index, 0, merged.length) : void 0;
3955
+ const incoming = normalizeCandidateList(candidates, true).filter(
3956
+ (id) => !merged.some((x) => String(x) === String(id))
3957
+ );
3958
+ if (insertAt === void 0) {
3959
+ merged.push(...incoming);
3960
+ } else {
3961
+ merged.splice(insertAt, 0, ...incoming);
3962
+ }
3963
+ return replace(context, merged, options2);
3964
+ }
3965
+ function remove(context, candidate) {
3966
+ const next = getScope(context).filter(
3967
+ (id) => String(id) !== String(candidate)
3968
+ );
3969
+ return writeScope(context, next);
3970
+ }
3971
+ function replace(context, candidates, options2) {
3972
+ const strict = !!(options2 == null ? void 0 : options2.strict);
3973
+ const normalized = normalizeCandidateList(candidates, true);
3974
+ const checked = check(context, normalized);
3975
+ const next = strict ? checked.allowed : normalized;
3976
+ return writeScope(context, next);
3977
+ }
3978
+ function clear(context) {
3979
+ return writeScope(context, []);
3980
+ }
3981
+ function eligible(context, opt) {
3982
+ if (!props) return [];
3983
+ if (context.scope === "global") {
3984
+ return getEligibleFallbacks({
3985
+ primary: context.primary,
3986
+ services,
3987
+ fallbacks: current,
3988
+ settings,
3989
+ props,
3990
+ exclude: opt == null ? void 0 : opt.exclude,
3991
+ unique: opt == null ? void 0 : opt.unique,
3992
+ limit: opt == null ? void 0 : opt.limit
3993
+ });
3994
+ }
3995
+ const info = getFallbackRegistrationInfo(props, context.nodeId);
3996
+ if (!info.primary) return [];
3997
+ return getEligibleFallbacks({
3998
+ primary: info.primary,
3999
+ nodeId: context.nodeId,
4000
+ tagId: info.tagContexts[0],
4001
+ services,
4002
+ fallbacks: current,
4003
+ settings,
4004
+ props,
4005
+ exclude: opt == null ? void 0 : opt.exclude,
4006
+ unique: opt == null ? void 0 : opt.unique,
4007
+ limit: opt == null ? void 0 : opt.limit
4008
+ });
4009
+ }
4010
+ function writeScope(context, nextList) {
4011
+ var _a2, _b2;
4012
+ const next = cloneFallbacks(current);
4013
+ if (context.scope === "global") {
4014
+ (_a2 = next.global) != null ? _a2 : next.global = {};
4015
+ if (nextList.length) {
4016
+ next.global[context.primary] = [...nextList];
4017
+ } else {
4018
+ delete next.global[context.primary];
4019
+ if (!Object.keys(next.global).length) delete next.global;
4020
+ }
4021
+ } else {
4022
+ (_b2 = next.nodes) != null ? _b2 : next.nodes = {};
4023
+ if (nextList.length) {
4024
+ next.nodes[context.nodeId] = [...nextList];
4025
+ } else {
4026
+ delete next.nodes[context.nodeId];
4027
+ if (!Object.keys(next.nodes).length) delete next.nodes;
4028
+ }
4029
+ }
4030
+ current = next;
4031
+ return state();
4032
+ }
4033
+ return {
4034
+ state,
4035
+ value,
4036
+ reset,
4037
+ get,
4038
+ getScope,
4039
+ check,
4040
+ add,
4041
+ addMany,
4042
+ remove,
4043
+ replace,
4044
+ clear,
4045
+ eligible
4046
+ };
4047
+ }
4048
+ function cloneFallbacks(input) {
4049
+ return {
4050
+ ...(input == null ? void 0 : input.nodes) ? { nodes: cloneRecordArray(input.nodes) } : {},
4051
+ ...(input == null ? void 0 : input.global) ? { global: cloneRecordArray(input.global) } : {}
4052
+ };
4053
+ }
4054
+ function cloneRecordArray(input) {
4055
+ const out = {};
4056
+ for (const [k, v] of Object.entries(input)) out[k] = [...v != null ? v : []];
4057
+ return out;
4058
+ }
4059
+ function sameFallbacks(a, b) {
4060
+ return JSON.stringify(a != null ? a : {}) === JSON.stringify(b != null ? b : {});
4061
+ }
4062
+ function normalizeCandidateList(input, preserveOrder) {
4063
+ const out = [];
4064
+ for (const item of input != null ? input : []) {
4065
+ if (!isValidServiceIdRef(item)) continue;
4066
+ const exists = out.some((x) => String(x) === String(item));
4067
+ if (exists) continue;
4068
+ out.push(item);
4069
+ }
4070
+ return preserveOrder ? out : out;
4071
+ }
4072
+ function isValidServiceIdRef(value) {
4073
+ return typeof value === "number" && Number.isFinite(value) || typeof value === "string" && value.trim().length > 0;
4074
+ }
4075
+ function clamp(n, min, max) {
4076
+ return Math.max(min, Math.min(max, n));
4077
+ }
4078
+ function getNodeRegistrationInfo(props, nodeId) {
4079
+ const tag = props.filters.find((t) => t.id === nodeId);
4080
+ if (tag) {
4081
+ if (!isValidServiceIdRef(tag.service_id)) {
4082
+ return { ok: false, reasons: ["no_primary"] };
4083
+ }
4084
+ return {
4085
+ ok: true,
4086
+ primary: tag.service_id,
4087
+ tagContexts: [tag.id]
4088
+ };
4089
+ }
4090
+ const hit = findOptionOwner(props.fields, nodeId);
4091
+ if (!hit) {
4092
+ return { ok: false, reasons: ["node_not_found"] };
4093
+ }
4094
+ if (!isValidServiceIdRef(hit.option.service_id)) {
4095
+ return { ok: false, reasons: ["no_primary"] };
4096
+ }
4097
+ return {
4098
+ ok: true,
4099
+ primary: hit.option.service_id,
4100
+ tagContexts: bindIdsToArray2(hit.field.bind_id)
4101
+ };
4102
+ }
4103
+ function findOptionOwner(fields, optionId) {
4104
+ var _a;
4105
+ for (const field of fields) {
4106
+ for (const option of (_a = field.options) != null ? _a : []) {
4107
+ if (option.id === optionId) return { field, option };
4108
+ }
4109
+ }
4110
+ return null;
4111
+ }
4112
+ function bindIdsToArray2(v) {
4113
+ if (Array.isArray(v)) return v.filter(Boolean);
4114
+ return v ? [v] : [];
4115
+ }
4116
+ function mapDiagReason(reason) {
4117
+ switch (String(reason)) {
4118
+ case "unknown_service":
4119
+ return "unknown_service";
4120
+ case "no_primary":
4121
+ return "no_primary";
4122
+ case "rate_violation":
4123
+ return "rate_violation";
4124
+ case "constraint_mismatch":
4125
+ return "constraint_mismatch";
4126
+ case "cycle":
4127
+ return "cycle";
4128
+ case "no_tag_context":
4129
+ return "no_tag_context";
4130
+ default:
4131
+ return "node_not_found";
4132
+ }
4133
+ }
3789
4134
  export {
3790
4135
  buildOrderSnapshot,
3791
4136
  collectFailedFallbacks,
3792
4137
  createBuilder,
4138
+ createFallbackEditor,
3793
4139
  createNodeIndex,
3794
4140
  getEligibleFallbacks,
4141
+ getFallbackRegistrationInfo,
3795
4142
  normalise,
3796
4143
  resolveServiceFallback,
3797
4144
  validate,