'use strict';

var index = require('./index-DuCXzb-X.js');

const spwHeaderNavigationDropdownCss = () => `html{line-height:1.3;-webkit-text-size-adjust:100%;}h1,h2,h3,h4,h5,h6,p,ul,li{padding:0;margin:0}ul{list-style:none}:host{box-sizing:border-box;line-height:1.3;-webkit-text-size-adjust:100%;font-size:100%}:host *,:host *::before,:host *::after{box-sizing:inherit;font-family:var(--spw-ds-font-main)}body{margin:0}hr{box-sizing:content-box;height:0;overflow:visible;}pre{font-family:monospace, monospace;font-size:1em;}a{background-color:transparent}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted;}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace, monospace;font-size:1em;}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-0.25em}sup{top:-0.5em}img{border-style:none}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;margin:0;}button,input{overflow:visible}button,select{text-transform:none}button,[type=button],[type=reset],[type=submit]{-webkit-appearance:button}button::-moz-focus-inner,[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner{border-style:none;padding:0}button:-moz-focusring,[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:0.35em 0.75em 0.625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal;}progress{vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0;}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px;}[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit;}details{display:block}summary{display:list-item}template{display:none}[hidden]{display:none}:root{--spw-ds-container-xl:1232px;--spw-ds-container-lg:1024px;--spw-ds-container-md:768px;--spw-ds-container-sm:640px}.spw-ds-container{width:100%;padding-left:16px;padding-right:16px;margin-left:auto;margin-right:auto}@media (min-width: 460px){.spw-ds-container{max-width:var(--spw-ds-container-sm)}}@media (min-width: 768px){.spw-ds-container{max-width:var(--spw-ds-container-md)}}@media (min-width: 1024px){.spw-ds-container{max-width:var(--spw-ds-container-lg)}}@media (min-width: 1350px){.spw-ds-container{max-width:var(--spw-ds-container-xl)}}:host{display:contents}.spw-header-navigation-dropdown--is-megamenu{display:contents}@media (min-width: 1024px){.spw-header-navigation-dropdown--is-megamenu{width:100%;max-width:var(--spw-ds-container-lg);margin:0 auto;display:flex;flex-wrap:wrap;padding:16px 0}}@media (min-width: 1350px){.spw-header-navigation-dropdown--is-megamenu{max-width:var(--spw-ds-container-xl)}}@media (min-width: 1024px){:host(.spw-header-navigation-dropdown--is-megamenu) ::slotted(spw-header-navigation-item[data-level="3"]:nth-child(n+8)){display:none}}.spw-header-navigation-item--view-more{display:none !important}@media (min-width: 1024px){.spw-header-navigation-item--view-more{display:block !important;padding:8px 16px;border-left:1px solid var(--spw-colors-support-grey-grey-300);font-size:14px}}`;

const SpwHeaderNavigationDropdown = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        /** Texte du bouton "Voir plus" (uniquement en mode megamenu) */
        this.viewMoreText = 'Voir plus';
        this.shouldShowViewMore = false;
        this.parentHref = '#';
        this.parentTarget = '_self';
        this.dropdownLevel = 1;
        this.isMegamenu = false;
    }
    componentDidLoad() {
        this.calculateDropdownLevel();
        this.isMegamenu = this.isParentMegamenu();
        this.checkIfShouldShowViewMore();
        const slot = this.el.shadowRoot.querySelector('slot');
        if (slot) {
            const mutationObserver = new MutationObserver(() => {
                this.checkIfShouldShowViewMore();
            });
            mutationObserver.observe(slot, { childList: true, subtree: true });
        }
    }
    calculateDropdownLevel() {
        let level = 1;
        let parent = this.el.parentElement;
        while (parent && parent.tagName !== 'SPW-HEADER-NAVIGATION') {
            if (parent.tagName === 'SPW-HEADER-NAVIGATION-DROPDOWN') {
                level++;
            }
            parent = parent.parentElement;
        }
        this.dropdownLevel = level;
    }
    checkIfShouldShowViewMore() {
        if (this.dropdownLevel !== 2) {
            this.shouldShowViewMore = false;
            return;
        }
        if (!this.isMegamenu) {
            this.shouldShowViewMore = false;
            return;
        }
        const slot = this.el.shadowRoot.querySelector('slot');
        if (!slot)
            return;
        const assignedElements = slot.assignedElements({ flatten: true });
        const navItems = Array.from(assignedElements).filter(el => el.tagName === 'SPW-HEADER-NAVIGATION-ITEM');
        const level3Items = navItems.filter(item => {
            const level = item.getAttribute('data-level');
            return level === '3';
        });
        this.getParentLink();
        this.shouldShowViewMore = level3Items.length > 7 || level3Items.length === 0;
    }
    isParentMegamenu() {
        let parent = this.el.parentElement;
        while (parent) {
            if (parent.tagName === 'SPW-HEADER-NAVIGATION-ITEM') {
                const level = parent.getAttribute('data-level');
                if (level === '1') {
                    return parent.getAttribute('variant') === 'megamenu';
                }
            }
            parent = parent.parentElement;
        }
        return false;
    }
    getParentLink() {
        let parent = this.el.parentElement;
        while (parent) {
            if (parent.tagName === 'SPW-HEADER-NAVIGATION-ITEM') {
                const link = parent.querySelector('a');
                if (link) {
                    this.parentHref = link.getAttribute('href') || '#';
                    this.parentTarget = link.getAttribute('target') || '_self';
                }
                break;
            }
            parent = parent.parentElement;
        }
    }
    render() {
        return (index.h(index.Host, { key: 'ba7103f29234e8bae802d8aecf08f942b9fa5481', class: { 'spw-header-navigation-dropdown--is-megamenu': this.isMegamenu } }, index.h("div", { key: '7371574a8be7084a053cf9fa91eb5c5239fd0769', class: "spw-header-navigation-dropdown" }, index.h("slot", { key: '1609aa6db756e127e8cd5ca954fee0c4c0b63486' }), this.shouldShowViewMore && (index.h("spw-link", { key: '226401939eb4327ebe413c73860e58439fb7e7c7', icon: "fa-arrow-right", class: "spw-header-navigation-item--view-more", href: this.parentHref, target: this.parentTarget }, this.viewMoreText)))));
    }
    get el() { return index.getElement(this); }
};
SpwHeaderNavigationDropdown.style = spwHeaderNavigationDropdownCss();

exports.spw_header_navigation_dropdown = SpwHeaderNavigationDropdown;
