import{html as e,css as t,nothing as o}from"../../../lit/index.js";import{SimpleFilterMixin as i}from"../../simple-filter/simple-filter.js";import{I18NMixin as s}from"../../i18n-manager/lib/I18NMixin.js";import{SimpleColors as r}from"../../simple-colors/simple-colors.js";import{UserScaffoldInstance as a}from"../../user-scaffold/user-scaffold.js";import{autorun as n,toJS as l}from"../../../mobx/dist/mobx.esm.js";import"../../../@lit-labs/virtualizer/lit-virtualizer.js";import"./super-daemon-row.js";import"./super-daemon-search.js";export class SuperDaemonUI extends(i(s(r))){constructor(){super(),this.focused=!1,this.like="",this.voiceSearch=!1,this.iconAccent="purple",this.multiMatch=!0,this.t=this.t||{},this.t={...this.t,noResultsForThisTerm:this._defaultTextEmpty,voiceSearch:"Voice search",results:"Results",loadingResults:"Loading results"},this.opened=!1,this.items=[],this.mini=!1,this.wand=!1,this.loading=!0,this.listeningForInput=!1,this.programSearch="",this.commandContext="*",this.programName=null,this.shadowRootOptions={...r.shadowRootOptions,delegatesFocus:!0},this.activeDrag=!1,this.activeType=null,this.where="index",this.icon="hardware:keyboard-return",n((()=>{const e=l(a.action),t=l(a.data),o=globalThis.SuperDaemonManager.requestAvailability();a.active&&a.memory.isLoggedIn&&null===o.programName&&"drag"===e.type?(this.activeDrag=!0,this.activeType=t.value||t.architype):a.active&&a.memory.isLoggedIn&&null===o.programName&&"dragleave"===e.type&&(this.activeDrag=!1,this.activeType=null)}))}static get tag(){return"super-daemon-ui"}static get properties(){return{...super.properties,icon:{type:String},iconAccent:{type:String,attribute:"icon-accent"},voiceSearch:{type:Boolean,reflect:!0,attribute:"voice-search"},listeningForInput:{type:Boolean,reflect:!0,attribute:"listening-for-input"},activeDrag:{type:Boolean},activeType:{type:String},mini:{type:Boolean,reflect:!0},wand:{type:Boolean,reflect:!0},loading:{type:Boolean,reflect:!0},programSearch:{type:String,attribute:"program-search"},programName:{type:String,attribute:"program-name"},commandContext:{type:String,attribute:"command-context"},opened:{type:Boolean,reflect:!0},focused:{type:Boolean,reflect:!0}}}static get styles(){let e=[];return super.styles&&(e=super.styles),[e,t`
        :host {
          display: block;
        }
        super-daemon-search {
          display: flex;
          margin: 16px;
        }
        :host([wand]) super-daemon-search {
          margin: -24px 0 0 0;
          height: 48px;
        }
        .voice {
          --simple-icon-height: 50px;
          --simple-icon-width: 100px;
          --simple-icon-button-border-radius: 0;
          color: var(--simple-colors-default-theme-grey-10, grey);
          transition: color 0.6s ease-in-out;
        }
        :host([mini]) .voice {
          --simple-icon-height: 24px;
          --simple-icon-width: 24px;
        }
        .voice:hover,
        .voice:focus {
          color: var(--simple-colors-default-theme-purple-6, purple);
        }
        .voice.listening {
          color: var(--simple-colors-default-theme-purple-4, purple);
        }

        .search .user-context-icon {
          display: inline-flex;
          --simple-icon-height: 50px;
          --simple-icon-width: 30px;
        }
        :host([mini]) .search .user-context-icon {
          --simple-icon-height: 24px;
          --simple-icon-width: 24px;
          margin-top: 0px;
        }
        .loading {
          font-size: 12px;
          font-style: italic;
          margin: 16px;
        }
        .results-stats {
          font-size: 12px;
          color: var(--simple-colors-default-theme-grey-10, black);
          padding: 8px;
          float: right;
        }
        :host([focused][wand]) .results {
          display: block;
        }
        .results {
          width: 100%;
          padding: 16px 0px;
        }
        :host([mini]) .results lit-virtualizer {
          max-height: unset;
          height: unset;
        }
        .results lit-virtualizer {
          max-height: 50vh;
          width: 100%;
          display: block;
          height: 50vh;
          border: 2px solid var(--simple-colors-default-theme-grey-10, black);
        }
        .results super-daemon-row {
          scroll-snap-align: start;
          scroll-snap-stop: always;
          width: -webkit-fill-available;
        }
        .no-results {
          font-size: 32px;
          font-weight: bold;
          word-break: break-all;
          overflow: hidden;
          line-height: 32px;
          margin: 32px;
          border: 1px solid transparent;
          box-shadow: none;
          outline: 0px;
        }
        .slotted {
          display: block;
          font-size: 12px;
          line-height: 18px;
        }
        .slotted ::slotted(a) {
          color: var(--simple-colors-default-theme-grey-8, blue);
          font-weight: bold;
          text-decoration: underline;
          cursor: pointer;
        }
        :host([mini]) .no-results {
          margin: 16px;
        }

        @media screen and (max-width: 800px) {
          .voice {
            --simple-icon-height: 30px;
            --simple-icon-width: 30px;
          }
          super-daemon-search {
            margin: 8px;
          }
          .results-stats {
            display: none;
          }
          .results {
            padding: 0px;
          }
          super-daemon-row {
            --super-daemon-row-icon: 30px;
            margin: 4px;
          }

          super-daemon-row::part(label-wrap) {
            min-width: 70%;
          }
          super-daemon-row::part(button) {
            padding: 4px;
          }
          super-daemon-row::part(action) {
            font-size: 24px;
            line-height: 24px;
            height: 24px;
            max-width: unset;
          }
          super-daemon-row::part(tags) {
            display: none;
          }
          super-daemon-row::part(path) {
            font-size: 12px;
          }
        }
        :host([mini]) {
          color: var(--simple-colors-default-theme-grey-12, black);
          background-color: var(--simple-colors-default-theme-grey-1, white);
        }
        :host([mini]) super-daemon-row {
          --super-daemon-row-icon: 24px;
          border-radius: 0px;
          margin: 4px;
        }
        :host([mini]) .results-stats {
          display: none;
        }
        :host([mini]) .results {
          padding: 0;
        }
      `]}updated(e){super.updated&&super.updated(e),e.forEach(((e,t)=>{if("filtered"==t&&void 0!==e){this.filtered.length>0&&(this.loading=!1);const e=globalThis.SuperDaemonManager.requestAvailability();(e.santaMode||this.listeningForInput)&&(clearTimeout(this._selectTimeout),this._selectTimeout=setTimeout((()=>{(1===this.filtered.length||this.filtered&&this.filtered[0]&&this.filtered[0].title.toLocaleLowerCase()==e.value.toLocaleLowerCase())&&(this.shadowRoot.querySelector("super-daemon-row").selected(),e.listeningForInput=!0)}),600))}"opened"==t&&this.shadowRoot&&(this.opened?(this.activeType=null,this.activeDrag=!1,this.focusInput(),this.mini&&!this.wand&&(globalThis.document.body.style.overflow="hidden",this.shadowRoot.querySelector(".results").scrollTo(0,0))):globalThis.document.body.style.overflow=""),"commandContext"==t&&this.dispatchEvent(new CustomEvent("super-daemon-command-context-changed",{detail:{value:this[t]}})),"like"==t&&this.dispatchEvent(new CustomEvent("like-changed",{detail:{value:this[t]}}))}))}focusInput(){this.shadowRoot.querySelector("super-daemon-search").focusInput()}selectInput(){this.shadowRoot.querySelector("super-daemon-search").selectInput()}setupProgram(){this.programSearch="",this.focusInput(),this.selectInput(),this.shadowRoot.querySelector(".results").scrollTo(0,0)}itemSelected(e){this.like="",this.programSearch=""}_resultsKeydown(e){if(this.filtered.length>0&&this.shadowRoot.querySelector("super-daemon-row[active]"))switch(e.key){case"ArrowUp":case"ArrowLeft":this.shadowRoot.querySelector("super-daemon-row[active]")===this.shadowRoot.querySelector("super-daemon-row")?(this.shadowRoot.querySelector("lit-virtualizer").scrollToIndex(this.filtered.length-1,"center"),requestAnimationFrame((()=>{this.shadowRoot.querySelector("super-daemon-row:last-of-type").focus()}))):this.shadowRoot.querySelector("super-daemon-row[active]").previousElementSibling.focus();break;case"ArrowDown":case"ArrowRight":this.shadowRoot.querySelector("super-daemon-row[active]")===this.shadowRoot.querySelector("super-daemon-row:last-of-type")?(this.shadowRoot.querySelector("lit-virtualizer").scrollToIndex(0,"center"),requestAnimationFrame((()=>{this.shadowRoot.querySelector("super-daemon-row").focus()}))):this.shadowRoot.querySelector("super-daemon-row[active]").nextElementSibling.focus()}}focusedChanged(e){this.focused=e.detail.value}inputfilterChanged(e){this.programName?this.programSearch=e.target.value:this.like=e.target.value}listeningForInputChanged(e){e.detail.value&&this.shadowRoot.querySelector(".results").scrollTo(0,0)}commandContextChanged(e){this.commandContext=e.detail.value}_inputKeydown(e){if(this.filtered.length>0)switch(e.key){case"Enter":this.shadowRoot.querySelector("super-daemon-row").selected();break;case"ArrowUp":this.shadowRoot.querySelector("lit-virtualizer").scrollToIndex(this.filtered.length-1,"center"),requestAnimationFrame((()=>{this.shadowRoot.querySelector("super-daemon-row:last-of-type").focus()}));break;case"ArrowDown":this.shadowRoot.querySelector("lit-virtualizer").scrollToIndex(0,"center"),requestAnimationFrame((()=>{this.shadowRoot.querySelector("super-daemon-row").focus()}))}switch(e.key){case"!":case"/":case"\\":case">":case"<":"\\"===e.key&&""==this.like||"!"===e.key&&""==this.like?(this.commandContext="/",e.preventDefault()):"<"===e.key&&""==this.like?(this.commandContext=">",e.preventDefault()):""==this.like&&(this.commandContext=e.key,e.preventDefault());break;case"Backspace":""==this.programSearch&&this.programName?(this.dispatchEvent(new CustomEvent("super-daemon-run-program",{bubbles:!0,cancelable:!0,composed:!0,detail:!1})),e.preventDefault()):!this.programName&&""==this.like&&this.commandContext&&(this.commandContext="*",e.preventDefault())}}dropEvent(e){e.preventDefault(),this.activeDrag=!1,this.activeType=null;globalThis.SuperDaemonManager.requestAvailability().waveWand(["","/",e,"hax-agent","Agent"],this.shadowRoot.querySelector("#merlin"),"coin2")}dragenterEvent(e){e.preventDefault(),this.shadowRoot.querySelector("super-daemon-search").dragover=!0}dragoverEvent(e){e.preventDefault(),this.shadowRoot.querySelector("super-daemon-search").dragover=!0}dragleaveEvent(e){e.preventDefault(),this.shadowRoot.querySelector("super-daemon-search").dragover=!1}render(){return e`
      <super-daemon-search
        @keydown="${this._inputKeydown}"
        @focused-changed="${this.focusedChanged}"
        @value-changed="${this.inputfilterChanged}"
        @command-context-changed="${this.commandContextChanged}"
        @listening-for-input-changed="${this.listeningForInputChanged}"
        @drop="${this.dropEvent}"
        @dragenter="${this.dragenterEvent}"
        @dragleave="${this.dragleaveEvent}"
        @dragover="${this.dragoverEvent}"
        icon="${this.icon}"
        icon-accent="${this.iconAccent}"
        value="${this.like}"
        ?voice-search="${this.voiceSearch}"
        ?mini="${this.mini}"
        ?wand="${this.wand}"
        ?loading="${this.loading}"
        program-search="${this.programSearch}"
        ?listening-for-input="${this.listeningForInput}"
        command-context="${this.commandContext}"
        droppable-type="${this.activeType}"
        ?droppable="${this.activeDrag}"
      >
      </super-daemon-search>
      <div
        class="results"
        @keydown="${this._resultsKeydown}"
        @super-daemon-row-selected="${this.itemSelected}"
      >
        ${this.loading?e`<div class="loading">${this.t.loadingResults}..</div>`:e`
              ${this.filtered.length&&0!==this.filtered.length?e`
                    <lit-virtualizer
                      scroller
                      .items=${this.filtered}
                      .renderItem=${(t,i)=>t?e`<super-daemon-row
                              data-row-index="${i}"
                              .value="${t.value}"
                              icon="${t.icon}"
                              image="${t.image}"
                              ?dark="${this.dark}"
                              text-character="${t.textCharacter}"
                              title="${t.title}"
                              .tags="${t.tags}"
                              event-name="${t.eventName}"
                              path="${t.path}"
                              ?more="${t.more&&(!this.mini||this.wand)}"
                              ?mini="${this.mini}"
                              >${t.more?t.more:o}</super-daemon-row
                            >`:""}
                    ></lit-virtualizer>
                  `:e`<div class="no-results">
                    ${this.t.noResultsForThisTerm}
                    <div class="slotted"><slot></slot></div>
                  </div> `}
            `}
        <div class="results-stats">
          ${this.filtered.length} / ${this.items.length} ${this.t.results}
        </div>
      </div>
      <div id="bottom"></div>
    `}}customElements.define(SuperDaemonUI.tag,SuperDaemonUI);