import { user } from "pn-design-assets/pn-assets/icons.js";
import { h, Host, } from "@stencil/core";
import { MarketWebLoginManager, } from "../../../globals/MarketWebLoginManager";
import { state as headerState } from "./pn-marketweb-siteheader-store";
export class PnMarketwebSiteheaderLogin {
    constructor() {
        this.endpoint = '';
        this.token = '';
        this.i18n = {
            searchlabel: '',
            searchplaceholder: '',
            searchbuttontext: '',
            menuHomeButton: '',
            menuGoBackButton: '',
            mainMenuButton: '',
            menuStartButton: '',
            profileSelectionFlyoutHeading: '',
            unifiedLoginButton: '',
            unifiedMyPagesButton: '',
        };
        this.siteUrl = '';
        this.emitEvents = true;
        this.loginDialog = null;
        this.fullname = '';
        this.loggedin = false;
        this.showProfileSelection = false;
        this.showUnifiedLogin = false;
        this.checkUserLoggedInStateInterval = 0;
        this.environment = undefined;
        this.appearance = 'light';
        this.loginLinks = undefined;
        this.toggleButtonText = this.loginDialog?.loginMenuLinkText ? this.loginDialog.loginMenuLinkText : '';
        this.username = this.fullname;
        this.useMarketwebLogin = undefined;
    }
    hostElement;
    // Events
    loginStateChange;
    componentWillLoad() {
        const userInfo = this.loggedin && this.fullname ? { given_name: this.fullname } : null;
        headerState.loginManager = new MarketWebLoginManager({
            endpoint: this.endpoint,
            eventTarget: this.hostElement,
            checkUserLoggedInStateInterval: this.checkUserLoggedInStateInterval,
            userInfo,
        });
        headerState.loginManager.setSiteUrl(this.siteUrl);
        this.hostElement['loginmanager'] = headerState.loginManager;
        if (this.token) {
            headerState.loginManager.registerToken(this.token, 'backend');
        }
        else if (this.emitEvents) {
            // The primary login instance
            headerState.loginManager.invalidateTokenOfType('backend');
        }
        this.hostElement.addEventListener(headerState.loginManager.events.loginstatechange, this.onLoginStateChange.bind(this));
        this.init();
        this.setUserName();
        this.setToggleButtonText();
    }
    async init() {
        headerState.loginManager.init(this.emitEvents);
        this.setToggleButtonText();
        this.adjustLoginLinks();
    }
    adjustLoginLinks() {
        if (!this.loginDialog?.loginMenuLinks) {
            return;
        }
        const adjustLink = (link, i) => {
            if (!link.isLogoutLink) {
                link.isLogoutLink = link.pageLink.indexOf('location.href') !== -1 && link.pageLink.indexOf('logout') !== -1;
            }
            if (link.isLogoutLink) {
                link.href = headerState.loginManager.getLogoutUrl(link.pageLink);
            }
            if (i === 0 && !link.href) {
                link.href = headerState.loginManager.getLoginUrl();
                link.linkType = 'primary';
            }
            if (link.primaryLinkApperance && !link.linkType) {
                link.linkType = 'primary';
            }
            return link;
        };
        if (this.loginDialog) {
            this.loginDialog.loginMenuLinks = this.loginDialog.loginMenuLinks.map(adjustLink);
            this.loginDialog.loggedInLinks = this.loginDialog.loggedInLinks.map(adjustLink);
        }
        this.loginDialog.legacyLoginLinks = [
            {
                href: headerState.loginManager.getLoginUrl(this.loginDialog.loginUrl),
                linkText: this.loginDialog.loginLinkText,
                openInNewWindow: false,
                isLogoutLink: false,
                pageLink: null,
                linkType: 'primary',
                primaryLinkApperance: true,
            },
            {
                href: headerState.loginManager.getLoginUrl(this.loginDialog.registerUrl),
                linkText: this.loginDialog.createLoginLinkText,
                openInNewWindow: false,
                isLogoutLink: false,
                pageLink: null,
                primaryLinkApperance: false,
            },
        ];
    }
    setUserName() {
        const userInfo = headerState.loginManager.getUserInfo();
        let name = '';
        if (!this.loggedin || !userInfo) {
            this.username = name;
        }
        if (userInfo && userInfo.given_name) {
            name = userInfo.given_name + (userInfo?.family_name ? ` ${userInfo?.family_name}` : '');
        }
        if (!name && userInfo && userInfo.email) {
            name = userInfo.email;
        }
        if (!name && this.fullname) {
            name = this.fullname;
        }
        this.username = name;
        this.adjustLoginLinks();
    }
    setToggleButtonText() {
        if (this.loggedin) {
            this.toggleButtonText = this.username;
            if (!this.toggleButtonText) {
                this.toggleButtonText = this.loginDialog.loggedInButtonText;
            }
        }
        else {
            this.toggleButtonText = this.loginDialog.loginMenuLinkText;
        }
    }
    onLoginStateChange(e) {
        if (this.loggedin !== e.detail && this.emitEvents) {
            this.loggedin = e.detail;
            this.loginStateChange.emit({ loggedIn: this.loggedin, token: headerState.loginManager.getToken() });
            this.setUserName();
            this.setToggleButtonText();
        }
    }
    render() {
        if (!this.loginDialog) {
            return;
        }
        let hostElementAttribute = {
            username: null,
            loggedin: 'false',
        };
        if (this.username) {
            hostElementAttribute.username = this.username;
            hostElementAttribute.loggedin = this.loggedin + '';
        }
        return (h(Host, { ...hostElementAttribute }, h("pn-button-dropdown", { label: this.toggleButtonText, icon: user, appearance: this.appearance, variant: "borderless", class: "siteheader-logindialog" }, this.showUnifiedLogin && !this.loggedin && (h("pn-marketweb-siteheader-unified-login", { loggedIn: this.loggedin, logInLabel: this.i18n.unifiedLoginButton, useMarketwebLogin: this.useMarketwebLogin, environment: this.environment })), this.loggedin && this.showProfileSelection ? (h("pn-marketweb-siteheader-login-profileselection", { loginDialog: this.loginDialog, loggedin: this.loggedin, i18n: this.i18n, idNamespace: this.emitEvents ? '1' : '2', endpoint: this.endpoint })) : (h("pn-marketweb-siteheader-login-links", { loginDialog: this.loginDialog, loggedin: this.loggedin, idNamespace: this.emitEvents ? '1' : '2', username: this.username, showUnifiedLogin: this.showUnifiedLogin })), !this.loggedin && this.loginDialog?.loginInfo && (h("span", { innerHTML: `${this.loginDialog.loginInfo}` })))));
    }
    static get is() { return "pn-marketweb-siteheader-login"; }
    static get originalStyleUrls() {
        return {
            "$": ["pn-marketweb-siteheader-login.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["pn-marketweb-siteheader-login.css"]
        };
    }
    static get properties() {
        return {
            "endpoint": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Specifies which endpoint domain we should load from"
                },
                "attribute": "endpoint",
                "reflect": false,
                "defaultValue": "''"
            },
            "token": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Access token passed from backend"
                },
                "attribute": "token",
                "reflect": false,
                "defaultValue": "''"
            },
            "i18n": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "SiteHeaderI18N",
                    "resolved": "SiteHeaderI18N",
                    "references": {
                        "SiteHeaderI18N": {
                            "location": "import",
                            "path": "./pn-marketweb-siteheader-types",
                            "id": "src/components/layout-components/pn-marketweb-siteheader/pn-marketweb-siteheader-types.ts::SiteHeaderI18N"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "defaultValue": "{\n    searchlabel: '',\n    searchplaceholder: '',\n    searchbuttontext: '',\n    menuHomeButton: '',\n    menuGoBackButton: '',\n    mainMenuButton: '',\n    menuStartButton: '',\n    profileSelectionFlyoutHeading: '',\n    unifiedLoginButton: '',\n    unifiedMyPagesButton: '',\n  } as SiteHeaderI18N"
            },
            "siteUrl": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "site-url",
                "reflect": false,
                "defaultValue": "''"
            },
            "emitEvents": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "emit-events",
                "reflect": false,
                "defaultValue": "true"
            },
            "loginDialog": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "LoginDialog",
                    "resolved": "LoginDialog",
                    "references": {
                        "LoginDialog": {
                            "location": "import",
                            "path": "./pn-marketweb-siteheader-types",
                            "id": "src/components/layout-components/pn-marketweb-siteheader/pn-marketweb-siteheader-types.ts::LoginDialog"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "defaultValue": "null"
            },
            "fullname": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "User Fullname"
                },
                "attribute": "fullname",
                "reflect": false,
                "defaultValue": "''"
            },
            "loggedin": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "loggedin",
                "reflect": false,
                "defaultValue": "false"
            },
            "showProfileSelection": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If this is selected we will show the new profile selection dropdown instead of the login version"
                },
                "attribute": "show-profile-selection",
                "reflect": false,
                "defaultValue": "false"
            },
            "showUnifiedLogin": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "if this is selected we will show the unifiedlogin button along with new look for dropdown"
                },
                "attribute": "show-unified-login",
                "reflect": false,
                "defaultValue": "false"
            },
            "checkUserLoggedInStateInterval": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Continiously check if a user is logged in *"
                },
                "attribute": "check-user-logged-in-state-interval",
                "reflect": false,
                "defaultValue": "0"
            },
            "environment": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "environment",
                "reflect": false
            },
            "appearance": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "HeaderAppearance",
                    "resolved": "\"dark\" | \"light\"",
                    "references": {
                        "HeaderAppearance": {
                            "location": "import",
                            "path": "../pn-marketweb-header/utils/header-theme",
                            "id": "src/components/layout-components/pn-marketweb-header/utils/header-theme.ts::HeaderAppearance"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "appearance",
                "reflect": true,
                "defaultValue": "'light'"
            },
            "useMarketwebLogin": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "use-marketweb-login",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "loginLinks": {},
            "toggleButtonText": {},
            "username": {}
        };
    }
    static get events() {
        return [{
                "method": "loginStateChange",
                "name": "loginStateChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "hostElement"; }
    static get watchers() {
        return [{
                "propName": "i18n",
                "methodName": "init"
            }, {
                "propName": "loginDialog",
                "methodName": "init"
            }];
    }
}
//# sourceMappingURL=pn-marketweb-siteheader-login.js.map
