import { Bi, Ct, Fe, Ht, Ot, Pt, Ra, SE, Subject, Ve, ar, kE, nt, nu, ot, rn } from "./chunk-23V3HWTR.js"; // node_modules/.pnpm/@univerjs+thread-comment@0.5.5_@grpc+grpc-js@1.13.4_react@18.3.1_rxjs@7.8.1/node_modules/@univerjs/thread-comment/lib/es/index.js var A = Object.defineProperty; var j = (s, t, e) => t in s ? A(s, t, { enumerable: true, configurable: true, writable: true, value: e }) : s[t] = e; var u = (s, t, e) => j(s, typeof t != "symbol" ? t + "" : t, e); var F = class extends nt { constructor() { super(); u(this, "_dataSource", null); u(this, "syncUpdateMutationToColla", true); } set dataSource(e) { this._dataSource = e; } get dataSource() { return this._dataSource; } async getThreadComment(e, n, o) { return this._dataSource ? (await this._dataSource.listComments(e, n, [o]))[0] : null; } async addComment(e) { var n; return this._dataSource ? this._dataSource.addComment(e) : { ...e, threadId: (n = e.threadId) != null ? n : e.id }; } async updateComment(e) { return this._dataSource ? this._dataSource.updateComment(e) : true; } async resolveComment(e) { return this._dataSource ? this._dataSource.resolveComment(e) : true; } async deleteComment(e, n, o, r) { return this._dataSource ? this._dataSource.deleteComment(e, n, o, r) : true; } async listThreadComments(e, n, o) { return this.dataSource ? this.dataSource.listComments(e, n, o) : false; } saveToSnapshot(e, n) { if (this._dataSource) { const o = {}; return Object.keys(e).forEach((r) => { const a = e[r]; o[r] = a.map(this.dataSource.saveCommentToSnapshot); }), o; } return e; } }; var p = Ct("univer.thread-comment.data-source-service"); var q = Object.defineProperty; var z = Object.getOwnPropertyDescriptor; var Q = (s, t, e, n) => { for (var o = n > 1 ? void 0 : n ? z(t, e) : t, r = s.length - 1, a; r >= 0; r--) (a = s[r]) && (o = (n ? a(t, e, o) : a(o)) || o); return n && o && q(t, e, o), o; }; var I = (s, t) => (e, n) => t(e, n, s); var i = class extends nt { constructor(t, e) { super(); u(this, "_commentsMap", /* @__PURE__ */ new Map()); u(this, "_threadMap", /* @__PURE__ */ new Map()); u(this, "_commentUpdate$", new Subject()); u(this, "commentUpdate$", this._commentUpdate$.asObservable()); u(this, "_tasks", []); this._dataSourceService = t, this._lifecycleService = e, this.disposeWithMe(() => { this._commentUpdate$.complete(); }), this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe((n) => { const o = /* @__PURE__ */ new Map(); n === Ve.Rendered && (this._tasks.forEach(({ unitId: r, subUnitId: a, threadIds: d }) => { let m = o.get(r); m || (m = /* @__PURE__ */ new Map(), o.set(r, m)); let c = m.get(a); c || (c = /* @__PURE__ */ new Set(), m.set(a, c)); for (const h of d) c.add(h); }), this._tasks = [], o.forEach((r, a) => { r.forEach((d, m) => { this.syncThreadComments(a, m, Array.from(d)); }); })); })); } _ensureCommentMap(t, e) { let n = this._commentsMap.get(t); n || (n = /* @__PURE__ */ new Map(), this._commentsMap.set(t, n)); let o = n.get(e); return o || (o = /* @__PURE__ */ new Map(), n.set(e, o)), o; } ensureMap(t, e) { return this._ensureCommentMap(t, e); } _ensureThreadMap(t, e) { let n = this._threadMap.get(t); n || (n = /* @__PURE__ */ new Map(), this._threadMap.set(t, n)); let o = n.get(e); return o || (o = /* @__PURE__ */ new Map(), n.set(e, o)), o; } _replaceComment(t, e, n) { const o = this._ensureCommentMap(t, e), r = o.get(n.id); if (r) { const { children: a, ...d } = n, m = { ...d, ref: r.ref }; o.set(n.id, m), a == null || a.forEach((c) => { o.set(c.id, { ...c, ref: "" }); }), this._commentUpdate$.next({ unitId: t, subUnitId: e, type: "syncUpdate", payload: m }), !!n.resolved != !!r.resolved && this._commentUpdate$.next({ unitId: t, subUnitId: e, type: "resolve", payload: { commentId: n.id, resolved: !!n.resolved } }); } } async syncThreadComments(t, e, n) { if (this._lifecycleService.stage < Ve.Rendered) { this._tasks.push({ unitId: t, subUnitId: e, threadIds: n }); return; } const o = this._ensureThreadMap(t, e), r = this._ensureCommentMap(t, e), a = await this._dataSourceService.listThreadComments(t, e, n); if (!a) return; const d = new Set(n); a.forEach((m) => { this._replaceComment(t, e, m), d.delete(m.threadId); }), d.forEach((m) => { o.delete(m), r.forEach((c, h) => { c.threadId === m && r.delete(h); }); }); } addComment(t, e, n, o) { const r = this._ensureCommentMap(t, e), { parentId: a, children: d = [], ...m } = n, c = { ...m, parentId: a === n.id ? void 0 : a }; c.threadId || (c.threadId = c.parentId || c.id); const h = (f) => { r.set(f.id, f), this._commentUpdate$.next({ unitId: t, subUnitId: e, type: "add", payload: f, isRoot: !f.parentId }); }; h(c); const _ = this._ensureThreadMap(t, e); if (!c.parentId) { _.set(c.threadId, c); for (const f of d) h(f); } return o && this.syncThreadComments(t, e, [c.threadId]), true; } updateComment(t, e, n, o) { const a = this._ensureCommentMap(t, e).get(n.commentId); return a && (a.updated = true, a.text = n.text, a.attachments = n.attachments, a.updateT = n.updateT, this._commentUpdate$.next({ unitId: t, subUnitId: e, type: "update", payload: n, silent: o })), true; } updateCommentRef(t, e, n, o) { const a = this._ensureCommentMap(t, e).get(n.commentId); return a ? (a.ref = n.ref, this._commentUpdate$.next({ unitId: t, subUnitId: e, type: "updateRef", payload: n, silent: o, threadId: a.threadId }), true) : false; } resolveComment(t, e, n, o) { const a = this._ensureCommentMap(t, e).get(n); return a ? (a.resolved = o, this._commentUpdate$.next({ unitId: t, subUnitId: e, type: "resolve", payload: { commentId: n, resolved: o } }), true) : false; } getComment(t, e, n) { return this._ensureCommentMap(t, e).get(n); } getRootComment(t, e, n) { return this._ensureThreadMap(t, e).get(n); } getThread(t, e, n) { const o = this._ensureCommentMap(t, e), r = Array.from(o.values()).filter((c) => c.threadId === n); let a; const d = [], m = /* @__PURE__ */ new Set(); for (const c of r) c.parentId ? d.push(c) : a = c, m.add(c.personId); if (a) return { root: a, children: d, relativeUsers: m, unitId: t, subUnitId: e, threadId: n }; } getCommentWithChildren(t, e, n) { const o = this.getComment(t, e, n); if (o) return this.getThread(t, e, o.threadId); } _deleteComment(t, e, n) { const o = this._ensureCommentMap(t, e), r = o.get(n); r && (o.delete(n), this._commentUpdate$.next({ unitId: t, subUnitId: e, type: "delete", payload: { commentId: n, isRoot: !r.parentId, comment: r } })); } deleteThread(t, e, n) { this._ensureThreadMap(t, e).delete(n), this._ensureCommentMap(t, e).forEach((a) => { a.threadId === n && this._deleteComment(t, e, a.id); }); } deleteComment(t, e, n) { const r = this._ensureCommentMap(t, e).get(n); return r && (r.parentId ? this._deleteComment(t, e, n) : this.deleteThread(t, e, r.threadId)), true; } deleteUnit(t) { const e = this._commentsMap.get(t); e && e.forEach((n, o) => { n.forEach((r) => { this.deleteComment(t, o, r.id); }); }); } getUnit(t) { const e = this._threadMap.get(t); if (!e) return []; const n = []; return e.forEach((o, r) => { o.forEach((a, d) => { const m = this.getThread(t, r, d); m && n.push(m); }); }), n; } getAll() { const t = []; return this._commentsMap.forEach((e, n) => { t.push({ unitId: n, threads: this.getUnit(n) }); }), t; } }; i = Q([ I(0, ot(p)), I(1, ot(Ht)) ], i); var T = ((s) => (s[s.UNIVER_UNKNOWN = 0] = "UNIVER_UNKNOWN", s[s.UNIVER_DOC = 1] = "UNIVER_DOC", s[s.UNIVER_SHEET = 2] = "UNIVER_SHEET", s[s.UNIVER_SLIDE = 3] = "UNIVER_SLIDE", s[s.UNIVER_PROJECT = 4] = "UNIVER_PROJECT", s[s.UNRECOGNIZED = -1] = "UNRECOGNIZED", s))(T || {}); var w = "UNIVER_THREAD_COMMENT_PLUGIN"; var X = Object.defineProperty; var k = Object.getOwnPropertyDescriptor; var ee = (s, t, e, n) => { for (var o = n > 1 ? void 0 : n ? k(t, e) : t, r = s.length - 1, a; r >= 0; r--) (a = s[r]) && (o = (n ? a(t, e, o) : a(o)) || o); return n && o && X(t, e, o), o; }; var v = (s, t) => (e, n) => t(e, n, s); var S = class extends nt { constructor(s, t, e) { super(), this._resourceManagerService = s, this._threadCommentModel = t, this._threadCommentDataSourceService = e, this._initSnapshot(); } _initSnapshot() { const s = (e) => { const n = this._threadCommentModel.getUnit(e), o = {}; return n ? (n.forEach((r) => { var d; const a = (d = o[r.subUnitId]) != null ? d : []; a.push({ ...r.root, children: r.children }), o[r.subUnitId] = a; }), JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o, e))) : ""; }, t = (e) => { if (!e) return {}; try { return JSON.parse(e); } catch { return {}; } }; this.disposeWithMe( this._resourceManagerService.registerPluginResource({ pluginName: `SHEET_${w}`, businesses: [T.UNIVER_SHEET, T.UNIVER_DOC], toJson: (e) => s(e), parseJson: (e) => t(e), onUnLoad: (e) => { this._threadCommentModel.deleteUnit(e); }, onLoad: async (e, n) => { Object.keys(n).forEach((o) => { const r = n[o]; r.forEach((a) => { this._threadCommentModel.addComment(e, o, a); }), this._threadCommentModel.syncThreadComments(e, o, r.map((a) => a.threadId)); }); } }) ); } }; S = ee([ v(0, Bi), v(1, ot(i)), v(2, p) ], S); var P = { id: "thread-comment.mutation.add-comment", type: ar.MUTATION, handler(s, t, e) { if (!t) return false; const n = s.get(i), { unitId: o, subUnitId: r, comment: a, sync: d } = t, m = d || (e == null ? void 0 : e.fromChangeset) && !a.parentId; return n.addComment(o, r, a, m); } }; var $ = { id: "thread-comment.mutation.update-comment", type: ar.MUTATION, handler(s, t) { if (!t) return false; const e = s.get(i), { unitId: n, subUnitId: o, payload: r, silent: a } = t; return e.updateComment(n, o, r, a); } }; var te = { id: "thread-comment.mutation.update-comment-ref", type: ar.MUTATION, handler(s, t) { if (!t) return false; const e = s.get(i), { unitId: n, subUnitId: o, payload: r, silent: a } = t; return e.updateCommentRef(n, o, r, a); } }; var x = { id: "thread-comment.mutation.resolve-comment", type: ar.MUTATION, handler(s, t) { if (!t) return false; const e = s.get(i), { unitId: n, subUnitId: o, resolved: r, commentId: a } = t; return e.resolveComment(n, o, a, r); } }; var N = { id: "thread-comment.mutation.delete-comment", type: ar.MUTATION, handler(s, t) { if (!t) return false; const e = s.get(i), { unitId: n, subUnitId: o, commentId: r } = t; return e.deleteComment(n, o, r); } }; var ne = { id: "thread-comment.command.add-comment", type: ar.COMMAND, async handler(s, t) { if (!t) return false; const e = s.get(Pt), n = s.get(p), { comment: o } = t, r = await n.addComment(o), a = n.syncUpdateMutationToColla, d = !o.parentId, m = { id: P.id, params: { ...t, comment: r } }; return d ? await e.executeCommand(m.id, m.params) : e.executeCommand(m.id, m.params, { onlyLocal: !a }); } }; var oe = { id: "thread-comment.command.update-comment", type: ar.COMMAND, async handler(s, t) { if (!t) return false; const { unitId: e, subUnitId: n, payload: o } = t, r = s.get(Pt), a = s.get(i), d = s.get(p), m = d.syncUpdateMutationToColla, c = a.getComment( e, n, o.commentId ); if (!c) return false; const { children: h, ..._ } = c; if (!await d.updateComment({ ..._, ...o })) return false; const E = { id: $.id, params: t }; return r.executeCommand(E.id, E.params, { onlyLocal: !m }), true; } }; var re = { id: "thread-comment.command.resolve-comment", type: ar.COMMAND, async handler(s, t) { if (!t) return false; const { unitId: e, subUnitId: n, resolved: o, commentId: r } = t, a = s.get(p), m = s.get(i).getComment(e, n, r), c = a.syncUpdateMutationToColla; return !m || !await a.resolveComment({ ...m, resolved: o }) ? false : s.get(Pt).executeCommand( x.id, t, { onlyLocal: !c } ); } }; var ae = { id: "thread-comment.command.delete-comment", type: ar.COMMAND, async handler(s, t) { if (!t) return false; const e = s.get(i), n = s.get(p), o = s.get(Pt), { unitId: r, subUnitId: a, commentId: d } = t, m = n.syncUpdateMutationToColla, c = e.getComment(r, a, d); if (!c || !await n.deleteComment(r, a, c.threadId, d)) return false; const h = { id: N.id, params: t }; return o.executeCommand(h.id, h.params, { onlyLocal: !m }); } }; var se = { id: "thread-comment.command.delete-comment-tree", type: ar.COMMAND, async handler(s, t) { if (!t) return false; const e = s.get(i), n = s.get(Pt), o = s.get(p), { unitId: r, subUnitId: a, commentId: d } = t, m = e.getCommentWithChildren(r, a, d); return !m || !await o.deleteComment(r, a, m.root.threadId, d) ? false : await n.executeCommand(N.id, { unitId: r, subUnitId: a, commentId: m.root.id }); } }; var me = "thread-comment.config"; var R = {}; var ce = Object.defineProperty; var de = Object.getOwnPropertyDescriptor; var ie = (s, t, e, n) => { for (var o = n > 1 ? void 0 : n ? de(t, e) : t, r = s.length - 1, a; r >= 0; r--) (a = s[r]) && (o = (n ? a(t, e, o) : a(o)) || o); return n && o && ce(t, e, o), o; }; var y = (s, t) => (e, n) => t(e, n, s); var M; var D = (M = class extends Ra { constructor(s = R, t, e, n) { super(), this._config = s, this._injector = t, this._commandService = e, this._configService = n; const { ...o } = nu( {}, R, this._config ); this._configService.setConfig(me, o); } onStarting() { var s; kE([ [p, { useClass: F }], [i], [S] ], (s = this._config) == null ? void 0 : s.overrides).forEach( (t) => { this._injector.add(t); } ), [ ne, oe, ae, re, se, P, $, te, N, x ].forEach((t) => { this._commandService.registerCommand(t); }), this._injector.get(S); } }, u(M, "pluginName", w), u(M, "type", Fe.UNIVER_UNKNOWN), M); D = ie([ y(1, ot(Ot)), y(2, Pt), y(3, SE) ], D); function pe(s) { return rn(s).format("YYYY/MM/DD HH:mm"); } export { F, p, i, w, S, P, $, te, x, N, ne, oe, re, ae, se, D, pe }; //# sourceMappingURL=chunk-PG4GXCOC.js.map