import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
import { s as state, d as defineCustomElement$1 } from './pn-marketweb-siteheader-login-linklist2.js';
import { a as arrow_right } from './arrow_right.js';

const pnMarketwebSiteheaderLoginProfileselectionCss = "pn-button-dropdown .pn-button-dropdown>pn-button>.pn-button{min-height:initial}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}@media screen and (max-width: 767px){.sr-only-mobile{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}}pn-marketweb-siteheader-login-profileselection{--login-profileselection-heading-color:var(--siteheader-login-profileselection-heading-color,\n      inherit);--login-profileselection-description-color:var(--siteheader-login-profileselection-description-color,\n      #5e554a);--login-profileselection-user-name-color:var(--siteheader-login-profileselection-user-name-color,\n      inherit);--login-profileselection-divider-background:var(--siteheader-login-profileselection-divider-background,\n      #00a0d6);--login-profileselection-profileoption-link-background:var(--siteheader-login-profileselection-profileoption-link-background,\n      #ffffff);--login-profileselection-profileoption-link-color:var(--siteheader-login-profileselection-profileoption-link-color,\n      #005d92);--login-profileselection-profileoption-link-hover-background:var(--siteheader-login-profileselection-profileoption-link-hover-background,\n      #f3f2f2);--login-profileselection-logout-link-color:var(--siteheader-login-profileselection-logout-link-color,\n      #005d92)}.pn-marketweb-siteheader-login-profileselection-heading{display:block;padding:0.5rem 0 1rem;color:var(--login-profileselection-heading-color)}.pn-marketweb-siteheader-login-profileselection-user,.pn-marketweb-siteheader-login-profileselection-currentprofile{margin-top:1rem;margin-bottom:1rem}.pn-marketweb-siteheader-login-profileselection-user-name,.pn-marketweb-siteheader-login-profileselection-currentprofile-name{line-height:1;color:var(--login-profileselection-user-name-color)}.pn-marketweb-siteheader-login-profileselection-user-description,.pn-marketweb-siteheader-login-profileselection-currentprofile-description{font-weight:400;white-space:normal;word-break:break-all;color:var(--login-profileselection-description-color);font-size:0.75em}.pn-marketweb-siteheader-login-profileselection-divider{position:relative;width:100%;height:1rem;background:var(--login-profileselection-divider-background);margin-left:-1em;margin-top:1.6rem;margin-bottom:1.6rem;border-radius:0 0.6em 0.6em 0;padding:0}.pn-marketweb-siteheader-login-profileselection-divider::after{content:\"\";position:absolute;right:0;top:0;width:0.6em;height:0.6em;border-radius:50%;background:inherit;transform:translateX(calc(100% + 0.2em))}.pn-marketweb-siteheader-login-profileselection-profileoption-link{display:flex;align-items:center;margin:0 -1em;padding:0.6rem 1.6rem;background:var(--login-profileselection-profileoption-link-background);color:var(--login-profileselection-profileoption-link-color);text-decoration:none;transition:background 0.15s ease-in-out}.pn-marketweb-siteheader-login-profileselection-profileoption-link:hover,.pn-marketweb-siteheader-login-profileselection-profileoption-link:focus,.pn-marketweb-siteheader-login-profileselection-profileoption-link:active{background:var(--login-profileselection-profileoption-link-hover-background)}.pn-marketweb-siteheader-login-profileselection-profileoption-content{flex-grow:1}.pn-marketweb-siteheader-login-profileselection-profileoption-name{font-weight:700}.pn-marketweb-siteheader-login-profileselection-profileoption-description{font-weight:400;white-space:normal;word-break:break-all;color:var(--login-profileselection-description-color);font-size:0.75em}.pn-marketweb-siteheader-login-profileselection-logout{width:100%;padding:1.6rem;text-align:center}.pn-marketweb-siteheader-login-profileselection-logout-link{display:inline-block;font-weight:500;color:var(--login-profileselection-logout-link-color);text-decoration:none}.pn-marketweb-siteheader-login-profileselection-logout-link:hover,.pn-marketweb-siteheader-login-profileselection-logout-link:focus,.pn-marketweb-siteheader-login-profileselection-logout-link:active{text-decoration:underline}";
const PnMarketwebSiteheaderLoginProfileselectionStyle0 = pnMarketwebSiteheaderLoginProfileselectionCss;

const PnMarketwebSiteheaderLoginProfileselection = /*@__PURE__*/ proxyCustomElement(class PnMarketwebSiteheaderLoginProfileselection extends HTMLElement {
    constructor() {
        super();
        this.__registerHost();
        this.loginDialog = null;
        this.endpoint = '';
        this.loggedin = false;
        this.idNamespace = '';
        this.heading = null;
        this.i18n = {
            searchlabel: '',
            searchplaceholder: '',
            searchbuttontext: '',
            menuHomeButton: '',
            menuGoBackButton: '',
            mainMenuButton: '',
            menuStartButton: '',
            profileSelectionFlyoutHeading: '',
        };
        this.user = null;
        this.logoutLink = null;
        this.userName = null;
        this.userEmail = '';
        this.currentProfile = null;
        this.profileoptions = [];
    }
    get hostElement() { return this; }
    componentWillLoad() {
        if (this.heading === null && this.i18n?.profileSelectionFlyoutHeading) {
            this.heading = this.i18n.profileSelectionFlyoutHeading;
        }
        this.init();
    }
    init() {
        this.updateUserInfo();
        this.adjustLoginLinks();
        this.fetchProfiles();
    }
    async fetchProfiles() {
        if (!this.loggedin || this.profileoptions.length > 0) {
            return;
        }
        let apiUrl = `${this.endpoint}${this.endpoint.lastIndexOf('/') > -1 && this.endpoint.lastIndexOf('/') === this.endpoint.length - 1 ? '' : '/'}api/user/parentorganizations`;
        const req = await fetch(apiUrl);
        const reqdata = await req.json();
        const fallbackData = {
            loggedin: true,
            organizations: [
                {
                    name: 'Forsheda Livs AB',
                    customerNumber: '5564513603',
                    id: 'dbba867d-1dff-3dbb-b420-c57b4b828b73',
                    selected: true,
                    profiles: [
                        {
                            name: 'Forsheda Livs AB',
                            customerNumber: '20685482',
                            id: 'f757ba28-9a73-15a2-f128-4b394d970f3f',
                            ssid: '',
                            selected: false,
                            customerGroup: 'Business',
                        },
                        {
                            name: 'ICA MAXI KUNGSBACKA',
                            customerNumber: '20831191',
                            id: '5aea41df-32b0-3f7e-2946-78598360177d',
                            ssid: '259686',
                            selected: true,
                            customerGroup: 'Agent',
                        },
                    ],
                },
                {
                    name: 'Hem Livs i Kungsbacka AB',
                    customerNumber: '5565271250',
                    id: '97ca363c-40ef-cca9-7a3c-9e7f025567c2',
                    selected: false,
                    profiles: [
                        {
                            name: 'ICA KVANTUM KUNGSBACKA',
                            customerNumber: '20831193',
                            id: 'fbd61ffd-41c6-2206-b88e-cfef91e1dc39',
                            ssid: '259691',
                            selected: false,
                            customerGroup: 'Agent',
                        },
                    ],
                },
                {
                    name: 'ICA SUPERMARKET TORGET',
                    customerNumber: '5568902679',
                    id: '7cb76a94-e1a5-5f57-58fd-bc563851b28e',
                    selected: false,
                    profiles: [
                        {
                            name: 'ICA SUPERMARKET TORGET',
                            customerNumber: '20831212',
                            id: '3a2db017-b68f-6fa0-fe70-542c08cd97ce',
                            ssid: '',
                            selected: false,
                            customerGroup: 'Agent',
                        },
                    ],
                },
            ],
        };
        const data = window.location.href.indexOf('localhost:6008') !== -1 ? fallbackData : reqdata;
        if (!data || !data.organizations || !data.organizations.length) {
            this.heading = ""; //this is to not get "logged in as" text when its private account
            return;
        }
        data.organizations.forEach(org => {
            if (org.selected === true && org.profiles) {
                const selectedProfile = org.profiles.filter(x => x.selected === true);
                if (selectedProfile && selectedProfile[0] && selectedProfile[0].customerGroup.toLocaleLowerCase() === 'agent' && selectedProfile[0].ssid) {
                    org.customerNumber = selectedProfile[0].ssid;
                }
            }
            else {
                if (org.customerNumber.length === 10 && org.customerNumber.substring(0, 2) === '55') {
                    org.customerNumber = org.customerNumber.substring(0, 6) + '-' + org.customerNumber.substring(6);
                }
                this.profileoptions.push(org);
            }
        });
        let currentProfileObj = data.organizations.filter(x => x.selected === true)[0];
        const userProfileOption = {
            name: this.userName,
            customerNumber: '',
            id: '',
            selected: !currentProfileObj,
            profiles: [],
        };
        if (!currentProfileObj) {
            currentProfileObj = userProfileOption;
        }
        if (currentProfileObj && currentProfileObj.name) {
            this.currentProfile = currentProfileObj;
        }
        this.profileoptions.push(userProfileOption);
    }
    adjustLoginLinks() {
        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 = state.loginManager.getLogoutUrl(link.href);
            }
            if (i === 0 && !link.href) {
                link.href = state.loginManager.getLoginUrl();
                link.linkType = 'primary';
            }
            if (link.primaryLinkApperance && !link.linkType) {
                link.linkType = 'primary';
            }
        };
        if (this.loginDialog?.loggedInLinks) {
            this.loginDialog?.loggedInLinks.forEach(adjustLink);
        }
        this.logoutLink = this.loginDialog?.loggedInLinks.filter(x => x.isLogoutLink)[0] ?? null;
    }
    updateUserInfo() {
        const userInfo = state.loginManager.getUserInfo();
        this.user = userInfo;
        if (!userInfo) {
            return;
        }
        if (userInfo && userInfo.email) {
            this.userEmail = userInfo.email;
        }
        if (userInfo && userInfo.given_name) {
            this.userName = userInfo.given_name + (userInfo?.family_name ? ` ${userInfo?.family_name}` : '');
        }
        if (userInfo && userInfo.given_name) {
            this.userName = userInfo.given_name;
            return;
        }
        if (userInfo && userInfo.family_name) {
            this.userName = userInfo.family_name;
            return;
        }
        if (userInfo && userInfo.username) {
            this.userName = userInfo.username;
            return;
        }
    }
    filterLogoutLink(loggedInLinks) {
        return loggedInLinks.filter((link) => !link.isLogoutLink);
    }
    render() {
        if (!this.loginDialog) {
            return;
        }
        let hostElementAttribute = {};
        return (h(Host, { ...hostElementAttribute }, this.heading ? h("strong", { class: "pn-marketweb-siteheader-login-profileselection-heading" }, this.heading) : null, this.currentProfile !== null && this.currentProfile.name ? (h("div", { class: "pn-marketweb-siteheader-login-profileselection-currentprofile" }, h("div", { class: "pn-marketweb-siteheader-login-profileselection-currentprofile-name" }, this.currentProfile.name), h("div", { class: "pn-marketweb-siteheader-login-profileselection-currentprofile-description" }, this.currentProfile.customerNumber ? this.currentProfile.customerNumber : this.userEmail))) : null, h("pn-marketweb-siteheader-login-linklist", { links: this.filterLogoutLink(this.loginDialog.loggedInLinks), idNamespace: this.idNamespace }), h("div", { class: "pn-marketweb-siteheader-login-profileselection-divider" }), this.profileoptions
            .filter(x => x.selected !== true)
            .map(organization => {
            let customerNumber = organization.profiles.length > 0 ? organization.profiles[0].customerNumber : organization.customerNumber;
            return (h("div", { class: "pn-marketweb-siteheader-login-profileselection-profileoption" }, h("a", { href: `${this.endpoint}/api/user/setprofile?customernumber=${customerNumber}&returnUrl=${encodeURIComponent(window.location.href)}`, class: "pn-marketweb-siteheader-login-profileselection-profileoption-link" }, h("div", { class: "pn-marketweb-siteheader-login-profileselection-profileoption-content" }, h("div", { class: "pn-marketweb-siteheader-login-profileselection-profileoption-name" }, organization.name), h("div", { class: "pn-marketweb-siteheader-login-profileselection-profileoption-description" }, organization.customerNumber ? organization.customerNumber : this.userEmail)), h("pn-icon", { icon: arrow_right, small: "false", color: "blue700" }))));
        }), this.logoutLink ? (h("div", { class: "pn-marketweb-siteheader-login-profileselection-logout" }, h("a", { href: this.logoutLink.href, rel: "nofollower noopener", id: "pn-marketweb-header-logoutlink", onClick: () => state.loginManager.clearUserData(), class: "pn-marketweb-siteheader-login-profileselection-logout-link" }, this.logoutLink.linkText))) : null));
    }
    static get style() { return PnMarketwebSiteheaderLoginProfileselectionStyle0; }
}, [0, "pn-marketweb-siteheader-login-profileselection", {
        "loginDialog": [1040],
        "endpoint": [1],
        "loggedin": [4],
        "idNamespace": [1, "id-namespace"],
        "heading": [1],
        "i18n": [16],
        "currentProfile": [1040],
        "profileoptions": [1040],
        "user": [32],
        "logoutLink": [32],
        "userName": [32],
        "userEmail": [32]
    }]);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["pn-marketweb-siteheader-login-profileselection", "pn-marketweb-siteheader-login-linklist"];
    components.forEach(tagName => { switch (tagName) {
        case "pn-marketweb-siteheader-login-profileselection":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, PnMarketwebSiteheaderLoginProfileselection);
            }
            break;
        case "pn-marketweb-siteheader-login-linklist":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}

export { PnMarketwebSiteheaderLoginProfileselection as P, defineCustomElement as d };

//# sourceMappingURL=pn-marketweb-siteheader-login-profileselection2.js.map