@wix/himalaya-cli 0.1749.0 → 0.1750.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 (164) hide show
  1. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/ts/src/site-shell.d.ts +2 -1
  2. package/dist/cli.mjs +13347 -8151
  3. package/guides/getting-started.md +3 -1
  4. package/guides/himi-json-output.md +54 -1
  5. package/guides/himi-skill.md +3 -1
  6. package/guides/one-app-to-himi-app.md +31 -8
  7. package/guides/skill.md +3 -1
  8. package/guides/store-submission.md +149 -34
  9. package/guides/wix-business-context.md +14 -0
  10. package/package.json +10 -10
  11. package/template-icons/personal-fitness-trainer.png +0 -0
  12. package/templates/beauty-salon/config.ts +12 -0
  13. package/templates/beauty-salon/tier5-src/_shared/booking-session.ts +3 -1
  14. package/templates/beauty-salon/tier5-src/_shared/business-chat-transport.ts +148 -0
  15. package/templates/beauty-salon/tier5-src/_shared/header-badges.ts +113 -0
  16. package/templates/beauty-salon/tier5-src/_shared/location-pin.ts +97 -0
  17. package/templates/beauty-salon/tier5-src/_shared/member-access.ts +17 -6
  18. package/templates/beauty-salon/tier5-src/_shared/source-badges.ts +72 -0
  19. package/templates/beauty-salon/tier5-src/himi-branded-lite-nav/index.ts +152 -0
  20. package/templates/beauty-salon/tier5-src/himi-store-account/index.ts +13 -4
  21. package/templates/beauty-salon/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  22. package/templates/branded-lite/config.ts +12 -0
  23. package/templates/branded-lite/l10n/fr.json +5 -0
  24. package/templates/branded-lite/l10n/he.json +4 -0
  25. package/templates/branded-lite/tier5-src/_shared/booking-session.ts +3 -1
  26. package/templates/branded-lite/tier5-src/_shared/location-pin.ts +97 -0
  27. package/templates/branded-lite/tier5-src/_shared/member-access.ts +17 -6
  28. package/templates/branded-lite/tier5-src/himi-branded-lite-access-nav/index.ts +10 -5
  29. package/templates/branded-lite/tier5-src/himi-branded-lite-nav/index.ts +152 -0
  30. package/templates/branded-lite/tier5-src/himi-branded-lite-video/index.ts +12 -1
  31. package/templates/branded-lite/tier5-src/himi-store-account/index.ts +13 -4
  32. package/templates/branded-lite/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  33. package/templates/community-club/tier5-src/_shared/member-access.ts +17 -6
  34. package/templates/community-club/tier5-src/himi-store-account/index.ts +13 -4
  35. package/templates/community-club/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  36. package/templates/conference/tier5-src/_shared/member-access.ts +17 -6
  37. package/templates/conference/tier5-src/himi-store-account/index.ts +13 -4
  38. package/templates/events-meetups/tier5-src/_shared/member-access.ts +17 -6
  39. package/templates/events-meetups/tier5-src/himi-store-account/index.ts +13 -4
  40. package/templates/events-meetups/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  41. package/templates/gym-fitness/config.ts +12 -0
  42. package/templates/gym-fitness/tier5-src/_shared/booking-session.ts +3 -1
  43. package/templates/gym-fitness/tier5-src/_shared/business-chat-transport.ts +148 -0
  44. package/templates/gym-fitness/tier5-src/_shared/header-badges.ts +113 -0
  45. package/templates/gym-fitness/tier5-src/_shared/location-pin.ts +97 -0
  46. package/templates/gym-fitness/tier5-src/_shared/member-access.ts +17 -6
  47. package/templates/gym-fitness/tier5-src/_shared/source-badges.ts +72 -0
  48. package/templates/gym-fitness/tier5-src/himi-branded-lite-nav/index.ts +152 -0
  49. package/templates/gym-fitness/tier5-src/himi-store-account/index.ts +13 -4
  50. package/templates/gym-fitness/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  51. package/templates/medical-services/tier5-src/_shared/booking-session.ts +3 -1
  52. package/templates/medical-services/tier5-src/_shared/member-access.ts +17 -6
  53. package/templates/medical-services/tier5-src/himi-store-account/index.ts +13 -4
  54. package/templates/nonprofit-cause/tier5-src/_shared/member-access.ts +17 -6
  55. package/templates/nonprofit-cause/tier5-src/himi-store-account/index.ts +13 -4
  56. package/templates/nonprofit-cause/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  57. package/templates/personal-fitness-trainer/README.md +51 -0
  58. package/templates/personal-fitness-trainer/config.ts +343 -0
  59. package/templates/personal-fitness-trainer/dev/index.ts +24813 -0
  60. package/templates/personal-fitness-trainer/himalaya.content.json +79 -0
  61. package/templates/personal-fitness-trainer/himalaya.template.json +14 -0
  62. package/templates/personal-fitness-trainer/icon.png +0 -0
  63. package/templates/personal-fitness-trainer/platforms.json +3 -0
  64. package/templates/personal-fitness-trainer/splash.json +3 -0
  65. package/templates/personal-fitness-trainer/tier5-src/_shared/app-locale.ts +61 -0
  66. package/templates/personal-fitness-trainer/tier5-src/_shared/booking-location.ts +147 -0
  67. package/templates/personal-fitness-trainer/tier5-src/_shared/booking-session.ts +224 -0
  68. package/templates/personal-fitness-trainer/tier5-src/_shared/branded-lite-hosted.ts +280 -0
  69. package/templates/personal-fitness-trainer/tier5-src/_shared/branded-load-state.ts +109 -0
  70. package/templates/personal-fitness-trainer/tier5-src/_shared/business-chat-copy.ts +40 -0
  71. package/templates/personal-fitness-trainer/tier5-src/_shared/business-chat-history.ts +51 -0
  72. package/templates/personal-fitness-trainer/tier5-src/_shared/business-chat-message-model.ts +177 -0
  73. package/templates/personal-fitness-trainer/tier5-src/_shared/business-chat-model.ts +56 -0
  74. package/templates/personal-fitness-trainer/tier5-src/_shared/business-chat-presenter.ts +576 -0
  75. package/templates/personal-fitness-trainer/tier5-src/_shared/business-chat-transport.ts +148 -0
  76. package/templates/personal-fitness-trainer/tier5-src/_shared/collection-contract.ts +228 -0
  77. package/templates/personal-fitness-trainer/tier5-src/_shared/collection-query.ts +193 -0
  78. package/templates/personal-fitness-trainer/tier5-src/_shared/collection-service.ts +400 -0
  79. package/templates/personal-fitness-trainer/tier5-src/_shared/detail-presence.ts +51 -0
  80. package/templates/personal-fitness-trainer/tier5-src/_shared/empty-flags.ts +33 -0
  81. package/templates/personal-fitness-trainer/tier5-src/_shared/engine-intl.ts +523 -0
  82. package/templates/personal-fitness-trainer/tier5-src/_shared/himi-account.ts +194 -0
  83. package/templates/personal-fitness-trainer/tier5-src/_shared/himi-booking-saved.ts +88 -0
  84. package/templates/personal-fitness-trainer/tier5-src/_shared/himi-bookings.ts +318 -0
  85. package/templates/personal-fitness-trainer/tier5-src/_shared/himi-copy.ts +116 -0
  86. package/templates/personal-fitness-trainer/tier5-src/_shared/himi-layouts.ts +142 -0
  87. package/templates/personal-fitness-trainer/tier5-src/_shared/himi-store.ts +7 -0
  88. package/templates/personal-fitness-trainer/tier5-src/_shared/himi-text.ts +24 -0
  89. package/templates/personal-fitness-trainer/tier5-src/_shared/host-events.ts +66 -0
  90. package/templates/personal-fitness-trainer/tier5-src/_shared/hosted-checkout.ts +70 -0
  91. package/templates/personal-fitness-trainer/tier5-src/_shared/iap-attendance-form.ts +106 -0
  92. package/templates/personal-fitness-trainer/tier5-src/_shared/iap-attendance-purchase.ts +216 -0
  93. package/templates/personal-fitness-trainer/tier5-src/_shared/iap-attendance.ts +38 -0
  94. package/templates/personal-fitness-trainer/tier5-src/_shared/iap-worker.ts +127 -0
  95. package/templates/personal-fitness-trainer/tier5-src/_shared/inbox-rooms.ts +68 -0
  96. package/templates/personal-fitness-trainer/tier5-src/_shared/local-preferences.ts +38 -0
  97. package/templates/personal-fitness-trainer/tier5-src/_shared/member-access.ts +63 -0
  98. package/templates/personal-fitness-trainer/tier5-src/_shared/member-account-routes.ts +52 -0
  99. package/templates/personal-fitness-trainer/tier5-src/_shared/member-bookings.ts +107 -0
  100. package/templates/personal-fitness-trainer/tier5-src/_shared/member-screen.ts +99 -0
  101. package/templates/personal-fitness-trainer/tier5-src/_shared/offset-page.ts +10 -0
  102. package/templates/personal-fitness-trainer/tier5-src/_shared/online-actions.ts +76 -0
  103. package/templates/personal-fitness-trainer/tier5-src/_shared/owner-load-error.ts +348 -0
  104. package/templates/personal-fitness-trainer/tier5-src/_shared/payment-setup.ts +6 -0
  105. package/templates/personal-fitness-trainer/tier5-src/_shared/pricing-plan-display.ts +64 -0
  106. package/templates/personal-fitness-trainer/tier5-src/_shared/rich-body.ts +78 -0
  107. package/templates/personal-fitness-trainer/tier5-src/_shared/rich-content-link.ts +34 -0
  108. package/templates/personal-fitness-trainer/tier5-src/_shared/sign-in-sheet.ts +18 -0
  109. package/templates/personal-fitness-trainer/tier5-src/_shared/staff-name.ts +18 -0
  110. package/templates/personal-fitness-trainer/tier5-src/_shared/video-caption.ts +21 -0
  111. package/templates/personal-fitness-trainer/tier5-src/_shared/weekly-goal.ts +107 -0
  112. package/templates/personal-fitness-trainer/tier5-src/_shared/wix-bi.ts +85 -0
  113. package/templates/personal-fitness-trainer/tier5-src/_shared/wix-service-price.ts +146 -0
  114. package/templates/personal-fitness-trainer/tier5-src/business-chat/index.ts +732 -0
  115. package/templates/personal-fitness-trainer/tier5-src/business-contact/index.ts +6 -0
  116. package/templates/personal-fitness-trainer/tier5-src/himi-booking-bookings/index.ts +468 -0
  117. package/templates/personal-fitness-trainer/tier5-src/himi-booking-service/index.ts +1190 -0
  118. package/templates/personal-fitness-trainer/tier5-src/himi-booking-services/index.ts +791 -0
  119. package/templates/personal-fitness-trainer/tier5-src/himi-booking-time/index.ts +1729 -0
  120. package/templates/personal-fitness-trainer/tier5-src/himi-branded-lite-account-settings/blocked-members.ts +242 -0
  121. package/templates/personal-fitness-trainer/tier5-src/himi-branded-lite-account-settings/index.ts +1094 -0
  122. package/templates/personal-fitness-trainer/tier5-src/himi-branded-lite-agenda/index.ts +955 -0
  123. package/templates/personal-fitness-trainer/tier5-src/himi-branded-lite-appearance/index.ts +94 -0
  124. package/templates/personal-fitness-trainer/tier5-src/himi-branded-lite-notifsettings/index.ts +173 -0
  125. package/templates/personal-fitness-trainer/tier5-src/himi-branded-lite-plans/index.ts +495 -0
  126. package/templates/personal-fitness-trainer/tier5-src/himi-branded-lite-splash/index.ts +35 -0
  127. package/templates/personal-fitness-trainer/tier5-src/himi-branded-lite-video/index.ts +226 -0
  128. package/templates/personal-fitness-trainer/tier5-src/himi-branded-lite-videos/index.ts +145 -0
  129. package/templates/personal-fitness-trainer/tier5-src/himi-fitness-trainer-home/index.ts +70 -0
  130. package/templates/personal-fitness-trainer/tier5-src/himi-fitness-trainer-profile/index.ts +53 -0
  131. package/templates/personal-fitness-trainer/tier5-src/himi-store-account/index.ts +1007 -0
  132. package/templates/personal-fitness-trainer/tier5-src/wix-spaces-app-language/index.ts +73 -0
  133. package/templates/personal-fitness-trainer/tier5-src/wix-spaces-delete-account/index.ts +59 -0
  134. package/templates/personal-fitness-trainer/tier5-src/wix-spaces-my-subscriptions/index.ts +84 -0
  135. package/templates/personal-fitness-trainer/tier5-src/wix-spaces-subscription-detail/index.ts +139 -0
  136. package/templates/personal-fitness-trainer/tier5-src/wix-spaces-view-profile/index.ts +119 -0
  137. package/templates/personal-fitness-trainer/tokens.json +381 -0
  138. package/templates/podcast-creator/tier5-src/_shared/member-access.ts +17 -6
  139. package/templates/podcast-creator/tier5-src/himi-store-account/index.ts +13 -4
  140. package/templates/podcast-creator/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  141. package/templates/portfolio-creative/tier5-src/_shared/member-access.ts +17 -6
  142. package/templates/portfolio-creative/tier5-src/himi-store-account/index.ts +13 -4
  143. package/templates/portfolio-creative/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  144. package/templates/restaurant/tier5-src/_shared/member-access.ts +17 -6
  145. package/templates/restaurant/tier5-src/himi-store-account/index.ts +13 -4
  146. package/templates/restaurant/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  147. package/templates/retail-store/tier5-src/_shared/member-access.ts +17 -6
  148. package/templates/retail-store/tier5-src/himi-store-account/index.ts +13 -4
  149. package/templates/retail-store/tier5-src/wix-spaces-view-profile/index.ts +68 -55
  150. package/templates/salon-spa/tier5-src/_shared/booking-session.ts +3 -1
  151. package/templates/salon-spa/tier5-src/_shared/member-access.ts +17 -6
  152. package/templates/salon-spa/tier5-src/himi-store-account/index.ts +13 -4
  153. package/templates/wholesale-store/tier5-src/_shared/member-access.ts +17 -6
  154. package/templates/wholesale-store/tier5-src/himi-store-account/index.ts +13 -4
  155. package/templates/yoga-studio/config.ts +12 -0
  156. package/templates/yoga-studio/tier5-src/_shared/booking-session.ts +3 -1
  157. package/templates/yoga-studio/tier5-src/_shared/business-chat-transport.ts +148 -0
  158. package/templates/yoga-studio/tier5-src/_shared/header-badges.ts +113 -0
  159. package/templates/yoga-studio/tier5-src/_shared/location-pin.ts +97 -0
  160. package/templates/yoga-studio/tier5-src/_shared/member-access.ts +17 -6
  161. package/templates/yoga-studio/tier5-src/_shared/source-badges.ts +72 -0
  162. package/templates/yoga-studio/tier5-src/himi-branded-lite-nav/index.ts +152 -0
  163. package/templates/yoga-studio/tier5-src/himi-store-account/index.ts +13 -4
  164. package/templates/yoga-studio/tier5-src/wix-spaces-view-profile/index.ts +68 -55
@@ -0,0 +1,94 @@
1
+ import { defineService } from "@himalaya/state";
2
+ import { dispatch } from "@himalaya/dispatch";
3
+ import { DEFAULT_THEME, THEME_IDS, THEME_KEY, isThemeId, type ThemeId } from "../_shared/himi-layouts.js";
4
+
5
+ // The in-app Appearance worker: the app color theme (System default / Light / Dark).
6
+ // A tap applies the choice at once. The worker cannot repaint the app itself — tokens
7
+ // and native chrome belong to the host — so it hands the choice to the host through the
8
+ // `theme.set` host action (iOS, Android and the web host register it), the same seam
9
+ // App language uses for `locale.set`. No declared capability: a host action is not one.
10
+ //
11
+ // The choice is also mirrored in worker storage, so the radios show it on a host that
12
+ // does not answer `theme.get` (an older binary). On such a host the selection is kept,
13
+ // but the app does not change colour until the binary is updated.
14
+ //
15
+ // It replaced an accent-colour picker (six swatches that tinted a few brand surfaces),
16
+ // which did not suit every brand. The home worker no longer reads an accent.
17
+
18
+ interface State {
19
+ /** The applied theme id. */
20
+ theme: string;
21
+ // Per-option flags for the radio group. Flat fields: the three rows are static nodes,
22
+ // and a `$service:` path cannot index a map by `$item.`.
23
+ [flag: `selected_${string}`]: boolean | string;
24
+ }
25
+
26
+ interface StorageLike {
27
+ getItem(k: string): string | null;
28
+ setItem(k: string, v: string): void;
29
+ }
30
+ function storage(): StorageLike | undefined {
31
+ const host = globalThis as { himalaya?: { storage?: StorageLike }; localStorage?: StorageLike };
32
+ return host.himalaya?.storage ?? host.localStorage;
33
+ }
34
+ function storedTheme(): ThemeId {
35
+ try {
36
+ const s = storage()?.getItem(THEME_KEY) ?? null;
37
+ return isThemeId(s) ? s : DEFAULT_THEME;
38
+ } catch { return DEFAULT_THEME; }
39
+ }
40
+ function remember(theme: ThemeId): void {
41
+ try { storage()?.setItem(THEME_KEY, theme); } catch { /* best-effort: the host holds the real choice */ }
42
+ }
43
+ function paramValue(arg: unknown, key: string): string {
44
+ if (typeof arg === "string") return arg;
45
+ const result = (arg as Record<string, unknown> | undefined)?.[key];
46
+ return typeof result === "string" ? result : "";
47
+ }
48
+
49
+ function build(theme: ThemeId): State {
50
+ const flags: Record<string, boolean> = {};
51
+ for (const id of THEME_IDS) flags[`selected_${id}`] = id === theme;
52
+ return { ...flags, theme };
53
+ }
54
+
55
+ // Invalidates async host reads/writes as soon as a newer member choice is made.
56
+ let choiceRevision = 0;
57
+
58
+ const svc = defineService<State, {
59
+ sync: (state: State, arg?: unknown) => Promise<State>;
60
+ selectTheme: (state: State, arg?: unknown) => Promise<State>;
61
+ }>("himiBrandedAppearance", {
62
+ state: build(storedTheme()),
63
+ actions: {
64
+ // onAppear: the host is the source of truth (the member may have changed the theme
65
+ // on another screen instance, or the host restored it at launch). Fall back to the
66
+ // mirrored value where the host has no `theme.get`.
67
+ sync: async () => {
68
+ // A delayed onAppear read must not undo a choice made while it was in flight.
69
+ const choiceAtStart = choiceRevision;
70
+ try {
71
+ const payload = (await dispatch("theme.get")) as { scheme?: unknown } | null;
72
+ if (choiceAtStart !== choiceRevision) return build(storedTheme());
73
+ if (isThemeId(payload?.scheme)) {
74
+ remember(payload.scheme);
75
+ return build(payload.scheme);
76
+ }
77
+ } catch { /* no host handler (older binary): keep the mirrored choice */ }
78
+ return build(storedTheme());
79
+ },
80
+ // A tap on a radio applies that theme to the whole app at once.
81
+ selectTheme: async (state, arg) => {
82
+ const id = paramValue(arg, "id");
83
+ if (!isThemeId(id)) return state;
84
+ const revision = ++choiceRevision;
85
+ remember(id);
86
+ try {
87
+ await dispatch("theme.set", { scheme: id });
88
+ } catch { /* no host handler (older binary): the selection is kept, the app keeps its colours */ }
89
+ return revision === choiceRevision ? build(id) : build(storedTheme());
90
+ },
91
+ },
92
+ });
93
+
94
+ export { svc };
@@ -0,0 +1,173 @@
1
+ import { withOnlineWrites } from "../_shared/online-actions.js";
2
+ // himi-branded-lite-notifsettings — reads the site's notification topics + the member's preferences.
3
+ // a failed query is an error with Retry (or Sign in when Wix refuses the
4
+ // visitor) and an empty one is a real empty state — never sample rows presented as
5
+ // the customer's own. Samples appear only in the host's explicit sample preview
6
+ // (_shared/branded-load-state.ts).
7
+ // any exist. INTERACTIVE: tapping a row toggles that topic On/Off — optimistic +
8
+ // best-effort upsertMyNotificationPreferences (a no-op on sample rows).
9
+ import { defineService as registerService } from "@himalaya/state";
10
+ import { withEmptyFlags } from "../_shared/empty-flags.js";
11
+ import { stashCopy } from "../_shared/himi-copy.js";
12
+ import { memberAccess, assertMemberAccess, type MemberAccess } from "../_shared/member-access.js";
13
+ import { LOADING, loaded, loadFailed, type LoadCopy, type LoadFlags } from "../_shared/branded-load-state.js";
14
+ import {
15
+ getNotificationTopics,
16
+ getMyNotificationPreferences,
17
+ upsertMyNotificationPreferences,
18
+ muteStatusFor,
19
+ readTopics,
20
+ readPreferences,
21
+ isDeliveredNow,
22
+ isLocked,
23
+ preferenceFor,
24
+ type NotificationTopicDefinition,
25
+ type PreferenceInfo,
26
+ } from "@himalaya/wix-notification-preferences";
27
+
28
+ /** The channel this surface delivers on — read for the row state, written on toggle.
29
+ * One constant, so the read and the write cannot drift apart. */
30
+ const CHANNEL = "MOBILE";
31
+
32
+ /** `note` is the line under a row's switch — only a locked topic has one. */
33
+ interface Row { id: string; name: string; subtitle: string; note: string; on: boolean; locked: boolean;
34
+ /** Inverse of `locked` (hiddenWhen hides on TRUE) and the plain On/Off a locked row shows. */
35
+ notLocked: boolean; stateLabel: string }
36
+ interface S extends LoadFlags {
37
+ preferencesMember: MemberAccess | null;
38
+ topics: Row[]; emptyText: string; actionMessage: string;
39
+ isMock: boolean; notMock: boolean; /** #2849 — hiddenWhen() hides on TRUE, so these are inverted: the empty-state banner
40
+ * shows only when the site really has none, and the unavailable one only when the
41
+ * query failed. A failed query must never render the sentence that claims the
42
+ * customer has no content. */
43
+ notMockEmpty: boolean; notMockUnavailable: boolean;
44
+ }
45
+
46
+ /** Whether this topic is actually being DELIVERED — not merely what the recipient
47
+ * asked for. `isDeliveredNow` prefers the server's `granted` verdict, so a topic the
48
+ * member unmuted under a channel-wide mute renders OFF instead of promising a
49
+ * notification that will never arrive. No preference row at all ⇒ the topic's default,
50
+ * which is delivery. */
51
+ function isOn(topicId: string, prefs: PreferenceInfo[]): boolean {
52
+ const p = preferenceFor(topicId, prefs, CHANNEL);
53
+ if (!p) return true;
54
+ return isDeliveredNow(p.stateData);
55
+ }
56
+
57
+ /** A row the server says cannot be changed here (a broader rule overrides it). */
58
+ function locked(topicId: string, prefs: PreferenceInfo[]): boolean {
59
+ return isLocked(preferenceFor(topicId, prefs, CHANNEL)?.stateData);
60
+ }
61
+
62
+ /** Row subtitle doubles as an affordance: current state + how to change it. */
63
+ function subtitleFor(on: boolean): string {
64
+ return on ? "On · tap to mute" : "Off · tap to enable";
65
+ }
66
+
67
+ function row(id: string, name: string, on: boolean): Row {
68
+ return { id, name, subtitle: subtitleFor(on), note: "", on, locked: false, notLocked: true, stateLabel: on ? "On" : "Off" };
69
+ }
70
+
71
+ function norm(t: NotificationTopicDefinition, prefs: PreferenceInfo[]): Row {
72
+ const name = (t.displayName || "Notification").trim() || "Notification";
73
+ const id = t.topicId || name;
74
+ const r = row(id, name, isOn(id, prefs));
75
+ // An overridden row says so instead of offering a toggle that cannot take effect.
76
+ return locked(id, prefs)
77
+ ? { ...r, locked: true, notLocked: false, subtitle: "Managed by your channel settings", note: "Managed by your channel settings" }
78
+ : r;
79
+ }
80
+
81
+ const MOCK: Row[] = [
82
+ row("sample-orders", "Order updates", true),
83
+ row("sample-bookings", "Bookings & reminders", true),
84
+ row("sample-promos", "Promotions & offers", false),
85
+ row("sample-community", "Community activity", true),
86
+ ];
87
+
88
+ /** sample rows reach the screen ONLY in the host's explicit sample preview;
89
+ * a failed or empty query is an honest error / empty state (branded-load-state.ts). */
90
+ const COPY: LoadCopy<Row> = {
91
+ prefix: "notifsettings",
92
+ empty: "No notification topics to manage yet.",
93
+ error: "Notification settings couldn't be loaded.",
94
+ signIn: "Sign in to manage your notifications.",
95
+ sample: () => MOCK.map((r) => ({ ...r })),
96
+ };
97
+
98
+ const defineService = withOnlineWrites(registerService, ["toggle"], { messageFields: ["actionMessage"] });
99
+ const INITIAL = { ...LOADING, topics: [] as Row[], emptyText: "", actionMessage: "", preferencesMember: null };
100
+ const signInRequired = () => new Error("Unauthenticated: sign in to manage notifications");
101
+ let loadVersion = 0;
102
+ const svc = defineService<S, {
103
+ prepare: (s: S) => S;
104
+ load: (s: S, arg?: unknown) => Promise<S | undefined>;
105
+ toggle: (s: S, p?: { id?: string; value?: unknown }) => Promise<S | undefined>;
106
+ }>("himiBrandedNotifSettings", withEmptyFlags({
107
+ state: INITIAL,
108
+ actions: {
109
+ prepare: (s) => ({ ...s, ...INITIAL }),
110
+ load: async (s, arg): Promise<S | undefined> => {
111
+ stashCopy(arg);
112
+ const version = ++loadVersion;
113
+ await svc.prepare();
114
+ try {
115
+ const member = await memberAccess();
116
+ if (!member.email) throw signInRequired();
117
+ const [topicsRes, prefsRes] = await Promise.all([
118
+ getNotificationTopics(),
119
+ getMyNotificationPreferences(),
120
+ ]);
121
+ await assertMemberAccess(member);
122
+ const prefs = readPreferences(prefsRes);
123
+ const topics = readTopics(topicsRes).map((t) => norm(t, prefs));
124
+ return version === loadVersion ? { ...s, ...loaded("topics", topics, COPY), preferencesMember: member, actionMessage: "" } : undefined;
125
+ } catch {
126
+ const member = await memberAccess();
127
+ // An authenticated denial is not repaired by an endless sign-in loop.
128
+ const failure = member.email ? new Error(COPY.error) : signInRequired();
129
+ return version === loadVersion ? { ...s, ...loadFailed("topics", failure, COPY), preferencesMember: null, actionMessage: "" } : undefined;
130
+ }
131
+ },
132
+ // Flip one topic's On/Off. Optimistic (updates the row immediately); the real
133
+ // upsert is best-effort and a no-op on sample rows.
134
+ toggle: async (s, p): Promise<S | undefined> => {
135
+ const id = p?.id;
136
+ const target = s.topics.find((r) => r.id === id);
137
+ if (s.busy || !id || !target || target.locked) return undefined;
138
+ if (!s.isMock) {
139
+ try {
140
+ if (!s.preferencesMember) return undefined;
141
+ await assertMemberAccess(s.preferencesMember);
142
+ if (svc.getState().preferencesMember !== s.preferencesMember) return undefined;
143
+ } catch {
144
+ await svc.load();
145
+ return undefined;
146
+ }
147
+ }
148
+ let nextOn = true;
149
+ const topics = s.topics.map((r) => {
150
+ if (r.id !== id) return r;
151
+ // A Toggle sends the NEXT state as `value` (action-payload contract); honour it so
152
+ // a repeated event can never flip the row the wrong way. A bare tap still flips.
153
+ nextOn = typeof p?.value === "boolean" ? p.value : !r.on;
154
+ return { ...r, on: nextOn, subtitle: subtitleFor(nextOn), stateLabel: nextOn ? "On" : "Off" };
155
+ });
156
+ if (!s.isMock && !id.startsWith("sample-")) {
157
+ try {
158
+ // `stateData.state` is a SCALAR enum — the old nested `{ muteStatus }` was a
159
+ // shape this service does not have, so every write was malformed.
160
+ await upsertMyNotificationPreferences([
161
+ { topicId: id, channel: CHANNEL, stateData: { state: muteStatusFor(nextOn) } },
162
+ ]);
163
+ } catch { /* keep the optimistic state */ }
164
+ try { await assertMemberAccess(s.preferencesMember!); }
165
+ catch { await svc.load(); return undefined; }
166
+ }
167
+ return svc.getState().preferencesMember === s.preferencesMember ? { ...svc.getState(), topics } : undefined;
168
+ },
169
+ },
170
+ }, { topics: "noTopics" }));
171
+ svc.prepare();
172
+ svc.load();
173
+ export { svc };