'use strict';

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

const spwDropdownItemCss = () => `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:block}.spw-dropdown-item{cursor:pointer;position:relative;display:flex;align-items:center;gap:8px;padding:8px 12px;color:var(--spw-colors-support-grey-grey-900);text-decoration:none;transition:background-color 0.2s ease;font-size:14px;border-radius:4px}.spw-dropdown-item:hover{background-color:var(--spw-colors-support-grey-grey-200)}.spw-dropdown-item:focus-visible{outline:2px solid white;outline-offset:0;box-shadow:0 0 0 4px black}.spw-dropdown-item--disabled{cursor:not-allowed;opacity:0.5;pointer-events:none}.spw-dropdown-item--has-sub-items{padding-right:32px}.spw-dropdown-item--sub-items-open{background-color:var(--spw-colors-support-grey-grey-100)}.spw-dropdown-item__icon{display:inline-flex;align-items:center;justify-content:center;width:20px;height:20px;font-size:14px;color:var(--spw-colors-support-grey-grey-700)}.spw-dropdown-item__content{flex:1;display:flex;align-items:center;justify-content:space-between;position:relative}.spw-dropdown-item__label{flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.spw-dropdown-item__toggle{position:absolute;right:-24px;top:50%;transform:translateY(-50%);width:20px;height:20px;display:inline-flex;align-items:center;justify-content:center;border-radius:50%;cursor:pointer;transition:background-color 0.2s ease, transform 0.2s ease}.spw-dropdown-item__toggle:hover{background-color:var(--spw-colors-support-grey-grey-300)}.spw-dropdown-item__toggle spw-icon{font-size:12px;color:var(--spw-colors-support-grey-grey-900);transition:transform 0.2s ease}.spw-dropdown-item__toggle--open spw-icon{transform:rotate(180deg)}.spw-dropdown-item__sub-items{padding-left:16px;margin-top:4px}.spw-dropdown-item__sub-items spw-dropdown-item{font-size:13px}`;

const SpwDropdownItem = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        /** Si true, l'item est désactivé */
        this.disabled = false;
        /** Variante d'icône font-awesome */
        this.iconVariant = 'fa-regular';
        this.hasSubItems = false;
        this.subItemsOpen = false;
        this.handleToggleSubItems = (event) => {
            event.preventDefault();
            event.stopPropagation();
            if (this.disabled)
                return;
            this.subItemsOpen = !this.subItemsOpen;
        };
        this.handleItemClick = (event) => {
            if (this.disabled)
                return;
            if (this.hasSubItems && !this.href) {
                event.preventDefault();
                event.stopPropagation();
                this.subItemsOpen = !this.subItemsOpen;
            }
        };
        this.handleItemKeyDown = (event) => {
            var _a;
            if (this.disabled)
                return;
            if (event.key === 'Enter' || event.key === ' ') {
                event.preventDefault();
                if (this.hasSubItems && !this.href) {
                    this.subItemsOpen = !this.subItemsOpen;
                }
                else if (this.href) {
                    (_a = this.el.querySelector('a')) === null || _a === void 0 ? void 0 : _a.click();
                }
            }
        };
    }
    componentDidLoad() {
        this.checkForSubItems();
        this.setupMutationObserver();
        setTimeout(() => {
            this.checkForSubItems();
        }, 0);
    }
    componentDidUpdate() {
        this.checkForSubItems();
    }
    disconnectedCallback() {
        if (this.mutationObserver) {
            this.mutationObserver.disconnect();
        }
    }
    setupMutationObserver() {
        this.mutationObserver = new MutationObserver(() => {
            this.checkForSubItems();
        });
        this.mutationObserver.observe(this.el, {
            childList: true,
            subtree: true,
        });
    }
    checkForSubItems() {
        const subItems = this.el.querySelector('[slot="sub-items"]');
        const newHasSubItems = subItems !== null && (subItems.children.length > 0 || subItems.childNodes.length > 0);
        if (this.hasSubItems !== newHasSubItems) {
            this.hasSubItems = newHasSubItems;
        }
    }
    get classes() {
        return {
            'spw-dropdown-item': true,
            'spw-dropdown-item--disabled': this.disabled,
            'spw-dropdown-item--has-sub-items': this.hasSubItems,
            'spw-dropdown-item--sub-items-open': this.subItemsOpen,
        };
    }
    render() {
        const Tag = this.href && !this.disabled ? 'a' : 'div';
        const toggleClasses = {
            'spw-dropdown-item__toggle': true,
            'spw-dropdown-item__toggle--open': this.subItemsOpen,
        };
        return (index.h(index.Host, { key: '9972c17813e0f7be9d07188a6a6596fc5f3e9b75' }, index.h(Tag, { key: '9048817b668d6f9ea4294f327af676b2128b2632', class: this.classes, href: this.href, "aria-disabled": this.disabled ? 'true' : undefined, role: "menuitem", tabindex: this.disabled ? -1 : 0, onClick: this.handleItemClick, onKeyDown: this.handleItemKeyDown }, this.icon && (index.h("div", { key: 'a43132e6f697d77849d158164bcdbe783b56ab21', class: "spw-dropdown-item__icon" }, index.h("spw-icon", { key: '5c8643b3c22b79c1a665c420edb9529453699341', variant: this.iconVariant, icon: this.icon }))), index.h("div", { key: '65db2295b5086d4e342234a336251ec282b9eebc', class: "spw-dropdown-item__content" }, index.h("div", { key: '54d0059dd24a51a136b0afee730cea8c25f02ecb', class: "spw-dropdown-item__label" }, index.h("slot", { key: '4e992a877d20ec0b84c83a8f5786eec8301a1e63' })), this.hasSubItems && (index.h("div", { key: 'f8a34e4a575ae202670cb86ec3095e0b7b717ed2', class: toggleClasses, onClick: this.handleToggleSubItems }, index.h("spw-icon", { key: '44593757e5e18c481ed0faab171adaf6129e29fe', icon: "fa-chevron-down" }))))), this.hasSubItems && (index.h("div", { key: 'fde76f69a90e770c732899d326c4880915b0baa7', class: "spw-dropdown-item__sub-items", style: { display: this.subItemsOpen ? 'block' : 'none' } }, index.h("slot", { key: '2b1f6087cd1063dd3a028b9ba2f091702f960290', name: "sub-items" })))));
    }
    get el() { return index.getElement(this); }
};
SpwDropdownItem.style = spwDropdownItemCss();

exports.spw_dropdown_item = SpwDropdownItem;
