UNPKG

@isartech/chat

Version:

A reusable native webcomponent that can be initiated with parameters such as an endpoint url and styles in any environment based on web technologies.

416 lines (415 loc) 14.8 kB
import * as v from "react"; import ue from "react"; import { IsarTechProduct as C, IsarTechChat as ie } from "./isartech-chat.mjs"; var O = { exports: {} }, P = {}; /** * @license React * react-jsx-runtime.production.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var q; function le() { if (q) return P; q = 1; var o = Symbol.for("react.transitional.element"), s = Symbol.for("react.fragment"); function a(t, c, f) { var l = null; if (f !== void 0 && (l = "" + f), c.key !== void 0 && (l = "" + c.key), "key" in c) { f = {}; for (var d in c) d !== "key" && (f[d] = c[d]); } else f = c; return c = f.ref, { $$typeof: o, type: t, key: l, ref: c !== void 0 ? c : null, props: f }; } return P.Fragment = s, P.jsx = a, P.jsxs = a, P; } var w = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var F; function fe() { return F || (F = 1, process.env.NODE_ENV !== "production" && function() { function o(e) { if (e == null) return null; if (typeof e == "function") return e.$$typeof === oe ? null : e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case N: return "Fragment"; case K: return "Profiler"; case z: return "StrictMode"; case te: return "Suspense"; case re: return "SuspenseList"; case ae: return "Activity"; } if (typeof e == "object") switch (typeof e.tag == "number" && console.error( "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue." ), e.$$typeof) { case u: return "Portal"; case Z: return (e.displayName || "Context") + ".Provider"; case X: return (e._context.displayName || "Context") + ".Consumer"; case ee: var n = e.render; return e = e.displayName, e || (e = n.displayName || n.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e; case ne: return n = e.displayName || null, n !== null ? n : o(e.type) || "Memo"; case D: n = e._payload, e = e._init; try { return o(e(n)); } catch { } } return null; } function s(e) { return "" + e; } function a(e) { try { s(e); var n = !1; } catch { n = !0; } if (n) { n = console; var i = n.error, R = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object"; return i.call( n, "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.", R ), s(e); } } function t(e) { if (e === N) return "<>"; if (typeof e == "object" && e !== null && e.$$typeof === D) return "<...>"; try { var n = o(e); return n ? "<" + n + ">" : "<...>"; } catch { return "<...>"; } } function c() { var e = g.A; return e === null ? null : e.getOwner(); } function f() { return Error("react-stack-top-frame"); } function l(e) { if (U.call(e, "key")) { var n = Object.getOwnPropertyDescriptor(e, "key").get; if (n && n.isReactWarning) return !1; } return e.key !== void 0; } function d(e, n) { function i() { W || (W = !0, console.error( "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)", n )); } i.isReactWarning = !0, Object.defineProperty(e, "key", { get: i, configurable: !0 }); } function m() { var e = o(this.type); return L[e] || (L[e] = !0, console.error( "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release." )), e = this.props.ref, e !== void 0 ? e : null; } function T(e, n, i, R, b, _, x, I) { return i = _.ref, e = { $$typeof: r, type: e, key: n, props: _, _owner: b }, (i !== void 0 ? i : null) !== null ? Object.defineProperty(e, "ref", { enumerable: !1, get: m }) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: 0 }), Object.defineProperty(e, "_debugInfo", { configurable: !1, enumerable: !1, writable: !0, value: null }), Object.defineProperty(e, "_debugStack", { configurable: !1, enumerable: !1, writable: !0, value: x }), Object.defineProperty(e, "_debugTask", { configurable: !1, enumerable: !1, writable: !0, value: I }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e; } function E(e, n, i, R, b, _, x, I) { var y = n.children; if (y !== void 0) if (R) if (ce(y)) { for (R = 0; R < y.length; R++) k(y[R]); Object.freeze && Object.freeze(y); } else console.error( "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead." ); else k(y); if (U.call(n, "key")) { y = o(e); var h = Object.keys(n).filter(function(se) { return se !== "key"; }); R = 0 < h.length ? "{key: someKey, " + h.join(": ..., ") + ": ...}" : "{key: someKey}", B[y + R] || (h = 0 < h.length ? "{" + h.join(": ..., ") + ": ...}" : "{}", console.error( `A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`, R, y, h, y ), B[y + R] = !0); } if (y = null, i !== void 0 && (a(i), y = "" + i), l(n) && (a(n.key), y = "" + n.key), "key" in n) { i = {}; for (var S in n) S !== "key" && (i[S] = n[S]); } else i = n; return y && d( i, typeof e == "function" ? e.displayName || e.name || "Unknown" : e ), T( e, y, _, b, c(), i, x, I ); } function k(e) { typeof e == "object" && e !== null && e.$$typeof === r && e._store && (e._store.validated = 1); } var p = ue, r = Symbol.for("react.transitional.element"), u = Symbol.for("react.portal"), N = Symbol.for("react.fragment"), z = Symbol.for("react.strict_mode"), K = Symbol.for("react.profiler"), X = Symbol.for("react.consumer"), Z = Symbol.for("react.context"), ee = Symbol.for("react.forward_ref"), te = Symbol.for("react.suspense"), re = Symbol.for("react.suspense_list"), ne = Symbol.for("react.memo"), D = Symbol.for("react.lazy"), ae = Symbol.for("react.activity"), oe = Symbol.for("react.client.reference"), g = p.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, U = Object.prototype.hasOwnProperty, ce = Array.isArray, A = console.createTask ? console.createTask : function() { return null; }; p = { "react-stack-bottom-frame": function(e) { return e(); } }; var W, L = {}, Y = p["react-stack-bottom-frame"].bind( p, f )(), H = A(t(f)), B = {}; w.Fragment = N, w.jsx = function(e, n, i, R, b) { var _ = 1e4 > g.recentlyCreatedOwnerStacks++; return E( e, n, i, !1, R, b, _ ? Error("react-stack-top-frame") : Y, _ ? A(t(e)) : H ); }, w.jsxs = function(e, n, i, R, b) { var _ = 1e4 > g.recentlyCreatedOwnerStacks++; return E( e, n, i, !0, R, b, _ ? Error("react-stack-top-frame") : Y, _ ? A(t(e)) : H ); }; }()), w; } var Q; function de() { return Q || (Q = 1, process.env.NODE_ENV === "production" ? O.exports = le() : O.exports = fe()), O.exports; } var j = de(); /** * @license * Copyright 2018 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const me = /* @__PURE__ */ new Set(["children", "localName", "ref", "style", "className"]), V = /* @__PURE__ */ new WeakMap(), $ = (o, s, a, t, c) => { const f = c == null ? void 0 : c[s]; f === void 0 ? (o[s] = a, a == null && s in HTMLElement.prototype && o.removeAttribute(s)) : a !== t && ((l, d, m) => { let T = V.get(l); T === void 0 && V.set(l, T = /* @__PURE__ */ new Map()); let E = T.get(d); m !== void 0 ? E === void 0 ? (T.set(d, E = { handleEvent: m }), l.addEventListener(d, E)) : E.handleEvent = m : E !== void 0 && (T.delete(d), l.removeEventListener(d, E)); })(o, f, a); }, Ee = ({ react: o, tagName: s, elementClass: a, events: t, displayName: c }) => { const f = new Set(Object.keys(t ?? {})), l = o.forwardRef((d, m) => { const T = o.useRef(/* @__PURE__ */ new Map()), E = o.useRef(null), k = {}, p = {}; for (const [r, u] of Object.entries(d)) me.has(r) ? k[r === "className" ? "class" : r] = u : f.has(r) || r in a.prototype ? p[r] = u : k[r] = u; return o.useLayoutEffect(() => { if (E.current === null) return; const r = /* @__PURE__ */ new Map(); for (const u in p) $(E.current, u, d[u], T.current.get(u), t), T.current.delete(u), r.set(u, d[u]); for (const [u, N] of T.current) $(E.current, u, void 0, N, t); T.current = r; }), o.useLayoutEffect(() => { var r; (r = E.current) == null || r.removeAttribute("defer-hydration"); }, []), k.suppressHydrationWarning = !0, o.createElement(s, { ...k, ref: o.useCallback((r) => { E.current = r, typeof m == "function" ? m(r) : m !== null && (m.current = r); }, [m]) }); }); return l.displayName = c ?? a.name, l; }, Re = (o, s) => { const a = new Map(o.cart); switch (s.type) { case "PRODUCT_INCREMENT": { const t = o.cart.get(s.payload.itemId); return t ? a.set(t.itemId, { ...t, quantity: t.quantity + 1 }) : a.set(s.payload.itemId, { ...s.payload, quantity: 1 }), { cart: a }; } case "PRODUCT_DECREMENT": { const t = a.get(s.payload.itemId); return t && t.quantity && t.quantity > 0 && a.set(t.itemId, { ...t, quantity: t.quantity - 1 }), !(t != null && t.quantity) && !(t != null && t.bulkQuantity) && a.delete(s.payload.itemId), { cart: a }; } case "PRODUCT_INCREMENT_BULK": { const t = o.cart.get(s.payload.itemId); return t ? a.set(t.itemId, { ...t, bulkQuantity: t.bulkQuantity + 1 }) : a.set(s.payload.itemId, { ...s.payload, bulkQuantity: 1 }), { cart: a }; } case "PRODUCT_DECREMENT_BULK": { const t = a.get(s.payload.itemId); return t && t.bulkQuantity && t.bulkQuantity > 0 && a.set(t.itemId, { ...t, bulkQuantity: t.bulkQuantity - 1 }), { cart: a }; } default: return o; } }, G = v.createContext({ cart: /* @__PURE__ */ new Map() }), J = v.createContext((o) => { }), ye = Ee({ tagName: "isartech-chat", elementClass: ie, react: v, events: { changeQuantity: C.EVENT_CHANGE_QUANTITY, changeBulkQuantity: C.EVENT_CHANGE_QUANTITY_BULK, onProductIncrement: C.EVENT_INCREMENT, onProductDecrement: C.EVENT_DECREMENT, onProductIncrementBulk: C.EVENT_INCREMENT_BULK, onProductDecrementBulk: C.EVENT_DECREMENT_BULK } }), pe = ye, M = v.createContext({ incrementWithHook: async () => !0, decrementWithHook: async () => !0, incrementBulkWithHook: async () => !0, decrementBulkWithHook: async () => !0 }), Te = () => ({ cart: /* @__PURE__ */ new Map() }), ve = ({ children: o, initialState: s, onBeforeIncrement: a, onBeforeDecrement: t, onBeforeIncrementBulk: c, onBeforeDecrementBulk: f, onError: l }) => { const [d, m] = v.useReducer(Re, s || Te()), T = async (r) => { try { return a && (r = await a(r)), m({ type: "PRODUCT_INCREMENT", payload: r }), !0; } catch (u) { return console.error("error incrementing", u), l && l(u, "increment", r), !1; } }, E = async (r) => { try { return t && (r = await t(r)), m({ type: "PRODUCT_DECREMENT", payload: r }), !0; } catch (u) { return console.error("error decrementing product", u), l && l(u, "decrement", r), !1; } }, k = async (r) => { try { return c && (r = await c(r)), m({ type: "PRODUCT_INCREMENT_BULK", payload: r }), !0; } catch (u) { return console.error("error incrementing bulk product", u), l && l(u, "incrementBulk", r), !1; } }, p = async (r) => { try { return f && (r = await f(r)), m({ type: "PRODUCT_DECREMENT_BULK", payload: r }), !0; } catch (u) { return console.error("error decrementing bulk product", u), l && l(u, "decrementBulk", r), !1; } }; return /* @__PURE__ */ j.jsx(G.Provider, { value: d, children: /* @__PURE__ */ j.jsx(J.Provider, { value: m, children: /* @__PURE__ */ j.jsx(M.Provider, { value: { incrementWithHook: T, decrementWithHook: E, incrementBulkWithHook: k, decrementBulkWithHook: p }, children: o }) }) }); }, be = () => v.useContext(G), he = () => v.useContext(J), Ce = () => v.useContext(M), Ne = () => { const { incrementWithHook: o, decrementWithHook: s, incrementBulkWithHook: a, decrementBulkWithHook: t } = v.useContext(M); return { onProductIncrement: async (c) => { await o(c.detail); }, onProductDecrement: async (c) => { await s(c.detail); }, onProductIncrementBulk: async (c) => { await a(c.detail); }, onProductDecrementBulk: async (c) => { await t(c.detail); } }; }; export { M as CartApiContext, G as CartContext, J as CartDispatch, pe as IsartechChatReact, ve as IsartechProvider, Re as cartReducer, pe as default, he as setCart, be as useCart, Ce as useCartApi, Ne as useCartEventHandlers };