/*!
 * EdGEL: The University of Edinburgh Design System v5.0.0 (https://design-system.ed.ac.uk/)
 * Copyright 2014-2023 The University of Edinburgh
 *
 * Bootstrap  v5.3.2 (https://getbootstrap.com/)
 * Copyright 2011-2023 The Bootstrap Authors
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
 */
import{_ as e,x as s}from"./base-2Isyko0t.js";import{o as t}from"./if-defined-66voXlkv.js";import{E as i,e as n}from"./collapse-zYCjoaZf.js";import"./edgel-icon.js";let a,r,l,o,h,u,d,c,m,b=e=>e;class p extends i{constructor(){super(),this.itemClass="uoe-nav-link",this.activeClass="uoe-nav-link-active",this.expanded=!1,this.controls="uoe-menu",this._breadcrumb=[],this._leaves=[],this._setMenuHtml=this._setMenuHtml.bind(this),this.observer=new MutationObserver(this._setMenuHtml)}_setMenuHtml(){"undefined"==typeof window&&(global.window={}),window.EdGEL||(window.EdGEL={});try{const e=this.shadowRoot?this.shadowRoot.host:this;this.menu=JSON.parse(e.textContent.trim()),window.EdGEL.menu=JSON.parse(JSON.stringify(this.menu));const s=new CustomEvent("updated.edgel.menu",{detail:{parts:Object.keys(this.menu)},bubbles:!0,composed:!0});this.dispatchEvent(s)}catch(e){if(!window.EdGEL.menu)throw new Error(`No valid menu supplied in the window.EdGEL.menu object or passed in the <${this.tagName.toLowerCase()}> tag.`);this.menu=JSON.parse(JSON.stringify(window.EdGEL.menu))}this.collapseDisabled=!0,this._validateMenu(),this._processMenu(),this.menuHtml=this._renderMenu(),this.collapseDisabled=!this.menuHtml,this.requestUpdate()}connectedCallback(){super.connectedCallback(),this._setMenuHtml(),this.observer.observe(this.shadowRoot.host,{characterData:!0,subtree:!0})}disconnectedCallback(){super.disconnectedCallback(),this.observer.disconnect()}_validateMenu(){if(!this.menu||!this.menu.breadcrumb)throw new Error("No breadcrumb supplied in the menu object.");if(!Array.isArray(this.menu.breadcrumb)||this.menu.breadcrumb.length<1)throw new Error("Breadcumb must be an array with at least one item.");return!0}_processMenu(){if(this._leaves=[],this._breadcrumb=JSON.parse(JSON.stringify(this.menu.breadcrumb)),this._breadcrumb.at(-1)._active=!0,this.menu.children&&Array.isArray(this.menu.children)&&this.menu.children.length)this._leaves=JSON.parse(JSON.stringify(this.menu.children));else if(this.menu.siblings&&Array.isArray(this.menu.siblings)&&this.menu.siblings.length){if(this._breadcrumb.length>1){const e=this._breadcrumb.pop();this._leaves=JSON.parse(JSON.stringify(this.menu.siblings));const s=this._leaves.find((s=>s.url===e.url));s?s._active=!0:this._leaves.unshift(e)}}else this._breadcrumb.length>2&&(this._leaves=[this._breadcrumb.pop()])}_nestedList(e,i=0){const n=e.shift();if(!e.length)return n;let r;return n._classes=[],i?i&&1===e.length&&e[0]?n._classes.push("uoe-nav-link-joint"):e.length>1&&n._classes.push("uoe-nav-link-ancestor"):(n._classes.push("uoe-nav-link-root"),r=this.controls),s(a||(a=b`
      <ul id=${0}><li>
        ${0}
        ${0}
      </li></ul>
    `),t(r),this._itemLink(n),this._nestedList(e,++i))}_lockSuffix(e){return e?s(r||(r=b`
        <edgel-icon name="lock-fill"></edgel-icon>
        <span class="visually-hidden"> (${0})</span>
      `),"string"==typeof e?e:"protected resource"):s(l||(l=b``))}_itemLink({url:e,text:i,restricted:n,_active:a,_classes:r=[],_attrs:l={}}){r.unshift(this.itemClass),a&&r.push(this.activeClass);const h=this._lockSuffix(n);return s(o||(o=b`
      <a
          href=${0}
          class=${0}
          aria-current=${0}
          id=${0}
          role=${0}
          aria-expanded=${0}
          @click=${0}
          data-bs-toggle=${0}
      >
        ${0}
        ${0}
      </a>
    `),e,r.join(" "),t(a?"page":void 0),t(l.id),t(l.role),t(l.expanded),t(l.click),t(l.toggle),i,h)}_renderMenu(){if(!this._breadcrumb.length)return s(h||(h=b``));let e="";const t=this._breadcrumb.length>1?"uoe-nav-link-leaf":"uoe-nav-link-child";if(this._leaves.length){const i=[];for(const e of this._leaves)e._classes=[t],i.push(s(u||(u=b`<li>${0}</li>`),this._itemLink(e)));e=s(d||(d=b`<ul>${0}</ul>`),i)}const i=JSON.parse(JSON.stringify(this._breadcrumb));return i.push(e),this._nestedList(i)}render(){if(this.collapseDisabled)return;const e={hamburger:!0,"hamburger-expanded":this.expanded},t=s(c||(c=b`
      <span class=${0} aria-hidden="true"></span>
      <span class="visually-hidden">Toggle Navigation</span> Menu
    `),n(e)),i=this.buttonHtml(t,"btn-uoe-nav-toggler");return s(m||(m=b`
      <span class="edgel-js">
        <nav aria-label="Content navigation" class="uoe-nav">
          ${0}
          ${0}
        </nav>
      </span>
    `),i,this.menuHtml)}}e(p,"properties",{expanded:{type:Boolean,attribute:!1}});export{p as E};
