function p(l, e) {
try {
return JSON.parse(l);
} catch {
return e;
}
}
const g = "376px", x = "628px";
function u(l) {
const e = document.createElement("button");
return e.addEventListener("click", l.onClick), e.id = "constructor-ai-chat-widget-button", e.setAttribute("aria-label", "Open chat widget"), e.type = "button", e.tabIndex = 0, e.style.position = "fixed", e.style.bottom = "20px", e.style.right = "20px", e.style.zIndex = "7000", e.style.border = "2px solid #FFFFFF", e.style.borderRadius = "100%", e.style.boxShadow = "0px 0px 8px 0px rgba(0, 0, 0, 0.15)", e.style.width = "48px", e.style.height = "48px", e.style.display = "flex", e.style.justifyContent = "center", e.style.alignItems = "center", e.style.cursor = "pointer", e.style.transition = "all 0.3s ease", e.style.backgroundColor = l.toggleColor ?? "rgba(10, 10, 10, 0.94)", e.style.color = "rgba(255, 255, 255, 0.98)", e.addEventListener("focus", () => {
e.style.outline = "2px solid #008CE3";
}), e.addEventListener("mouseover", () => {
e.style.transform = "scale(1.1)", e.style.boxShadow = "0px 0px 12px 0px rgba(0, 0, 0, 0.25)";
}), e.addEventListener("mouseout", () => {
e.style.transform = "scale(1)", e.style.boxShadow = "0px 0px 8px 0px rgba(0, 0, 0, 0.15)";
}), e;
}
function r(l) {
l.innerHTML = '';
}
function a(l, e, t, i) {
if (e)
l.innerHTML = '', l.style.marginTop = "16px";
else if (i) {
const n = new Image();
n.width = 20, n.height = 20, n.alt = "widget logo", n.onerror = () => r(l), l.innerHTML = "", l.appendChild(n), n.src = f(t);
} else
r(l);
}
function f(l) {
return `${new URL(import.meta.url).origin}/api/platform-open/ai-search/knowledge-models/${l}/widget/cover`;
}
function y(l, e) {
const t = document.createElement("iframe"), i = new URL(l);
return t.id = "constructor-ai-chat-widget-iframe", t.src = i.toString(), t.style.position = "fixed", t.style.display = "block", t.style.bottom = "80px", t.style.right = "20px", t.style.maxInlineSize = e.maxInlineSize, t.style.inlineSize = "calc(100% - 40px)", t.style.maxBlockSize = e.maxBlockSize, t.style.blockSize = "calc(100% - 100px)", t.style.border = "none", t.style.padding = "12px", t.style.boxShadow = "0px 0px 8px 0px rgba(0, 0, 0, 0.15)", t.style.borderRadius = "20px", t.style.backgroundColor = "#FFFFFF", t.style.zIndex = "7000", t;
}
function m(l) {
const e = document.createElement("div");
return e.innerText = l, e.style.padding = "16px", e.style.borderRadius = "20px 20px 0px 20px", e.style.backgroundColor = "#fff", e.style.boxShadow = "0px 0px 8px 0px rgba(0, 0, 0, 0.15)", e.style.fontSize = "13px", e.style.lineHeight = "20px", e.style.fontFamily = "sans-serif", e.style.color = "rgba(10, 10, 10, .94)", e.style.maxInlineSize = "100%", e.style.fontWeight = "600", e.style.textOverflow = "ellipsis", e.style.overflow = "hidden", e.style.boxSizing = "border-box", e;
}
function h(l, e) {
const t = document.createElement("div");
t.style.position = "fixed", t.style.bottom = "84px", t.style.right = "20px", t.style.zIndex = "7000", t.style.display = "flex", t.style.flexDirection = "column", t.style.alignItems = "flex-end", t.style.gap = "16px", t.style.maxInlineSize = e.maxInlineSize;
for (const i of l)
t.appendChild(m(i));
return document.body.appendChild(t), t;
}
function s(l, e) {
var t;
if (l.isOpen = !l.isOpen, a(l.button, l.isOpen, e.knowledgeModelId, e.coverFileId), l.isOpen) {
if ((t = l.initialMessages) == null || t.remove(), l.initialMessages = null, !l.iframe) {
const i = new URL(
`/platform/knowledge-models/${e.knowledgeModelId}/chat-widget`,
// use this line for debugging from localhost
// 'https://test-constructor.dev',
new URL(import.meta.url).origin
).href;
l.iframe = y(i, e), document.body.appendChild(l.iframe);
}
} else l.iframe && (l.iframe.remove(), l.iframe = null);
}
function b(l) {
const e = {
button: u({
onClick: () => s(e, l),
toggleColor: l.toggleColor
}),
iframe: null,
initialMessages: null,
isOpen: !1
};
l.collapsed && l.initialMessages.length && (e.initialMessages = h(l.initialMessages, l)), l.collapsed || (s(e, l), e.isOpen = !0), a(e.button, e.isOpen, l.knowledgeModelId, l.coverFileId), document.body.appendChild(e.button);
}
function w(l) {
return p(l, []).filter((t) => t);
}
const o = document.querySelector(`script[src="proxy.php?url=${import.meta.url}"]`);
if (o && o instanceof HTMLScriptElement) {
const {
knowledgeModelId: l,
collapsed: e,
maxBlockSize: t,
maxInlineSize: i,
initialMessages: n,
toggleColor: d,
coverFileId: c
} = o.dataset;
l ? b({
knowledgeModelId: l,
collapsed: e !== "false",
initialMessages: w(n),
toggleColor: d || null,
maxBlockSize: t || x,
maxInlineSize: i || g,
coverFileId: c || null
}) : console.error("Knowledge model ID not provided");
}