import{html as t,css as e,LitElement as i}from"../../../lit/index.js";import{DDDSuper as r}from"../../d-d-d/d-d-d.js";import{store as a}from"../../haxcms-elements/lib/core/haxcms-site-store.js";import{HAXStore as o}from"../../hax-body/lib/hax-store.js";import{toJS as s}from"../../../mobx/dist/mobx.esm.js";export class PageAnchor extends(r(i)){constructor(){super(),this.value=null,this.target="",this.entityId=null}static get properties(){return{...super.properties,value:{type:String},target:{type:String,reflect:!0},entityId:{type:String,attribute:"entity-id"}}}static get styles(){return[super.styles,e`
        :host {
          display: inline-block;
          cursor: pointer;
        }
        simple-icon-lite {
          margin-right: var(--ddd-spacing-2);
          --simple-icon-height: var(--ddd-icon-xxs);
          --simple-icon-width: var(--ddd-icon-xxs);
        }
      `]}clickHandler(t){if(this._haxState&&"click"===t.type)return t.preventDefault(),t.stopPropagation(),t.stopImmediatePropagation(),!1;let e;e=this._haxState?o.activeHaxBody.querySelector(this.target):globalThis.document.querySelector(".haxcms-theme-element "+this.target),e?(e.scrollIntoView(),setTimeout((()=>{switch(e.tagName.toLowerCase()){case"video-player":case"audio-player":this.value?e.seek(parseInt(this.value)):e.play();break;case"play-list":e.slide=parseInt(this.value)}}),100)):this.target=null}getMatchFromFields(t,e,i="accentColor"){const r=s(a.entityData);if(r[t]&&r[t][i])return"accentColor"==i?`--simple-colors-default-theme-${r[t][i]}-3`:r[t][i];let l;if(l=this._haxState?o.activeHaxBody.querySelector(e):globalThis.document.querySelector(".haxcms-theme-element "+e),l&&e&&"accentColor"==i){let t=o.haxSchemaFromTag(l.tagName);if(t&&t.gizmo&&t.gizmo.color)return`--simple-colors-default-theme-${t.gizmo.color}-3`}return null}render(){let e;try{e=this.getMatchFromFields(this.entityId,this.target,"icon")}catch(t){}return t`<mark @click="${this.clickHandler}">
      ${e?t`<simple-icon-lite icon="${e}"></simple-icon-lite>`:""}
      <slot></slot>
    </mark>`}static get tag(){return"page-anchor"}static get haxProperties(){return new URL(`./${this.tag}.haxProperties.json`,import.meta.url).href}haxHooks(){return{editModeChanged:"haxeditModeChanged",setupActiveElementForm:"haxsetupActiveElementForm"}}haxeditModeChanged(t){this._haxState=t}haxsetupActiveElementForm(t){var e=[{text:"-- No association --",value:null}];o.activeHaxBody.querySelectorAll("[id],[resource]").forEach((t=>{if(!["PAGE-BREAK","PAGE-ANCHOR","RICH-TEXT-EDITOR-HIGHLIGHT"].includes(t.tagName)){let i=o.haxSchemaFromTag(t.tagName),r=t.gizmo?t.gizmo.title:"",a=t.tagName.toLowerCase();i.gizmo&&i.gizmo.metadata&&i.gizmo.metadata.anchorLabel?r=t[i.gizmo.metadata.anchorLabel]:(""!=t.innerText&&(r=t.innerText),t.getAttribute("id")?(r+=` (${t.getAttribute("id")})`,a=`#${t.getAttribute("id")}`):t.getAttribute("resource")&&(r+=` (${t.getAttribute("resource")})`,a=`[resource="${t.getAttribute("resource")}"]`)),e.push({text:r,value:a})}}));const i=s(a.entityData);var r=[{text:"-- No association --",value:null}];Object.keys(i).map((t=>{r.push({text:i[t].title,value:i[t].id})})),t.settings.configure.forEach(((i,a)=>{"target"===i.property&&(t.settings.configure[a].inputMethod="select",t.settings.configure[a].itemsList=e),"entityId"===i.property&&(t.settings.configure[a].inputMethod="select",t.settings.configure[a].itemsList=r)}))}}globalThis.customElements.define(PageAnchor.tag,PageAnchor);