import { firstValueFrom, Subject, take } from 'rxjs' import { SofiaProRegular, SofiaProLight } from '@web3-onboard/common' import AccountSelect from './views/AccountSelect.svelte' import { accounts$ } from './streams.js' import { validateSelectAccountOptions } from './validation.js' import type { SelectAccountOptions, Account } from './types.js' // eslint-disable-next-line max-len const accountSelect = async ( options: SelectAccountOptions ): Promise => { if (options) { const error = validateSelectAccountOptions(options) if (error) { throw error } } const app = mountAccountSelect(options, accounts$) accounts$.pipe(take(1)).subscribe(() => { app.$destroy() }) return firstValueFrom(accounts$) } // eslint-disable-next-line max-len const mountAccountSelect = ( selectAccountOptions: SelectAccountOptions, accounts$: Subject ) => { class AccountSelectEl extends HTMLElement { constructor() { super() } } if (!customElements.get('account-select')) { customElements.define('account-select', AccountSelectEl) } // Add Fonts to main page const styleEl = document.createElement('style') styleEl.innerHTML = ` ${SofiaProRegular} ${SofiaProLight} ` document.body.appendChild(styleEl) // add to DOM const accountSelectDomElement = document.createElement('account-select') const target = accountSelectDomElement.attachShadow({ mode: 'open' }) accountSelectDomElement.style.all = 'initial' target.innerHTML = ` ` const containerElementQuery = selectAccountOptions.containerElement || 'body' const containerElement = document.querySelector(containerElementQuery) if (!containerElement) { throw new Error( `Element with query ${containerElementQuery} does not exist.` ) } containerElement.appendChild(accountSelectDomElement) const app = new AccountSelect({ target: target, props: { selectAccountOptions, accounts$ } }) return app } export default accountSelect