"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.webComponentDataAttributeName = exports.versionedWebComponentName = exports.renderReactInWebComponent = exports.getReactWebComponent = exports.WebComponentClass = void 0;
var _createReactRootElements = require("./createReactRootElements.cjs");
var _encapsulateShadowDomFromGlobalStyles = require("./encapsulateShadowDomFromGlobalStyles.cjs");
var _odysseyWebComponentVersionGenerated = _interopRequireDefault(require("./odysseyWebComponentVersion.generated.cjs"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function __transformExtension(filepath, extMapping) {
  if (!filepath.startsWith('./') && !filepath.startsWith('../')) {
    return filepath;
  }
  const idx = filepath.lastIndexOf('.');
  if (idx === -1 || filepath.includes('/', idx)) {
    const newExt = extMapping[''];
    if (newExt) {
      return filepath + newExt;
    }
    return filepath;
  }
  for (let [origExt, newExt] of Object.entries(extMapping).sort((a, b) => b[0].length - a[0].length)) {
    if (filepath.endsWith(origExt)) {
      return filepath.slice(0, -origExt.length) + newExt;
    }
  }
  return filepath;
} /*!
   * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.
   * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
   *
   * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *
   * See the License for the specific language governing permissions and limitations under the License.
   */

const webComponentDataAttributeName = exports.webComponentDataAttributeName = "data-odyssey-react-web-component";
const versionedWebComponentName = exports.versionedWebComponentName = `odyssey-react-web-component-${_odysseyWebComponentVersionGenerated.default}`.toLowerCase();
const SsrFriendlyHtmlElementClass = "HTMLElement" in globalThis ? HTMLElement : class {};
class WebComponentClass extends SsrFriendlyHtmlElementClass {
  #getReactComponent = null;
  #reactRootElements;
  elementName = this.localName;
  reactRootPromise = Promise.resolve(null);
  constructor() {
    super();
    this.#reactRootElements = (0, _createReactRootElements.createReactRootElements)();
    const {
      appRootElement,
      stylesRootElement
    } = this.#reactRootElements;
    const shadowRoot = this.attachShadow({
      mode: "open"
    });
    shadowRoot.appendChild(stylesRootElement);
    shadowRoot.appendChild(appRootElement);
  }
  setGetReactComponent(getReactComponent) {
    this.reactRootPromise = this.reactRootPromise.then(reactRoot => {
      this.#getReactComponent = getReactComponent;
      if (reactRoot) {
        reactRoot.render(this.#getReactComponent(this.#reactRootElements));
      } else {}
      return reactRoot;
    });
  }
  connectedCallback() {
    (0, _encapsulateShadowDomFromGlobalStyles.encapsulateShadowDomFromGlobalStyles)({
      nonce: this.getAttribute("nonce") || window.cspNonce,
      stylesRootElement: this.#reactRootElements.stylesRootElement
    });
    this.reactRootPromise = this.reactRootPromise.then(reactRoot => {
      if (reactRoot) {
        throw new Error(`connectedCallback fired when reactRoot is already mounted.`);
      }
      return (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(__transformExtension("react-dom/client", {
        ".js": ".cjs"
      })).then(({
        createRoot
      }) => createRoot(this.#reactRootElements.appRootElement));
    }).then(reactRoot => {
      if (!this.#getReactComponent) {} else {
        reactRoot.render(this.#getReactComponent(this.#reactRootElements));
      }
      return reactRoot;
    });
  }
  disconnectedCallback() {
    this.reactRootPromise = this.reactRootPromise.then(reactRoot => {
      if (!reactRoot) {
        throw new Error(`disconnectedCallback fired when reactRoot is already unmounted.`);
      }
      reactRoot.unmount();
      return null;
    });
  }
}
exports.WebComponentClass = WebComponentClass;
const getReactWebComponent = ({
  webComponentName = versionedWebComponentName,
  getReactComponent,
  nonce
}) => {
  if (!customElements.get(webComponentName)) {
    customElements.define(webComponentName, WebComponentClass);
  }
  const element = document.createElement(webComponentName);
  element.setAttribute(webComponentDataAttributeName, "");
  element.setAttribute("nonce", nonce);
  element.setGetReactComponent(getReactComponent);
  return element;
};
exports.getReactWebComponent = getReactWebComponent;
const renderReactInWebComponent = ({
  webComponentName,
  getReactComponent,
  webComponentChildren,
  webComponentParentElement,
  webComponentRootElement,
  nonce = window.cspNonce
}) => {
  const reactElement = getReactWebComponent({
    getReactComponent,
    webComponentName,
    nonce
  });
  if (webComponentChildren) {
    (Array.isArray(webComponentChildren) ? webComponentChildren : [webComponentChildren]).forEach(webComponentChild => {
      reactElement.appendChild(webComponentChild);
    });
  }
  if (webComponentParentElement) {
    webComponentParentElement.appendChild(reactElement);
  } else if (webComponentRootElement) {
    webComponentRootElement.appendChild(reactElement);
  }
  return reactElement;
};
exports.renderReactInWebComponent = renderReactInWebComponent;
//# sourceMappingURL=renderReactInWebComponent.cjs.map