var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { MemoryRouter as Router } from 'react-router-dom';
import { CacheProvider, ThemeProvider } from '@emotion/react';
import { create } from 'jss';
import { StylesProvider, jssPreset } from '@mui/styles';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { SwTheme } from './theme';
import store from './store/store';
import { EventsHandlerWrapper } from './components/EventsHandlerWrapper';
import SwAuthModal, { SwAuthButton } from './SwAuth';
import { AttributeNames, createShadowElement, extractAttributes, isElement } from './utils/utils';
export function InitSwAuth(authConfig) {
    if (authConfig === void 0) { authConfig = null; }
    var TAG_NAME = 'sw-auth';
    // we don't to initialized again when saving changes on hot-reloading
    if (customElements.get(TAG_NAME)) {
        return;
    }
    customElements.define(TAG_NAME, /** @class */ (function (_super) {
        __extends(class_1, _super);
        function class_1() {
            var _this = _super !== null && _super.apply(this, arguments) || this;
            _this.setAttributeChangeCallback = function (callBack) {
                if (callBack)
                    _this.childAttrCalback = callBack;
            };
            return _this;
        }
        Object.defineProperty(class_1, "observedAttributes", {
            get: function () {
                // Add all tracked attributes to this array
                return [AttributeNames.hideButton, AttributeNames.disableCreateNewUser];
            },
            enumerable: false,
            configurable: true
        });
        class_1.prototype.attributeChangedCallback = function (name, oldValue, newValue) {
            if (this.childAttrCalback)
                this.childAttrCalback(name, oldValue, newValue);
        };
        class_1.prototype.connectedCallback = function () {
            var jss = create(jssPreset());
            var attributes = extractAttributes(this);
            var content = null;
            var mountPoint = null;
            if (authConfig === null || authConfig === void 0 ? void 0 : authConfig.container) {
                if (!isElement(authConfig.container)) {
                    throw new Error('Container is not of type HTMLElement');
                }
                Object.assign(authConfig.container.style, __assign({ position: 'absolute', left: '0', right: '0', top: '0', bottom: '0', overflow: 'hidden' }, (authConfig.containerStyles || {})));
                var mConfig = createShadowElement({ container: authConfig.container, className: 'sw-auth-modal' });
                var bConfig = createShadowElement({ container: this, className: 'sw-auth-button' });
                mountPoint = mConfig.mountPoint;
                content = (_jsxs(_Fragment, { children: [_jsx(CacheProvider, __assign({ value: bConfig.cache }, { children: _jsx(SwAuthButton, { container: bConfig.root, attributes: attributes, setAttrCallback: this.setAttributeChangeCallback }, void 0) }), void 0), _jsx(CacheProvider, __assign({ value: mConfig.cache }, { children: _jsx(SwAuthModal, { rootContainer: authConfig.container, container: mConfig.root }, void 0) }), void 0)] }, void 0));
            }
            else {
                var config = createShadowElement({ container: this, className: 'sw-auth' });
                mountPoint = config.mountPoint;
                content = (_jsx(_Fragment, { children: _jsxs(CacheProvider, __assign({ value: config.cache }, { children: [_jsx(SwAuthButton, { container: config.root, attributes: attributes, setAttrCallback: this.setAttributeChangeCallback }, void 0), _jsx(SwAuthModal, { container: config.root }, void 0)] }), void 0) }, void 0));
            }
            ReactDOM.render(_jsx(ThemeProvider, __assign({ theme: SwTheme() }, { children: _jsx(Provider, __assign({ store: store }, { children: _jsx(Router, __assign({ initialEntries: ['/'] }, { children: _jsx(EventsHandlerWrapper, { children: _jsx(StylesProvider, __assign({ jss: jss }, { children: content }), void 0) }, void 0) }), void 0) }), void 0) }), void 0), mountPoint);
        };
        return class_1;
    }(HTMLElement)));
}
