{"version":3,"file":"makeAdminPortalComponentMountFn.mjs","sources":["../../../src/bindings/makeAdminPortalComponentMountFn.ts"],"sourcesContent":["import { StytchProjectConfigurationInput } from '@stytch/core/public';\nimport { AdminPortalCommonProps } from '@stytch/web/adminPortal/types';\nimport type { ComponentType } from 'react';\n\nimport { CreateSSRSafeWebComponent, IReactWebComponent } from './CreateWebComponent';\n\nexport type AdminPortalWebComponentOption = {\n  /**\n   * An HTML element or query selector string for the element that should contain the UI.\n   * @example '#container'\n   */\n  element: string | HTMLElement;\n};\n\nexport const makeAdminPortalComponentMountFn = <\n  TMountOptions extends AdminPortalCommonProps<StytchProjectConfigurationInput>,\n>(\n  component: ComponentType<TMountOptions>,\n  webComponentName: string,\n  mountFunctionName: string,\n) => {\n  const WebComponent = CreateSSRSafeWebComponent<TMountOptions>(component, webComponentName);\n\n  return (options: TMountOptions & AdminPortalWebComponentOption) => {\n    const { client, element } = options;\n    if (!client) {\n      throw new Error('A Stytch B2B UI client must be provided.');\n    }\n\n    const targetParentDomNode = typeof element === 'string' ? document.querySelector(element) : element;\n    if (!targetParentDomNode) {\n      if (typeof element === 'string') {\n        throw new Error(\n          `The selector you specified (${element}) applies to no DOM elements that are currently on the page. Make sure the element exists on the page before calling ${mountFunctionName}().`,\n        );\n      }\n      throw new Error(\n        `The element you provided is not a valid DOM element. Make sure the element exists on the page before calling ${mountFunctionName}().`,\n      );\n    }\n    /**\n     * If mount is called again on the same element,\n     * and it already has a matching child,\n     * call its render method to update its internal prop state\n     */\n    if (\n      // HTML Node names are canonically all upper case always\n      targetParentDomNode.firstChild?.nodeName.toLowerCase() === webComponentName.toLowerCase()\n    ) {\n      const node = targetParentDomNode.firstChild as IReactWebComponent<TMountOptions>;\n      node.render(options);\n      return;\n    }\n\n    /**\n     * If mount is called for the first time,\n     * make a new element!\n     */\n    const loginElement = WebComponent(options);\n    targetParentDomNode.appendChild(loginElement);\n  };\n};\n"],"names":["makeAdminPortalComponentMountFn","component","webComponentName","mountFunctionName","WebComponent","CreateSSRSafeWebComponent","options","client","element","Error","targetParentDomNode","document","querySelector","firstChild","nodeName","toLowerCase","node","render","loginElement","appendChild"],"mappings":";;AAcO,MAAMA,+BAAAA,GAAkC,CAG7CC,SAAAA,EACAC,gBAAAA,EACAC,iBAAAA,GAAAA;IAEA,MAAMC,YAAAA,GAAeC,0BAAyCJ,SAAAA,EAAWC,gBAAAA,CAAAA;AAEzE,IAAA,OAAO,CAACI,OAAAA,GAAAA;AACN,QAAA,MAAM,EAAEC,MAAM,EAAEC,OAAO,EAAE,GAAGF,OAAAA;AAC5B,QAAA,IAAI,CAACC,MAAAA,EAAQ;AACX,YAAA,MAAM,IAAIE,KAAAA,CAAM,0CAAA,CAAA;AAClB,QAAA;AAEA,QAAA,MAAMC,sBAAsB,OAAOF,OAAAA,KAAY,WAAWG,QAAAA,CAASC,aAAa,CAACJ,OAAAA,CAAAA,GAAWA,OAAAA;AAC5F,QAAA,IAAI,CAACE,mBAAAA,EAAqB;YACxB,IAAI,OAAOF,YAAY,QAAA,EAAU;gBAC/B,MAAM,IAAIC,KAAAA,CACR,CAAC,4BAA4B,EAAED,QAAQ,qHAAqH,EAAEL,iBAAAA,CAAkB,GAAG,CAAC,CAAA;AAExL,YAAA;AACA,YAAA,MAAM,IAAIM,KAAAA,CACR,CAAC,6GAA6G,EAAEN,iBAAAA,CAAkB,GAAG,CAAC,CAAA;AAE1I,QAAA;AACA;;;;AAIC,QACD;AAEEO,QAAAA,mBAAAA,CAAoBG,UAAU,EAAEC,QAAAA,CAASC,WAAAA,EAAAA,KAAkBb,gBAAAA,CAAiBa,WAAW,EAAA,EACvF;YACA,MAAMC,IAAAA,GAAON,oBAAoBG,UAAU;AAC3CG,YAAAA,IAAAA,CAAKC,MAAM,CAACX,OAAAA,CAAAA;AACZ,YAAA;AACF,QAAA;AAEA;;;QAIA,MAAMY,eAAed,YAAAA,CAAaE,OAAAA,CAAAA;AAClCI,QAAAA,mBAAAA,CAAoBS,WAAW,CAACD,YAAAA,CAAAA;AAClC,IAAA,CAAA;AACF;;;;"}