/**
 * Copyright 2019 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text.
 */
import{LitElement as t}from"../../../../../lit/index.js";import{store as e}from"../../core/haxcms-site-store.js";import{objectValFromStringPos as i}from"../../../../utils/utils.js";import{autorun as r,toJS as s}from"../../../../../mobx/dist/mobx.esm.js";Object.byString=function(t,e){return i(t,e)};class SiteQuery extends t{static get tag(){return"site-query"}static get properties(){return{routerManifest:{type:Object},activeId:{type:String,attribute:"active-id"},result:{type:Array},conditions:{type:Object},sort:{type:Object},forceRebuild:{type:Boolean,attribute:"force-rebuild"},limit:{type:Number},startIndex:{type:Number,attribute:"start-index"},random:{type:Boolean},entity:{type:String}}}constructor(){super(),this.entity="node",this.conditions={},this.random=!1,this.sort={order:"ASC"},this.forceRebuild=!1,this.limit=0,this.startIndex=0}updated(t){t.forEach(((t,e)=>{if(["result","conditions","sort","forceRebuild"].includes(e)){let t=`${e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase()}-changed`;this.dispatchEvent(new CustomEvent(t,{detail:{value:this[e]}}))}["entity","conditions","sort","routerManifest","activeId","limit","startIndex","random","forceRebuild"].includes(e)&&(this.result=[...this._computeResult(this.entity,this.conditions,this.sort,this.routerManifest,this.activeId,this.limit,this.startIndex,this.random,this.forceRebuild)])}))}_computeResult(t,e,i,r,o,n,a,c,b){if(r&&r.items){var u=[...s(r.items)];if("node"!==t){var d=[];for(var l in u)if(void 0!==Object.byString(u[l],t)){let e,i=Object.byString(u[l],t);if(e="object"==typeof i||"array"==typeof i?Object.assign([],Object.byString(u[l],t)):i,"object"==typeof e||"array"==typeof e)for(var l in e)if("object"==typeof e[l]||"array"==typeof e[l])e[l]._node=Object.assign({},u[l]),d.push(e[l]);else{let t={_node:Object.assign({},u[l]),value:e[l]};d.push(t)}else{let t={_node:Object.assign({},u[l]),value:e};d.push(t)}}u=Object.assign([],d)}if(e&&u)for(var l in e){null===e[l]?e[l]={value:[e[l]],operator:"="}:"object"!=typeof e[l]&&(e[l]={value:e[l],operator:"="});var y=e[l].value;"$activeId"===e[l].value?y=o:"$firstId"===e[l].value&&(y=u[0].id),u=u.filter((t=>{switch(e[l].operator){case">":return Object.byString(t,l)>y;case"<":return Object.byString(t,l)<y;case"!=":return"object"==typeof y&&!y.includes(Object.byString(t,l))||"string"==typeof y&&Object.byString(t,l)!==y;default:return!("object"==typeof y&&!y.includes(Object.byString(t,l)))&&("string"!=typeof y||Object.byString(t,l)===y)}}))}if(i)for(var l in i)u.sort(((t,e)=>"ASC"===i[l]?Object.byString(t,l)<Object.byString(e,l)?-1:Object.byString(t,l)>Object.byString(e,l)?1:0:Object.byString(t,l)>Object.byString(e,l)?-1:Object.byString(t,l)<Object.byString(e,l)?1:0));if(c&&u.sort(((t,e)=>Math.random()<Math.random()?-1:Math.random()>Math.random()?1:0)),0!==a&&u.length>a)for(;a>0;)u.shift(),a--;else if(u.length<a)return[];if(0!==n&&n>0)for(;u.length>n;)u.pop();return u}return[]}connectedCallback(){super.connectedCallback(),this.__disposer=r((()=>{this.routerManifest=Object.assign({},s(e.routerManifest))})),this.__disposer2=r((()=>{this.activeId=s(e.activeId)}))}disconnectedCallback(){this.__disposer(),this.__disposer2(),super.disconnectedCallback()}}customElements.define(SiteQuery.tag,SiteQuery);export{SiteQuery};