@timeax/digital-service-engine 0.0.3 → 0.0.5

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.
@@ -2556,6 +2556,10 @@ function getEligibleFallbacks(params) {
2556
2556
  }
2557
2557
  return eligible;
2558
2558
  }
2559
+ function getFallbackRegistrationInfo(props, nodeId) {
2560
+ const { primary, tagContexts } = primaryForNode(props, nodeId);
2561
+ return { primary, tagContexts };
2562
+ }
2559
2563
 
2560
2564
  // src/core/rate-coherence.ts
2561
2565
  function validateRateCoherenceDeep(params) {
@@ -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,