@meltingspot/widget 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2023 MeltingSpot
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its contributors
16
+ may be used to endorse or promote products derived from this software
17
+ without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Widget SDK
2
+
3
+ Embed a MeltingSpot Widget into your own web application — inline, as a modal, or as a drawer. Activate [SSO](https://help.meltingspot.io/manage-spot/sso) on your spot before using authenticated widgets.
4
+
5
+ ## Installation
6
+
7
+ ### Via `<script>` (CDN)
8
+
9
+ The standalone bundle is self-contained — it exposes `window.MeltingSpot.Widget`
10
+ and injects its styles automatically (no separate stylesheet to load).
11
+
12
+ ```html
13
+ <script
14
+ crossorigin
15
+ async
16
+ src="https://cdn.jsdelivr.net/npm/@meltingspot/widget"
17
+ ></script>
18
+ ```
19
+
20
+ ### Via npm
21
+
22
+ ```bash
23
+ npm install @meltingspot/widget
24
+ ```
25
+
26
+ ```typescript
27
+ import { Widget } from '@meltingspot/widget';
28
+ import '@meltingspot/widget/stylesheet.css';
29
+ ```
30
+
31
+ > The npm package declares `@meltingspot/core` and `@meltingspot/embed` as
32
+ > dependencies so your bundler resolves a single shared instance of each.
33
+
34
+ ## Usage
35
+
36
+ ### Inline
37
+
38
+ ```html
39
+ <div id="meltingspot-widget" style="width: 100%; height: 100%"></div>
40
+ <script>
41
+ window.addEventListener('DOMContentLoaded', function () {
42
+ window.MeltingSpot.Widget.injectWidgetInto('meltingspot-widget', {
43
+ spotId: 'YOUR_SPOT_ID',
44
+ widgetId: 'YOUR_WIDGET_ID',
45
+ authToken: 'CURRENT_USER_SSO_TOKEN', // optional: omit for public content
46
+ themeMode: 'auto',
47
+ });
48
+ });
49
+ </script>
50
+ ```
51
+
52
+ ### Modal
53
+
54
+ ```html
55
+ <button id="open-widget-modal" type="button"></button>
56
+ <script>
57
+ window.MeltingSpot.Widget.bindWidgetModalTo('open-widget-modal', {
58
+ spotId: 'YOUR_SPOT_ID',
59
+ widgetId: 'YOUR_WIDGET_ID',
60
+ authToken: 'OPTIONAL_SSO_TOKEN',
61
+ themeMode: 'auto',
62
+ });
63
+ </script>
64
+ ```
65
+
66
+ ### Drawer
67
+
68
+ ```html
69
+ <button id="open-widget-drawer" type="button"></button>
70
+ <script>
71
+ window.MeltingSpot.Widget.bindWidgetDrawerTo('open-widget-drawer', {
72
+ position: 'right', // or 'left'
73
+ spotId: 'YOUR_SPOT_ID',
74
+ widgetId: 'YOUR_WIDGET_ID',
75
+ authToken: 'OPTIONAL_SSO_TOKEN',
76
+ themeMode: 'auto',
77
+ });
78
+ </script>
79
+ ```
80
+
81
+ See the [Widget helpdesk](https://help.meltingspot.io) for the full integration guide.
82
+
83
+ ```
84
+
85
+ ```
package/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@meltingspot/core"),p=require("@meltingspot/embed"),C="0.1.0",d=new i.Logger({appName:"Widget SDK",appVersion:C});class b{constructor(t,e,o){this.content=t,this.rootDocument=e,this.dialogClassname=o,this.dialogElement=null,this.triggerElement=null,this.iframeElement=null,this.currentListener=null}inject({into:t,bind:e,options:o}){this.remove();const r=i.getHTMLElement({input:t,rootDocument:this.rootDocument});if(this.dialogElement=i.createDialog(this.rootDocument),this.dialogElement.classList.add(this.dialogClassname),this.dialogElement.onclose=()=>this.#e(),r.appendChild(this.dialogElement),e){this.triggerElement=i.getHTMLElement({input:e,rootDocument:this.rootDocument});const c=this.open.bind(this,o);this.currentListener=a=>{a.stopPropagation(),a.preventDefault(),c()},this.triggerElement.addEventListener("click",this.currentListener)}}remove(){this.dialogElement&&(this.dialogElement.remove(),this.dialogElement.onclose=null,this.dialogElement=null),this.triggerElement&&(this.currentListener&&this.triggerElement.removeEventListener("click",this.currentListener),this.triggerElement=null,this.currentListener=null)}open(t){this.dialogElement||this.inject({into:this.rootDocument.body}),this.dialogElement&&(this.#t(),this.dialogElement.showModal())}close(){this.dialogElement&&this.dialogElement.close()}#t(){this.dialogElement&&(this.iframeElement=this.dialogElement.appendChild(this.content.iframe))}#e(){this.iframeElement&&(this.iframeElement.remove(),this.iframeElement=null)}}class w extends b{constructor(t,e){super(t,e,"ms-drawer")}open(t){const{position:e="right"}=t??{};super.open(t),this.dialogElement&&(this.dialogElement.dataset.position=e)}}class I extends b{constructor(t,e){super(t,e,"ms-modal")}}class U{constructor(t){this.baseURL=i.formatBaseUrl(t.baseURL),this.spotId=i.formatUUID(t.spotId),this.widgetId=i.formatUUID(t.widgetId),this.authToken=t.authToken??null,this.themeMode=t.themeMode??"auto",this.domain=t.domain??null,this.utmParameters=t.utmParameters??null,this.rootDocument=t.rootDocument}get iframe(){const t=new URL(`/widgets/spot/${this.spotId}/widget/${this.widgetId}`,i.trimSlashRight(this.baseURL)),e=i.createIframe({url:t,searchParams:{themeMode:this.themeMode,ms_token:this.authToken,domain:this.domain,...i.flattenUtmParameters(this.utmParameters)},rootDocument:this.rootDocument});return e.classList.add("ms-widget"),e}}class L{constructor(t){this.baseURL=i.formatBaseUrl(t.baseURL),this.spotId=i.formatUUID(t.spotId),this.widgetContentId=i.formatUUID(t.widgetContentId),this.contentId=i.formatUUID(t.contentId),this.widgetId=i.formatUUID(t.widgetId),this.contentType=t.contentType,this.authToken=t.authToken??null,this.themeMode=t.themeMode??"auto",this.domain=t.domain??null,this.utmParameters=t.utmParameters??null,this.rootDocument=t.rootDocument}get iframe(){const t=new URL(`/widgets/spot/${this.spotId}/widgetContent/${this.widgetContentId}`,i.trimSlashRight(this.baseURL));t.searchParams.set("widgetId",this.widgetId);const e=i.createIframe({url:t,searchParams:{themeMode:this.themeMode,ms_token:this.authToken,domain:this.domain,contentId:this.contentId,contentType:this.contentType,...i.flattenUtmParameters(this.utmParameters)},rootDocument:this.rootDocument});return e.classList.add("ms-widget-content"),e}}const n=class n{static injectWidgetInto(t,e){const{spotId:o,rootDocument:r,msCommunityWebBaseUrl:c}=i.ConfigurationService.getConfiguration({spotId:e.spotId,rootDocument:e.rootDocument,msCommunityWebBaseUrl:e.baseURL});if(!o)throw d.error("Spot ID is required"),new Error("Spot ID is not configured. Please set the spotId in the configuration.");const{widget:a,args:m}=n.createWidget({...e,spotId:o,rootDocument:r,baseURL:c}),s=new p.EmbedFrame(a);return s.inject(t),n.widgets.push(s),d.log("Widget injected",n.cleanupLoggedArgs(m),t),s}static injectWidgetContentInto(t,e){const{spotId:o,rootDocument:r,msCommunityWebBaseUrl:c}=i.ConfigurationService.getConfiguration({spotId:e.spotId,rootDocument:e.rootDocument,msCommunityWebBaseUrl:e.baseURL});if(!o)throw d.error("Spot ID is required"),new Error("Spot ID is not configured. Please set the spotId in the configuration.");const{widgetContent:a,args:m}=n.createWidgetContent({...e,spotId:o,rootDocument:r,baseURL:c}),s=new p.EmbedFrame(a);return s.inject(t),n.widgets.push(s),d.log("WidgetContent injected",n.cleanupLoggedArgs(m),t),s}static bindWidgetModalTo(t,e){return n.bindEntityModalTo(t,{type:"Widget",data:e})}static bindWidgetDrawerTo(t,e){return n.bindEntityDrawerTo(t,{type:"Widget",data:e})}static createWidgetDrawer(t){const{widget:e}=n.createWidget(t);return d.log("Create Widget drawer",{data:n.cleanupLoggedArgs(t)}),new w(e,t.rootDocument??document)}static createWidgetModal(t){const{widget:e}=n.createWidget(t);return d.log("Create Widget modal",{data:n.cleanupLoggedArgs(t)}),new I(e,t.rootDocument??document)}static createWidgetEmbed(t){const{widget:e}=n.createWidget(t),o=new p.EmbedFrame(e);return d.log("Create Widget embed",{data:n.cleanupLoggedArgs(t)}),o}static clear(){n.widgets.forEach(t=>{t.remove()}),n.widgets=[],n.widgetOptions={},d.log("All Widget SDK MeltingSpot elements were cleared")}static bindEntityDrawerTo(t,e){const{spotId:o,rootDocument:r,msCommunityWebBaseUrl:c}=i.ConfigurationService.getConfiguration({spotId:e.data.spotId,rootDocument:e.data.rootDocument??document,msCommunityWebBaseUrl:e.data.baseURL});if(!o)throw d.error("Spot ID is required"),new Error("Spot ID is not configured. Please set the spotId in the configuration.");let a,m;switch(e.type){case"Widget":{const u=n.createWidget({...e.data,spotId:o,rootDocument:r,baseURL:c});a=u.widget,m=u.args}break;case"WidgetContent":{const u=n.createWidgetContent({...e.data,spotId:o,rootDocument:r,baseURL:c});a=u.widgetContent,m=u.args}}const s=new w(a,r),l={position:e.data.position},g=e.data.into??r.body;return s.inject({into:g,bind:t,options:l}),n.widgets.push(s),d.log("Widget bound to drawer",n.cleanupLoggedArgs(m),l,g,t),s}static bindEntityModalTo(t,e){const{spotId:o,rootDocument:r,msCommunityWebBaseUrl:c}=i.ConfigurationService.getConfiguration({spotId:e.data.spotId,rootDocument:e.data.rootDocument??document,msCommunityWebBaseUrl:e.data.baseURL});if(!o)throw d.error("Spot ID is required"),new Error("Spot ID is not configured. Please set the spotId in the configuration.");let a,m;switch(e.type){case"Widget":{const g=n.createWidget({...e.data,spotId:o,rootDocument:r,baseURL:c});a=g.widget,m=g.args}break;case"WidgetContent":{const g=n.createWidgetContent({...e.data,spotId:o,rootDocument:r,baseURL:c});a=g.widgetContent,m=g.args}}const s=new I(a,r),l=e.data.into??r.body;return s.inject({into:l,bind:t}),n.widgets.push(s),d.log("Widget bound to modal",n.cleanupLoggedArgs(m),l,t),s}static handleMessageEvent(t){if(!t.data||typeof t.data!="object"||t.data.type!=="OpenWidgetContent")return;const e=t.data?.widgetId;if(!e||typeof e!="string"){d.error("widgetId is required in the message event data");return}const o=n.widgetOptions[e];if(!o){d.error("No widget options found for widgetId",{widgetId:e});return}let r=null;try{r=new URL(o.baseURL).origin}catch{r=null}if(!r||t.origin!==r){d.warn("Ignoring OpenWidgetContent from unexpected origin",{origin:t.origin,expectedOrigin:r});return}const{spotId:c,rootDocument:a,msCommunityWebBaseUrl:m}=i.ConfigurationService.getConfiguration({spotId:o.spotId,rootDocument:o.rootDocument??document,msCommunityWebBaseUrl:o.baseURL}),{widgetContent:s}=n.createWidgetContent({...o,widgetContentId:t.data.widgetContentId,contentType:t.data.contentType,contentId:t.data.contentId,widgetId:e,spotId:c,rootDocument:a,baseURL:m});t.data.openingIn==="Drawer"?new w(s,a).open():t.data.openingIn==="Modal"&&new I(s,a).open()}static createWidget(t){const e=new URLSearchParams(window.location.search),o={spotId:t.spotId,widgetId:t.widgetId,authToken:t.authToken,themeMode:t.themeMode,domain:t.domain,rootDocument:t.rootDocument,baseURL:t.baseURL,utmParameters:t.utmParameters??i.parseUtmParameters(e)};return n.widgetOptions[o.widgetId]=o,n.messageListened||(window.addEventListener("message",n.handleMessageEvent),n.messageListened=!0),{widget:new U(o),args:o}}static createWidgetContent(t){const e=new URLSearchParams(window.location.search),o={spotId:t.spotId,contentId:t.contentId,contentType:t.contentType,widgetContentId:t.widgetContentId,widgetId:t.widgetId,authToken:t.authToken,themeMode:t.themeMode,domain:t.domain,rootDocument:t.rootDocument,baseURL:t.baseURL,utmParameters:t.utmParameters??i.parseUtmParameters(e)};return{widgetContent:new L(o),args:o}}static cleanupLoggedArgs(t){const{authToken:e,...o}=t;return{...o,authToken:e?"***":void 0}}};n.widgets=[],n.messageListened=!1,n.widgetOptions={};let f=n;const D=globalThis;D.MeltingSpot||(D.MeltingSpot={});D.MeltingSpot.Widget=f;exports.Drawer=w;exports.Modal=I;exports.Widget=f;
package/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { MeltingSpotWidget } from './meltingSpotWidget';
2
+ export type { BindModalToOptions, BindDrawerOptions, InjectWidgetIntoOptions, InjectWidgetContentIntoOptions, BindWidgetDrawerToOptions, BindWidgetContentDrawerToOptions, BindWidgetModalToOptions, BindWidgetContentModalToOptions, } from './meltingSpotWidget';
3
+ export { MeltingSpotWidget as Widget } from './meltingSpotWidget';
4
+ export { Drawer } from './rendering/drawer';
5
+ export { Modal } from './rendering/modal';
6
+ declare global {
7
+ interface MeltingSpotNamespace {
8
+ Widget: typeof MeltingSpotWidget;
9
+ }
10
+ }
package/index.mjs ADDED
@@ -0,0 +1,383 @@
1
+ import { Logger as T, getHTMLElement as L, createDialog as P, formatBaseUrl as W, formatUUID as l, trimSlashRight as E, createIframe as U, flattenUtmParameters as y, ConfigurationService as h, parseUtmParameters as C } from "@meltingspot/core";
2
+ import { EmbedFrame as I } from "@meltingspot/embed";
3
+ const M = "0.1.0", r = new T({
4
+ appName: "Widget SDK",
5
+ appVersion: M
6
+ });
7
+ class R {
8
+ constructor(t, e, o) {
9
+ this.content = t, this.rootDocument = e, this.dialogClassname = o, this.dialogElement = null, this.triggerElement = null, this.iframeElement = null, this.currentListener = null;
10
+ }
11
+ inject({ into: t, bind: e, options: o }) {
12
+ this.remove();
13
+ const i = L({
14
+ input: t,
15
+ rootDocument: this.rootDocument
16
+ });
17
+ if (this.dialogElement = P(this.rootDocument), this.dialogElement.classList.add(this.dialogClassname), this.dialogElement.onclose = () => this.#e(), i.appendChild(this.dialogElement), e) {
18
+ this.triggerElement = L({
19
+ input: e,
20
+ rootDocument: this.rootDocument
21
+ });
22
+ const d = this.open.bind(this, o);
23
+ this.currentListener = (s) => {
24
+ s.stopPropagation(), s.preventDefault(), d();
25
+ }, this.triggerElement.addEventListener("click", this.currentListener);
26
+ }
27
+ }
28
+ remove() {
29
+ this.dialogElement && (this.dialogElement.remove(), this.dialogElement.onclose = null, this.dialogElement = null), this.triggerElement && (this.currentListener && this.triggerElement.removeEventListener("click", this.currentListener), this.triggerElement = null, this.currentListener = null);
30
+ }
31
+ open(t) {
32
+ this.dialogElement || this.inject({ into: this.rootDocument.body }), this.dialogElement && (this.#t(), this.dialogElement.showModal());
33
+ }
34
+ close() {
35
+ this.dialogElement && this.dialogElement.close();
36
+ }
37
+ #t() {
38
+ this.dialogElement && (this.iframeElement = this.dialogElement.appendChild(this.content.iframe));
39
+ }
40
+ #e() {
41
+ this.iframeElement && (this.iframeElement.remove(), this.iframeElement = null);
42
+ }
43
+ }
44
+ class p extends R {
45
+ constructor(t, e) {
46
+ super(t, e, "ms-drawer");
47
+ }
48
+ open(t) {
49
+ const { position: e = "right" } = t ?? {};
50
+ super.open(t), this.dialogElement && (this.dialogElement.dataset.position = e);
51
+ }
52
+ }
53
+ class f extends R {
54
+ constructor(t, e) {
55
+ super(t, e, "ms-modal");
56
+ }
57
+ }
58
+ class k {
59
+ constructor(t) {
60
+ this.baseURL = W(t.baseURL), this.spotId = l(t.spotId), this.widgetId = l(t.widgetId), this.authToken = t.authToken ?? null, this.themeMode = t.themeMode ?? "auto", this.domain = t.domain ?? null, this.utmParameters = t.utmParameters ?? null, this.rootDocument = t.rootDocument;
61
+ }
62
+ get iframe() {
63
+ const t = new URL(
64
+ `/widgets/spot/${this.spotId}/widget/${this.widgetId}`,
65
+ E(this.baseURL)
66
+ ), e = U({
67
+ url: t,
68
+ searchParams: {
69
+ themeMode: this.themeMode,
70
+ ms_token: this.authToken,
71
+ domain: this.domain,
72
+ ...y(this.utmParameters)
73
+ },
74
+ rootDocument: this.rootDocument
75
+ });
76
+ return e.classList.add("ms-widget"), e;
77
+ }
78
+ }
79
+ class S {
80
+ constructor(t) {
81
+ this.baseURL = W(t.baseURL), this.spotId = l(t.spotId), this.widgetContentId = l(t.widgetContentId), this.contentId = l(t.contentId), this.widgetId = l(t.widgetId), this.contentType = t.contentType, this.authToken = t.authToken ?? null, this.themeMode = t.themeMode ?? "auto", this.domain = t.domain ?? null, this.utmParameters = t.utmParameters ?? null, this.rootDocument = t.rootDocument;
82
+ }
83
+ get iframe() {
84
+ const t = new URL(
85
+ `/widgets/spot/${this.spotId}/widgetContent/${this.widgetContentId}`,
86
+ E(this.baseURL)
87
+ );
88
+ t.searchParams.set("widgetId", this.widgetId);
89
+ const e = U({
90
+ url: t,
91
+ searchParams: {
92
+ themeMode: this.themeMode,
93
+ ms_token: this.authToken,
94
+ domain: this.domain,
95
+ contentId: this.contentId,
96
+ contentType: this.contentType,
97
+ ...y(this.utmParameters)
98
+ },
99
+ rootDocument: this.rootDocument
100
+ });
101
+ return e.classList.add("ms-widget-content"), e;
102
+ }
103
+ }
104
+ const n = class n {
105
+ static injectWidgetInto(t, e) {
106
+ const { spotId: o, rootDocument: i, msCommunityWebBaseUrl: d } = h.getConfiguration({
107
+ spotId: e.spotId,
108
+ rootDocument: e.rootDocument,
109
+ msCommunityWebBaseUrl: e.baseURL
110
+ });
111
+ if (!o)
112
+ throw r.error("Spot ID is required"), new Error(
113
+ "Spot ID is not configured. Please set the spotId in the configuration."
114
+ );
115
+ const { widget: s, args: c } = n.createWidget({
116
+ ...e,
117
+ spotId: o,
118
+ rootDocument: i,
119
+ baseURL: d
120
+ }), a = new I(s);
121
+ return a.inject(t), n.widgets.push(a), r.log(
122
+ "Widget injected",
123
+ n.cleanupLoggedArgs(c),
124
+ t
125
+ ), a;
126
+ }
127
+ static injectWidgetContentInto(t, e) {
128
+ const { spotId: o, rootDocument: i, msCommunityWebBaseUrl: d } = h.getConfiguration({
129
+ spotId: e.spotId,
130
+ rootDocument: e.rootDocument,
131
+ msCommunityWebBaseUrl: e.baseURL
132
+ });
133
+ if (!o)
134
+ throw r.error("Spot ID is required"), new Error(
135
+ "Spot ID is not configured. Please set the spotId in the configuration."
136
+ );
137
+ const { widgetContent: s, args: c } = n.createWidgetContent({
138
+ ...e,
139
+ spotId: o,
140
+ rootDocument: i,
141
+ baseURL: d
142
+ }), a = new I(s);
143
+ return a.inject(t), n.widgets.push(a), r.log(
144
+ "WidgetContent injected",
145
+ n.cleanupLoggedArgs(c),
146
+ t
147
+ ), a;
148
+ }
149
+ static bindWidgetModalTo(t, e) {
150
+ return n.bindEntityModalTo(t, {
151
+ type: "Widget",
152
+ data: e
153
+ });
154
+ }
155
+ static bindWidgetDrawerTo(t, e) {
156
+ return n.bindEntityDrawerTo(t, {
157
+ type: "Widget",
158
+ data: e
159
+ });
160
+ }
161
+ /**
162
+ * Create a drawer that will contain a widget.
163
+ * By default, the drawer is positioned on the right side of the screen.
164
+ * @param options {WidgetArgs} Widget creation args
165
+ * @returns A drawer, that can be opened and closed programmatically, and that will contain the widget.
166
+ */
167
+ static createWidgetDrawer(t) {
168
+ const { widget: e } = n.createWidget(t);
169
+ return r.log("Create Widget drawer", {
170
+ data: n.cleanupLoggedArgs(t)
171
+ }), new p(e, t.rootDocument ?? document);
172
+ }
173
+ /**
174
+ * Create a modal that will contain a widget.
175
+ * @param options {WidgetArgs} Widget creation args
176
+ * @returns A modal, that can be opened and closed programmatically, and that will contain the widget.
177
+ */
178
+ static createWidgetModal(t) {
179
+ const { widget: e } = n.createWidget(t);
180
+ return r.log("Create Widget modal", {
181
+ data: n.cleanupLoggedArgs(t)
182
+ }), new f(e, t.rootDocument ?? document);
183
+ }
184
+ /**
185
+ * Create an embed that will contain a widget.
186
+ * @param options {WidgetArgs} Widget creation args
187
+ * @returns An embed, that can be injected programmatically, and that will contain the
188
+ */
189
+ static createWidgetEmbed(t) {
190
+ const { widget: e } = n.createWidget(t), o = new I(e);
191
+ return r.log("Create Widget embed", {
192
+ data: n.cleanupLoggedArgs(t)
193
+ }), o;
194
+ }
195
+ static clear() {
196
+ n.widgets.forEach((t) => {
197
+ t.remove();
198
+ }), n.widgets = [], n.widgetOptions = {}, r.log("All Widget SDK MeltingSpot elements were cleared");
199
+ }
200
+ static bindEntityDrawerTo(t, e) {
201
+ const { spotId: o, rootDocument: i, msCommunityWebBaseUrl: d } = h.getConfiguration({
202
+ spotId: e.data.spotId,
203
+ rootDocument: e.data.rootDocument ?? document,
204
+ msCommunityWebBaseUrl: e.data.baseURL
205
+ });
206
+ if (!o)
207
+ throw r.error("Spot ID is required"), new Error(
208
+ "Spot ID is not configured. Please set the spotId in the configuration."
209
+ );
210
+ let s, c;
211
+ switch (e.type) {
212
+ case "Widget":
213
+ {
214
+ const u = n.createWidget({
215
+ ...e.data,
216
+ spotId: o,
217
+ rootDocument: i,
218
+ baseURL: d
219
+ });
220
+ s = u.widget, c = u.args;
221
+ }
222
+ break;
223
+ case "WidgetContent": {
224
+ const u = n.createWidgetContent({
225
+ ...e.data,
226
+ spotId: o,
227
+ rootDocument: i,
228
+ baseURL: d
229
+ });
230
+ s = u.widgetContent, c = u.args;
231
+ }
232
+ }
233
+ const a = new p(s, i), g = {
234
+ position: e.data.position
235
+ }, m = e.data.into ?? i.body;
236
+ return a.inject({
237
+ into: m,
238
+ bind: t,
239
+ options: g
240
+ }), n.widgets.push(a), r.log(
241
+ "Widget bound to drawer",
242
+ n.cleanupLoggedArgs(c),
243
+ g,
244
+ m,
245
+ t
246
+ ), a;
247
+ }
248
+ static bindEntityModalTo(t, e) {
249
+ const { spotId: o, rootDocument: i, msCommunityWebBaseUrl: d } = h.getConfiguration({
250
+ spotId: e.data.spotId,
251
+ rootDocument: e.data.rootDocument ?? document,
252
+ msCommunityWebBaseUrl: e.data.baseURL
253
+ });
254
+ if (!o)
255
+ throw r.error("Spot ID is required"), new Error(
256
+ "Spot ID is not configured. Please set the spotId in the configuration."
257
+ );
258
+ let s, c;
259
+ switch (e.type) {
260
+ case "Widget":
261
+ {
262
+ const m = n.createWidget({
263
+ ...e.data,
264
+ spotId: o,
265
+ rootDocument: i,
266
+ baseURL: d
267
+ });
268
+ s = m.widget, c = m.args;
269
+ }
270
+ break;
271
+ case "WidgetContent": {
272
+ const m = n.createWidgetContent({
273
+ ...e.data,
274
+ spotId: o,
275
+ rootDocument: i,
276
+ baseURL: d
277
+ });
278
+ s = m.widgetContent, c = m.args;
279
+ }
280
+ }
281
+ const a = new f(s, i), g = e.data.into ?? i.body;
282
+ return a.inject({ into: g, bind: t }), n.widgets.push(a), r.log(
283
+ "Widget bound to modal",
284
+ n.cleanupLoggedArgs(c),
285
+ g,
286
+ t
287
+ ), a;
288
+ }
289
+ static handleMessageEvent(t) {
290
+ if (!t.data || typeof t.data != "object" || t.data.type !== "OpenWidgetContent")
291
+ return;
292
+ const e = t.data?.widgetId;
293
+ if (!e || typeof e != "string") {
294
+ r.error("widgetId is required in the message event data");
295
+ return;
296
+ }
297
+ const o = n.widgetOptions[e];
298
+ if (!o) {
299
+ r.error("No widget options found for widgetId", { widgetId: e });
300
+ return;
301
+ }
302
+ let i = null;
303
+ try {
304
+ i = new URL(o.baseURL).origin;
305
+ } catch {
306
+ i = null;
307
+ }
308
+ if (!i || t.origin !== i) {
309
+ r.warn("Ignoring OpenWidgetContent from unexpected origin", {
310
+ origin: t.origin,
311
+ expectedOrigin: i
312
+ });
313
+ return;
314
+ }
315
+ const { spotId: d, rootDocument: s, msCommunityWebBaseUrl: c } = h.getConfiguration({
316
+ spotId: o.spotId,
317
+ rootDocument: o.rootDocument ?? document,
318
+ msCommunityWebBaseUrl: o.baseURL
319
+ }), { widgetContent: a } = n.createWidgetContent({
320
+ ...o,
321
+ widgetContentId: t.data.widgetContentId,
322
+ contentType: t.data.contentType,
323
+ contentId: t.data.contentId,
324
+ widgetId: e,
325
+ spotId: d,
326
+ rootDocument: s,
327
+ baseURL: c
328
+ });
329
+ t.data.openingIn === "Drawer" ? new p(a, s).open() : t.data.openingIn === "Modal" && new f(a, s).open();
330
+ }
331
+ static createWidget(t) {
332
+ const e = new URLSearchParams(window.location.search), o = {
333
+ spotId: t.spotId,
334
+ widgetId: t.widgetId,
335
+ authToken: t.authToken,
336
+ themeMode: t.themeMode,
337
+ domain: t.domain,
338
+ rootDocument: t.rootDocument,
339
+ baseURL: t.baseURL,
340
+ utmParameters: t.utmParameters ?? C(e)
341
+ };
342
+ return n.widgetOptions[o.widgetId] = o, n.messageListened || (window.addEventListener("message", n.handleMessageEvent), n.messageListened = !0), {
343
+ widget: new k(o),
344
+ args: o
345
+ };
346
+ }
347
+ static createWidgetContent(t) {
348
+ const e = new URLSearchParams(window.location.search), o = {
349
+ spotId: t.spotId,
350
+ contentId: t.contentId,
351
+ contentType: t.contentType,
352
+ widgetContentId: t.widgetContentId,
353
+ widgetId: t.widgetId,
354
+ authToken: t.authToken,
355
+ themeMode: t.themeMode,
356
+ domain: t.domain,
357
+ rootDocument: t.rootDocument,
358
+ baseURL: t.baseURL,
359
+ utmParameters: t.utmParameters ?? C(e)
360
+ };
361
+ return {
362
+ widgetContent: new S(o),
363
+ args: o
364
+ };
365
+ }
366
+ static cleanupLoggedArgs(t) {
367
+ const { authToken: e, ...o } = t;
368
+ return {
369
+ ...o,
370
+ authToken: e ? "***" : void 0
371
+ };
372
+ }
373
+ };
374
+ n.widgets = [], n.messageListened = !1, n.widgetOptions = {};
375
+ let b = n;
376
+ const D = globalThis;
377
+ D.MeltingSpot || (D.MeltingSpot = {});
378
+ D.MeltingSpot.Widget = b;
379
+ export {
380
+ p as Drawer,
381
+ f as Modal,
382
+ b as Widget
383
+ };