@blogmcp/sdk 0.8.1 → 0.9.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 (88) hide show
  1. package/dist/community/channels.d.ts +12 -0
  2. package/dist/community/channels.js +7 -0
  3. package/dist/community/client.d.ts +211 -0
  4. package/dist/community/client.js +188 -0
  5. package/dist/community/index.d.ts +34 -0
  6. package/dist/community/index.js +92 -0
  7. package/dist/community/links.d.ts +65 -0
  8. package/dist/community/links.js +81 -0
  9. package/dist/community/messages.cs.d.ts +331 -0
  10. package/dist/community/messages.cs.js +342 -0
  11. package/dist/community/messages.d.ts +40 -0
  12. package/dist/community/messages.en.d.ts +315 -0
  13. package/dist/community/messages.en.js +313 -0
  14. package/dist/community/messages.js +66 -0
  15. package/dist/community/outcome.d.ts +96 -0
  16. package/dist/community/outcome.js +211 -0
  17. package/dist/community/poll.d.ts +35 -0
  18. package/dist/community/poll.js +70 -0
  19. package/dist/community/routes.d.ts +110 -0
  20. package/dist/community/routes.js +66 -0
  21. package/dist/community/server/bff.d.ts +123 -0
  22. package/dist/community/server/bff.js +214 -0
  23. package/dist/community/server/env.d.ts +20 -0
  24. package/dist/community/server/env.js +16 -0
  25. package/dist/community/server/host-routes.d.ts +15 -0
  26. package/dist/community/server/host-routes.js +35 -0
  27. package/dist/community/server/http.d.ts +64 -0
  28. package/dist/community/server/http.js +90 -0
  29. package/dist/community/server/index.d.ts +45 -0
  30. package/dist/community/server/index.js +19 -0
  31. package/dist/community/server/route-decision.d.ts +70 -0
  32. package/dist/community/server/route-decision.js +89 -0
  33. package/dist/community/server/unknown-path.d.ts +21 -0
  34. package/dist/community/server/unknown-path.js +11 -0
  35. package/dist/community/shapes.d.ts +457 -0
  36. package/dist/community/shapes.js +51 -0
  37. package/dist/community/store.d.ts +183 -0
  38. package/dist/community/store.js +423 -0
  39. package/dist/community/transport.d.ts +42 -0
  40. package/dist/community/transport.js +59 -0
  41. package/dist/core/client.d.ts +17 -0
  42. package/dist/core/client.js +58 -55
  43. package/dist/core/comments-legacy.d.ts +19 -0
  44. package/dist/core/comments-legacy.js +18 -0
  45. package/dist/core/index.d.ts +3 -0
  46. package/dist/core/index.js +107 -101
  47. package/dist/core/queries.d.ts +46 -0
  48. package/dist/core/queries.js +128 -84
  49. package/dist/core/seo.js +40 -39
  50. package/dist/core/translations/cs.json.js +6 -6
  51. package/dist/core/translations/en.json.js +1 -1
  52. package/dist/core/types.d.ts +23 -1
  53. package/dist/core/view-model.d.ts +7 -0
  54. package/dist/core/view-model.js +29 -25
  55. package/dist/react/KbMdxContent.js +4 -3
  56. package/dist/vue/components/KbMdxContent.vue2.js +3 -2
  57. package/dist/vue/composables/useKbComments.d.ts +8 -0
  58. package/package.json +13 -4
  59. package/src/community/channels.ts +12 -0
  60. package/src/community/client.ts +437 -0
  61. package/src/community/index.ts +190 -0
  62. package/src/community/links.ts +166 -0
  63. package/src/community/messages.cs.ts +371 -0
  64. package/src/community/messages.en.ts +341 -0
  65. package/src/community/messages.ts +112 -0
  66. package/src/community/outcome.ts +317 -0
  67. package/src/community/poll.ts +154 -0
  68. package/src/community/routes.ts +139 -0
  69. package/src/community/server/bff.ts +530 -0
  70. package/src/community/server/env.ts +42 -0
  71. package/src/community/server/host-routes.ts +48 -0
  72. package/src/community/server/http.ts +175 -0
  73. package/src/community/server/index.ts +55 -0
  74. package/src/community/server/route-decision.ts +226 -0
  75. package/src/community/server/unknown-path.ts +32 -0
  76. package/src/community/shapes.ts +513 -0
  77. package/src/community/store.ts +786 -0
  78. package/src/community/transport.ts +111 -0
  79. package/src/core/client.ts +24 -1
  80. package/src/core/comments-legacy.ts +39 -0
  81. package/src/core/index.ts +8 -0
  82. package/src/core/queries.ts +93 -0
  83. package/src/core/seo.ts +5 -1
  84. package/src/core/translations/cs.json +3 -1
  85. package/src/core/translations/en.json +3 -1
  86. package/src/core/types.ts +19 -1
  87. package/src/core/view-model.ts +12 -1
  88. package/src/vue/composables/useKbComments.ts +8 -0
@@ -0,0 +1,51 @@
1
+ const t = (e, n) => typeof n == "string" && e.includes(n), _ = Object.freeze(["visible", "held", "removed", "deleted"]), y = (e) => t(_, e), f = Object.freeze(["member", "staff", "system"]), l = (e) => t(f, e), u = Object.freeze(["sign_in", "onboarding", "reconsent_rules", "art9_consent"]), h = (e) => t(u, e), d = Object.freeze(["restriction", "suspension", "ban"]), m = (e) => t(d, e), p = Object.freeze(["decision", "appeal_resolved", "erasure_scheduled", "inactivity_warning"]), E = (e) => t(p, e), O = Object.freeze([
2
+ "fraud_spam",
3
+ "harassment",
4
+ "hate",
5
+ "threats",
6
+ "sexual_content",
7
+ "child_at_risk",
8
+ "self_harm",
9
+ "doxxing",
10
+ "copyright",
11
+ "terrorism",
12
+ "drugs",
13
+ "other_illegal",
14
+ "rules",
15
+ "adverse_event",
16
+ "third_party_health_data",
17
+ "suspected_minor"
18
+ ]), b = (e) => t(O, e), A = "child_at_risk", S = Object.freeze(["16_17", "18_plus"]), T = Object.freeze(["rules", "terms", "privacy", "art9_consent"]);
19
+ function g(e) {
20
+ return typeof e == "object" && e !== null && l(e.kind);
21
+ }
22
+ function j(e) {
23
+ const n = typeof e == "object" && e !== null ? e.rules : void 0;
24
+ if (!Array.isArray(n)) return [];
25
+ const r = [];
26
+ for (const s of n) {
27
+ if (typeof s != "object" || s === null) continue;
28
+ const { id: o, title: i, summary: c, anchor: a } = s;
29
+ typeof o != "string" || typeof i != "string" || typeof a != "string" || r.push(Object.freeze({ id: o, title: i, summary: typeof c == "string" ? c : null, anchor: a }));
30
+ }
31
+ return r;
32
+ }
33
+ export {
34
+ S as AGE_BANDS,
35
+ f as AUTHOR_KINDS,
36
+ O as CASE_CATEGORIES,
37
+ A as CATEGORY_WITHOUT_NOTIFIER,
38
+ T as LEGAL_TEXT_KINDS,
39
+ _ as MESSAGE_STATES,
40
+ u as NEEDS,
41
+ p as NOTIFICATION_CODES,
42
+ d as RESTRICTION_KINDS,
43
+ j as communityRulesOf,
44
+ b as isCaseCategory,
45
+ g as isKnownAuthor,
46
+ l as isKnownAuthorKind,
47
+ h as isKnownNeed,
48
+ E as isKnownNotificationCode,
49
+ m as isKnownRestrictionKind,
50
+ y as isKnownState
51
+ };
@@ -0,0 +1,183 @@
1
+ /**
2
+ * One article's discussion as an immutable two-level tree (docs/internal/m1-contracts.md §10.2), framework-free:
3
+ * a page renders `getState()` and re-renders on `subscribe`. Every change builds a new state and shares every
4
+ * message it did not touch, so a Vue or React view can compare by identity.
5
+ *
6
+ * applyPage a `discussions/list` page merged by its cursor range (refreshDiscussion, loadMoreDiscussion);
7
+ * applyMessage an optimistic post, edit or delete under a `clientMsgId`, then the server's row (`confirm`) or the
8
+ * undo (`rollback`);
9
+ * applyHead a `discussions/head` answer: true when it is newer than what is loaded (→ refresh);
10
+ * applyReaction a reaction summary (the server's, or reactionAfter's optimistic one);
11
+ * collapse a member blocked or unblocked: their messages collapse or open;
12
+ * viewerNeeds what the composer still waits for.
13
+ *
14
+ * Open enums (community.md §3.7): a message whose author kind or state this SDK does not know is dropped — with the
15
+ * replies under it — and so is a need or a restriction kind it does not know. Nothing a newer kb-api sends throws.
16
+ *
17
+ * One store may show one article after another (reset(), or a page of another article): an answer that outlives
18
+ * the switch — a write's confirm, a server row, a refresh or „load more" still on its way — changes nothing.
19
+ */
20
+ import type { CommunityCallOptions, CommunityClient } from './client.js';
21
+ import type { CommunityOutcome } from './outcome.js';
22
+ import { type Discussion, type DiscussionHead, type KnownAuthor, type KnownMessageState, type KnownNeed, type KnownRestrictionKind, type PublicMessage, type ReactionSummary, type ReactionTarget } from './shapes.js';
23
+ /** A message as the store keeps it: a known author and state, and the optimistic change that waits, if any. */
24
+ export interface DiscussionMessage extends Omit<PublicMessage, 'author' | 'state' | 'replies' | 'reactions'> {
25
+ readonly author: KnownAuthor;
26
+ readonly state: KnownMessageState;
27
+ readonly reactions: ReactionSummary;
28
+ /** On a top-level message: its replies, oldest first (a reply has none). */
29
+ readonly replies: readonly DiscussionMessage[];
30
+ /** An optimistic change waiting for the server: render it as such (no reply, edit or reaction meanwhile). */
31
+ readonly pending?: 'post' | 'edit' | 'delete';
32
+ /** The id of that change (a post's `clientMsgId`). */
33
+ readonly clientMsgId?: string;
34
+ /** The filters' warning codes the member's own write came back with (`messages/post`, `messages/edit`). */
35
+ readonly warnings?: readonly string[];
36
+ }
37
+ export interface DiscussionRestriction {
38
+ readonly kind: KnownRestrictionKind;
39
+ readonly scope: string;
40
+ readonly endsAt: string | null;
41
+ readonly decisionId: string;
42
+ }
43
+ export interface DiscussionViewer {
44
+ readonly memberId: string | null;
45
+ readonly canPost: boolean;
46
+ readonly needs: readonly KnownNeed[];
47
+ readonly restrictions: readonly DiscussionRestriction[];
48
+ }
49
+ export interface DiscussionState {
50
+ /** The article of the pages applied (lower case, as kb-api answers it), or null before the first. */
51
+ readonly articleId: string | null;
52
+ readonly conversationId: string | null;
53
+ /** The messages everyone is shown. */
54
+ readonly count: number;
55
+ readonly lastSeq: number;
56
+ readonly lastChangeAt: string | null;
57
+ /** The loaded top-level messages, oldest first; an optimistic post comes last. */
58
+ readonly messages: readonly DiscussionMessage[];
59
+ /**
60
+ * Where „load more" continues — the end of the pages loaded from the first one on (a message the viewer just
61
+ * posted is no page of them) —, or null when the end is loaded.
62
+ */
63
+ readonly nextCursor: number | null;
64
+ readonly articleReactions: ReactionSummary;
65
+ readonly reactionSet: readonly string[];
66
+ readonly preset: string | null;
67
+ readonly viewer: DiscussionViewer;
68
+ /** A page was applied. */
69
+ readonly loaded: boolean;
70
+ /** A head newer than the loaded pages arrived: refresh. */
71
+ readonly stale: boolean;
72
+ }
73
+ /** An optimistic change and how it settles. `clientMsgId` names the change from start to end. */
74
+ export type MessageChange =
75
+ /** A new message (a reply with `replyToSeq`): shown at once, last, as `pending: 'post'`. */
76
+ {
77
+ readonly type: 'post';
78
+ readonly clientMsgId: string;
79
+ readonly body: string;
80
+ readonly replyToSeq?: number | null;
81
+ /** The member as their own messages show them. */
82
+ readonly author: KnownAuthor;
83
+ readonly createdAt?: string;
84
+ readonly languageCode?: string | null;
85
+ }
86
+ /** The member's own message with a new text. */
87
+ | {
88
+ readonly type: 'edit';
89
+ readonly clientMsgId: string;
90
+ readonly seq: number;
91
+ readonly body: string;
92
+ }
93
+ /** The member's own message, deleted. */
94
+ | {
95
+ readonly type: 'delete';
96
+ readonly clientMsgId: string;
97
+ readonly seq: number;
98
+ }
99
+ /**
100
+ * The server's answer to the change: its row replaces the optimistic one (`warnings` from the write's answer).
101
+ * The answer to a change reset() dropped, or a row of another conversation, is not this discussion's: ignored.
102
+ */
103
+ | {
104
+ readonly type: 'confirm';
105
+ readonly clientMsgId: string;
106
+ readonly message: PublicMessage;
107
+ readonly warnings?: readonly string[];
108
+ }
109
+ /** The server refused the change: it is undone. */
110
+ | {
111
+ readonly type: 'rollback';
112
+ readonly clientMsgId: string;
113
+ }
114
+ /** A row from the server outside an optimistic change: replaces the loaded one of its conversation and seq, if any. */
115
+ | {
116
+ readonly type: 'server';
117
+ readonly message: PublicMessage;
118
+ };
119
+ export interface DiscussionStore {
120
+ getState(): DiscussionState;
121
+ /** Called after every change with the new state; returns the unsubscribe. */
122
+ subscribe(listener: (state: DiscussionState) => void): () => void;
123
+ /** A page of `discussions/list`; `cursor` is the one it was asked with (null or absent: the first page). */
124
+ applyPage(page: Discussion, options?: {
125
+ cursor?: number | null;
126
+ }): void;
127
+ applyMessage(change: MessageChange): void;
128
+ /** True when the head is newer than the loaded pages (the state turns `stale`): refresh. */
129
+ applyHead(head: DiscussionHead): boolean;
130
+ applyReaction(target: ReactionTarget, reactions: ReactionSummary): void;
131
+ /** The viewer blocked (true) or unblocked (false) a member. */
132
+ collapse(memberId: string, blocked: boolean): void;
133
+ /** Back to the empty state (another article); the answers still on their way are dropped when they come. */
134
+ reset(): void;
135
+ }
136
+ export declare function createDiscussionStore(): DiscussionStore;
137
+ /** What the composer waits for, from the viewer's known needs. */
138
+ export interface ViewerNeeds {
139
+ /** No session that can write: offer the sign-in. */
140
+ readonly signIn: boolean;
141
+ /** A first join: the age band, the rules, the profile. */
142
+ readonly onboarding: boolean;
143
+ /** The rules' acceptance (a first join, or a newer rules text that asks again). */
144
+ readonly rules: boolean;
145
+ /** The consent the tenant asks for before writing. */
146
+ readonly art9: boolean;
147
+ /** Any of these. */
148
+ readonly any: boolean;
149
+ }
150
+ export declare function viewerNeeds(state: Pick<DiscussionState, 'viewer'> | {
151
+ needs: readonly unknown[];
152
+ }): ViewerNeeds;
153
+ /** A loaded message (top-level or reply) by its seq, or null. */
154
+ export declare function findMessage(state: Pick<DiscussionState, 'messages'>, seq: number): DiscussionMessage | null;
155
+ /** The viewer wrote it (a member message whose author is the viewer). */
156
+ export declare function isOwnMessage(state: Pick<DiscussionState, 'viewer'>, message: Pick<DiscussionMessage, 'author'>): boolean;
157
+ /** The reactions of the article or of one loaded message. */
158
+ export declare function reactionsFor(state: DiscussionState, target: ReactionTarget): ReactionSummary;
159
+ /**
160
+ * The summary after the viewer sets `emoji` (null: removes theirs) — one reaction per member, so the previous one
161
+ * gives up its count. For an optimistic `applyReaction` before `reactions.set` answers; keep the old summary to undo.
162
+ */
163
+ export declare function reactionAfter(summary: ReactionSummary, emoji: string | null): ReactionSummary;
164
+ type ListClient = Pick<CommunityClient, 'discussions'>;
165
+ /**
166
+ * Loads the discussion, or brings the loaded pages up to date (after a newer head): the first page, then the next
167
+ * ones until the pages loaded before are covered — to the end when the end was loaded. The first failure is the
168
+ * outcome (the pages read before it stay applied). A page that comes back after the store moved on (reset(), a
169
+ * page of another article) is not applied, and the refresh ends there with the store as it is.
170
+ */
171
+ export declare function refreshDiscussion(store: DiscussionStore, client: ListClient, input: {
172
+ articleId: string;
173
+ languageCode?: string | null;
174
+ }, options?: CommunityCallOptions): Promise<CommunityOutcome<DiscussionState>>;
175
+ /**
176
+ * The next page after the loaded ones („load more"); a store with the end loaded asks nothing, and a page that
177
+ * comes back after the store moved on (reset(), a page of another article) is not applied.
178
+ */
179
+ export declare function loadMoreDiscussion(store: DiscussionStore, client: ListClient, input: {
180
+ articleId: string;
181
+ languageCode?: string | null;
182
+ }, options?: CommunityCallOptions): Promise<CommunityOutcome<DiscussionState>>;
183
+ export {};
@@ -0,0 +1,423 @@
1
+ import { isKnownNeed as H, isKnownState as ce, isKnownRestrictionKind as ue, isKnownAuthorKind as ae } from "./shapes.js";
2
+ const O = (e) => typeof e == "object" && e !== null && !Array.isArray(e), u = (e) => typeof e == "string" ? e : null, N = (e) => typeof e == "number" && Number.isSafeInteger(e) && e > 0, R = Object.freeze({ counts: Object.freeze({}), mine: null });
3
+ function D(e) {
4
+ if (!O(e)) return R;
5
+ const n = {};
6
+ if (O(e.counts))
7
+ for (const [s, r] of Object.entries(e.counts))
8
+ typeof r == "number" && Number.isSafeInteger(r) && r > 0 && (n[s] = r);
9
+ return Object.freeze({ counts: Object.freeze(n), mine: u(e.mine) });
10
+ }
11
+ const U = Object.freeze({
12
+ kind: "member",
13
+ memberId: null,
14
+ handle: null,
15
+ displayName: null,
16
+ avatarPreset: null,
17
+ deleted: !0
18
+ }), de = Object.freeze({ kind: "system" });
19
+ function Y(e) {
20
+ if (!O(e) || !ae(e.kind)) return null;
21
+ if (e.kind === "system") return de;
22
+ if (e.kind === "staff")
23
+ return Object.freeze({ kind: "staff", staffId: u(e.staffId) ?? "", displayName: u(e.displayName) ?? "", badge: u(e.badge) ?? "" });
24
+ const n = u(e.memberId), s = u(e.handle), r = u(e.displayName);
25
+ return n === null || s === null || r === null || e.deleted === !0 ? U : Object.freeze({ kind: "member", memberId: n, handle: s, displayName: r, avatarPreset: u(e.avatarPreset) });
26
+ }
27
+ function G(e) {
28
+ if (Array.isArray(e))
29
+ return Object.freeze(e.filter((n) => typeof n == "string"));
30
+ }
31
+ function L(e, n) {
32
+ if (!O(e)) return null;
33
+ const s = Y(e.author);
34
+ if (s === null || !ce(e.state)) return null;
35
+ const r = u(e.conversationId), i = u(e.id);
36
+ if (r === null || i === null || !N(e.seq)) return null;
37
+ const p = u(e.body), b = [];
38
+ if (n && Array.isArray(e.replies))
39
+ for (const A of e.replies) {
40
+ const v = L(A, !1);
41
+ v && b.push(v);
42
+ }
43
+ const g = G(e.notes);
44
+ return Object.freeze({
45
+ conversationId: r,
46
+ seq: e.seq,
47
+ id: i,
48
+ rev: typeof e.rev == "number" ? e.rev : 0,
49
+ replyToSeq: n ? null : N(e.replyToSeq) ? e.replyToSeq : null,
50
+ author: s,
51
+ state: e.state,
52
+ body: p,
53
+ displayBody: u(e.displayBody) ?? p,
54
+ displayLanguageCode: u(e.displayLanguageCode),
55
+ sourceLanguageCode: u(e.sourceLanguageCode),
56
+ isTranslated: e.isTranslated === !0,
57
+ isEdited: e.isEdited === !0,
58
+ createdAt: u(e.createdAt) ?? "",
59
+ editedAt: u(e.editedAt),
60
+ collapsed: e.collapsed === !0,
61
+ reactions: D(e.reactions),
62
+ translationVersionId: u(e.translationVersionId),
63
+ ...g && g.length > 0 ? { notes: g } : {},
64
+ // Only as kb-api sends it: true, on a `held` message (the author's own, held back by the art. 9 policy).
65
+ ...e.hiddenUntilConsent === !0 && e.state === "held" ? { hiddenUntilConsent: !0 } : {},
66
+ replies: j(b),
67
+ ...n && e.repliesTruncated === !0 ? { repliesTruncated: !0 } : {}
68
+ });
69
+ }
70
+ function fe(e) {
71
+ const n = O(e) ? e : {}, s = Array.isArray(n.needs) ? n.needs.filter(H) : [], r = [];
72
+ if (Array.isArray(n.restrictions))
73
+ for (const i of n.restrictions)
74
+ !O(i) || !ue(i.kind) || typeof i.decisionId != "string" || r.push(Object.freeze({ kind: i.kind, scope: u(i.scope) ?? "", endsAt: u(i.endsAt), decisionId: i.decisionId }));
75
+ return Object.freeze({
76
+ memberId: u(n.memberId),
77
+ canPost: n.canPost === !0,
78
+ needs: Object.freeze([...new Set(s)]),
79
+ restrictions: Object.freeze(r)
80
+ });
81
+ }
82
+ const pe = Object.freeze({ memberId: null, canPost: !1, needs: Object.freeze([]), restrictions: Object.freeze([]) }), _ = Object.freeze({
83
+ articleId: null,
84
+ conversationId: null,
85
+ count: 0,
86
+ lastSeq: 0,
87
+ lastChangeAt: null,
88
+ messages: Object.freeze([]),
89
+ nextCursor: null,
90
+ articleReactions: R,
91
+ reactionSet: Object.freeze([]),
92
+ preset: null,
93
+ viewer: pe,
94
+ loaded: !1,
95
+ stale: !1
96
+ }), ge = (e, n) => e === null ? n : n === null ? e : Date.parse(n) > Date.parse(e) ? n : e;
97
+ function j(e) {
98
+ const n = e.filter((r) => r.seq > 0).sort((r, i) => r.seq - i.seq), s = e.filter((r) => r.seq < 0).sort((r, i) => i.seq - r.seq);
99
+ return Object.freeze([...n, ...s]);
100
+ }
101
+ function z(e, n) {
102
+ let s = !1;
103
+ const r = [];
104
+ for (const i of e) {
105
+ let p = n(i);
106
+ if (p !== null && p.replies.length > 0) {
107
+ const b = z(p.replies, n);
108
+ b !== p.replies && (p = Object.freeze({ ...p, replies: b }));
109
+ }
110
+ p !== i && (s = !0), p !== null && r.push(p);
111
+ }
112
+ return s ? Object.freeze(r) : e;
113
+ }
114
+ function h(e, n) {
115
+ for (const s of e) {
116
+ if (n(s)) return s;
117
+ for (const r of s.replies) if (n(r)) return r;
118
+ }
119
+ return null;
120
+ }
121
+ function W(e, n) {
122
+ return z(e, (s) => s.seq === n.seq ? s.replyToSeq === null && n.replyToSeq === null ? Object.freeze({ ...n, replies: s.replies }) : n : s);
123
+ }
124
+ function be(e, n) {
125
+ if (h(e, (r) => r.seq === n.seq)) return W(e, n);
126
+ if (n.replyToSeq === null) return j([...e, n]);
127
+ const s = e.find((r) => r.seq === n.replyToSeq);
128
+ return s ? Object.freeze(e.map((r) => r === s ? Object.freeze({ ...r, replies: j([...r.replies, n]) }) : r)) : e;
129
+ }
130
+ const ye = (e) => e.replyToSeq === null && e.replies.length > 0, V = (e, n) => e.toLowerCase() === n.toLowerCase(), Ie = 256, X = /* @__PURE__ */ new WeakMap();
131
+ function Oe() {
132
+ let e = _;
133
+ const n = /* @__PURE__ */ new Set(), s = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Set();
134
+ let i = 0, p = -1;
135
+ const b = () => {
136
+ i += 1;
137
+ for (const t of s.keys())
138
+ r.delete(t), r.add(t);
139
+ s.clear();
140
+ for (const t of r) {
141
+ if (r.size <= Ie) break;
142
+ r.delete(t);
143
+ }
144
+ }, g = (t) => {
145
+ if (t !== e) {
146
+ e = Object.freeze(t);
147
+ for (const o of [...n])
148
+ try {
149
+ o(e);
150
+ } catch {
151
+ }
152
+ }
153
+ };
154
+ function A(t, o = {}) {
155
+ if (!O(t)) return;
156
+ const a = u(t.articleId)?.toLowerCase() ?? null, c = e.articleId !== null && a !== null && a !== e.articleId, l = c ? _ : e;
157
+ c && b();
158
+ const y = typeof o.cursor == "number" && Number.isFinite(o.cursor) ? o.cursor : null, I = y ?? 0, q = typeof t.nextCursor == "number" && Number.isFinite(t.nextCursor) ? t.nextCursor : null, T = q ?? Number.POSITIVE_INFINITY, C = /* @__PURE__ */ new Map();
159
+ for (const f of Array.isArray(t.messages) ? t.messages : []) {
160
+ const d = L(f, !0);
161
+ d && d.seq > I && d.seq <= T && C.set(d.seq, d);
162
+ }
163
+ const m = /* @__PURE__ */ new Map();
164
+ for (const f of l.messages) {
165
+ const d = [f, ...f.replies];
166
+ for (const S of d) S.pending && S.pending !== "post" && S.clientMsgId && m.set(S.seq, S);
167
+ }
168
+ const x = (f) => {
169
+ const d = m.get(f.seq);
170
+ if (!d) return f;
171
+ const S = d.clientMsgId ? s.get(d.clientMsgId) : void 0;
172
+ return S && (S.before = f), d.replyToSeq === null ? Object.freeze({ ...d, replies: f.replies }) : d;
173
+ }, k = l.messages.filter((f) => f.seq < 0 || f.seq <= I || f.seq > T), P = /* @__PURE__ */ new Map();
174
+ for (const f of l.messages)
175
+ for (const d of f.replies)
176
+ d.seq < 0 && d.replyToSeq !== null && P.set(d.replyToSeq, [...P.get(d.replyToSeq) ?? [], d]);
177
+ const re = [...k.filter((f) => !C.has(f.seq)), ...C.values()].map((f) => {
178
+ if (!C.has(f.seq)) return f;
179
+ const d = x(f), S = d.replies.map(x), K = P.get(f.seq) ?? [];
180
+ return S.some((ie, le) => ie !== d.replies[le]) || K.length > 0 ? Object.freeze({ ...d, replies: j([...S, ...K]) }) : d;
181
+ }), E = l.loaded ? l.nextCursor : 0, oe = E === null ? null : I > E ? E : q === null ? null : Math.max(E, q);
182
+ g({
183
+ articleId: a ?? l.articleId,
184
+ conversationId: u(t.conversationId) ?? l.conversationId,
185
+ count: typeof t.count == "number" && t.count >= 0 ? t.count : l.count,
186
+ lastSeq: Math.max(typeof t.lastSeq == "number" ? t.lastSeq : 0, l.lastSeq),
187
+ lastChangeAt: ge(l.lastChangeAt, u(t.lastChangeAt)),
188
+ messages: j(re),
189
+ nextCursor: oe,
190
+ articleReactions: D(t.articleReactions),
191
+ reactionSet: Object.freeze(Array.isArray(t.reactionSet) ? t.reactionSet.filter((f) => typeof f == "string") : []),
192
+ preset: u(t.preset),
193
+ viewer: fe(t.viewer),
194
+ loaded: !0,
195
+ stale: y === null ? !1 : l.stale
196
+ });
197
+ }
198
+ function v(t) {
199
+ if (s.has(t.clientMsgId)) return;
200
+ const o = N(t.replyToSeq) ? t.replyToSeq : null;
201
+ if (o !== null && !e.messages.some((y) => y.seq === o)) return;
202
+ const a = Y(t.author) ?? U, c = Object.freeze({
203
+ conversationId: e.conversationId ?? "",
204
+ seq: p--,
205
+ id: `pending:${t.clientMsgId}`,
206
+ rev: 0,
207
+ replyToSeq: o,
208
+ author: a,
209
+ state: "visible",
210
+ body: t.body,
211
+ displayBody: t.body,
212
+ displayLanguageCode: t.languageCode ?? null,
213
+ sourceLanguageCode: t.languageCode ?? null,
214
+ isTranslated: !1,
215
+ isEdited: !1,
216
+ createdAt: t.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
217
+ editedAt: null,
218
+ collapsed: !1,
219
+ reactions: R,
220
+ translationVersionId: null,
221
+ replies: Object.freeze([]),
222
+ pending: "post",
223
+ clientMsgId: t.clientMsgId
224
+ });
225
+ s.set(t.clientMsgId, { type: "post", before: null });
226
+ const l = o === null ? j([...e.messages, c]) : Object.freeze(e.messages.map((y) => y.seq === o ? Object.freeze({ ...y, replies: j([...y.replies, c]) }) : y));
227
+ g({ ...e, messages: l });
228
+ }
229
+ function M(t) {
230
+ if (s.has(t.clientMsgId)) return;
231
+ const o = h(e.messages, (c) => c.seq === t.seq);
232
+ if (!o || o.pending) return;
233
+ s.set(t.clientMsgId, { type: t.type, before: o });
234
+ const a = t.type === "edit" ? Object.freeze({
235
+ ...o,
236
+ body: t.body,
237
+ displayBody: t.body,
238
+ displayLanguageCode: o.sourceLanguageCode,
239
+ isTranslated: !1,
240
+ isEdited: !0,
241
+ pending: "edit",
242
+ clientMsgId: t.clientMsgId
243
+ }) : Object.freeze({ ...o, pending: "delete", clientMsgId: t.clientMsgId });
244
+ g({ ...e, messages: z(e.messages, (c) => c === o ? a : c) });
245
+ }
246
+ const J = (t) => e.conversationId === null || V(t.conversationId, e.conversationId);
247
+ function Q(t) {
248
+ const o = s.get(t.clientMsgId);
249
+ if (!o && r.has(t.clientMsgId)) return;
250
+ s.delete(t.clientMsgId);
251
+ const a = L(t.message, t.message?.replyToSeq === null || t.message?.replyToSeq === void 0), c = a && J(a) ? a : null, l = z(
252
+ e.messages,
253
+ (m) => m.clientMsgId === t.clientMsgId && m.pending === "post" ? null : m.clientMsgId === t.clientMsgId ? B(m) : m
254
+ );
255
+ if (!c) {
256
+ l !== e.messages && g({ ...e, messages: l });
257
+ return;
258
+ }
259
+ const y = t.warnings ? G(t.warnings) : void 0, I = y && y.length > 0 ? Object.freeze({ ...c, warnings: y }) : c, q = h(l, (m) => m.seq === I.seq);
260
+ let T, C = e.count;
261
+ if (I.state === "deleted" || o?.type === "delete") {
262
+ const m = q ?? null, x = m?.state === "visible" && m.body !== null;
263
+ T = z(
264
+ l,
265
+ (k) => k.seq !== I.seq ? k : ye(k) ? Object.freeze({ ...I, replies: k.replies }) : null
266
+ ), x && (C = Math.max(0, C - 1));
267
+ } else
268
+ T = be(l, I), !q && I.state === "visible" && (C += 1);
269
+ g({
270
+ ...e,
271
+ // The answer to this store's own change names the new conversation; a row it never saw may be another article's.
272
+ conversationId: e.conversationId ?? (o ? I.conversationId : null),
273
+ lastSeq: Math.max(e.lastSeq, I.seq),
274
+ count: C,
275
+ messages: T
276
+ });
277
+ }
278
+ function Z(t) {
279
+ const o = s.get(t);
280
+ if (!o) return;
281
+ s.delete(t);
282
+ const a = z(e.messages, (c) => {
283
+ if (c.clientMsgId !== t) return c;
284
+ if (o.type === "post") return null;
285
+ const l = o.before;
286
+ return l ? c.replyToSeq === null ? Object.freeze({ ...l, replies: c.replies }) : l : B(c);
287
+ });
288
+ a !== e.messages && g({ ...e, messages: a });
289
+ }
290
+ function w(t) {
291
+ const o = L(t, t?.replyToSeq === null || t?.replyToSeq === void 0);
292
+ !o || !h(e.messages, (a) => a.seq === o.seq && !a.pending && V(a.conversationId, o.conversationId)) || g({ ...e, messages: W(e.messages, o) });
293
+ }
294
+ function ee(t) {
295
+ if (O(t))
296
+ switch (t.type) {
297
+ case "post":
298
+ return v(t);
299
+ case "edit":
300
+ case "delete":
301
+ return M(t);
302
+ case "confirm":
303
+ return Q(t);
304
+ case "rollback":
305
+ return Z(t.clientMsgId);
306
+ case "server":
307
+ return w(t.message);
308
+ }
309
+ }
310
+ function te(t) {
311
+ if (!O(t) || !e.loaded || e.articleId === null || u(t.articleId)?.toLowerCase() !== e.articleId) return !1;
312
+ if (e.stale) return !0;
313
+ const o = typeof t.lastSeq == "number" && t.lastSeq > e.lastSeq, a = typeof t.count == "number" && t.count !== e.count, c = u(t.lastChangeAt), l = c !== null && (e.lastChangeAt === null || Date.parse(c) > Date.parse(e.lastChangeAt));
314
+ return !o && !a && !l ? !1 : (g({ ...e, stale: !0 }), !0);
315
+ }
316
+ function ne(t, o) {
317
+ if (!O(t)) return;
318
+ const a = D(o);
319
+ if ("articleId" in t) {
320
+ if (u(t.articleId)?.toLowerCase() !== e.articleId) return;
321
+ g({ ...e, articleReactions: a });
322
+ return;
323
+ }
324
+ const { conversationId: c, seq: l } = t;
325
+ if (!N(l) || typeof c != "string") return;
326
+ const y = c.toLowerCase(), I = z(e.messages, (q) => q.seq === l && q.conversationId.toLowerCase() === y ? Object.freeze({ ...q, reactions: a }) : q);
327
+ I !== e.messages && g({ ...e, messages: I });
328
+ }
329
+ function se(t, o) {
330
+ if (typeof t != "string" || t === "") return;
331
+ const a = t.toLowerCase(), c = z(
332
+ e.messages,
333
+ (l) => l.author.kind === "member" && l.author.memberId !== null && l.author.memberId.toLowerCase() === a && l.body !== null && l.collapsed !== o ? Object.freeze({ ...l, collapsed: o }) : l
334
+ );
335
+ c !== e.messages && g({ ...e, messages: c });
336
+ }
337
+ const F = {
338
+ getState: () => e,
339
+ subscribe(t) {
340
+ return n.add(t), () => {
341
+ n.delete(t);
342
+ };
343
+ },
344
+ applyPage: A,
345
+ applyMessage: ee,
346
+ applyHead: te,
347
+ applyReaction: ne,
348
+ collapse: se,
349
+ reset() {
350
+ b(), g(_);
351
+ }
352
+ };
353
+ return X.set(F, () => i), F;
354
+ }
355
+ function B(e) {
356
+ const { pending: n, clientMsgId: s, ...r } = e;
357
+ return Object.freeze(r);
358
+ }
359
+ function ve(e) {
360
+ const n = "viewer" in e ? e.viewer.needs : e.needs, s = new Set(n.filter(H)), r = s.has("sign_in"), i = s.has("onboarding"), p = i || s.has("reconsent_rules"), b = s.has("art9_consent");
361
+ return Object.freeze({ signIn: r, onboarding: i, rules: p, art9: b, any: r || i || p || b });
362
+ }
363
+ function Ce(e, n) {
364
+ return h(e.messages, (s) => s.seq === n);
365
+ }
366
+ function ze(e, n) {
367
+ return n.author.kind === "member" && n.author.memberId !== null && n.author.memberId === e.viewer.memberId;
368
+ }
369
+ function je(e, n) {
370
+ if ("articleId" in n) return e.articleReactions;
371
+ const s = n.conversationId.toLowerCase();
372
+ return h(e.messages, (i) => i.seq === n.seq && i.conversationId.toLowerCase() === s)?.reactions ?? R;
373
+ }
374
+ function Ae(e, n) {
375
+ const s = { ...e.counts }, r = e.mine;
376
+ if (r !== null) {
377
+ const i = (s[r] ?? 0) - 1;
378
+ i > 0 ? s[r] = i : delete s[r];
379
+ }
380
+ return n !== null && (s[n] = (s[n] ?? 0) + 1), Object.freeze({ counts: Object.freeze(s), mine: n });
381
+ }
382
+ const me = 20;
383
+ function $(e) {
384
+ const n = X.get(e);
385
+ let s = n?.(), r = e.getState().articleId;
386
+ return {
387
+ moved: () => n !== void 0 && n() !== s || e.getState().articleId !== r,
388
+ mark() {
389
+ s = n?.(), r = e.getState().articleId;
390
+ }
391
+ };
392
+ }
393
+ const qe = (e) => typeof e == "number" && Number.isFinite(e) ? e : null;
394
+ async function Me(e, n, s, r) {
395
+ const i = e.getState(), p = i.loaded ? i.nextCursor : 0, b = $(e);
396
+ let g = null;
397
+ for (let A = 0; A < me; A++) {
398
+ const v = await n.discussions.list({ articleId: s.articleId, languageCode: s.languageCode ?? null, cursor: g }, r);
399
+ if (!v.ok) return v;
400
+ if (b.moved()) break;
401
+ e.applyPage(v.value, { cursor: g }), b.mark();
402
+ const M = qe(v.value.nextCursor);
403
+ if (M === null || p !== null && M >= p || g !== null && M <= g) break;
404
+ g = M;
405
+ }
406
+ return { ok: !0, value: e.getState() };
407
+ }
408
+ async function Te(e, n, s, r) {
409
+ const i = e.getState().nextCursor;
410
+ if (i === null) return { ok: !0, value: e.getState() };
411
+ const p = $(e), b = await n.discussions.list({ articleId: s.articleId, languageCode: s.languageCode ?? null, cursor: i }, r);
412
+ return b.ok ? (p.moved() || e.applyPage(b.value, { cursor: i }), { ok: !0, value: e.getState() }) : b;
413
+ }
414
+ export {
415
+ Oe as createDiscussionStore,
416
+ Ce as findMessage,
417
+ ze as isOwnMessage,
418
+ Te as loadMoreDiscussion,
419
+ Ae as reactionAfter,
420
+ je as reactionsFor,
421
+ Me as refreshDiscussion,
422
+ ve as viewerNeeds
423
+ };