var TableWidget_1,__decorate=this&&this.__decorate||function(t,e,i,s){var r,o=arguments.length,a=o<3?e:null===s?s=Object.getOwnPropertyDescriptor(e,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,i,s);else for(var l=t.length-1;l>=0;l--)(r=t[l])&&(a=(o<3?r(a):o>3?r(e,i,a):r(e,i))||a);return o>3&&a&&Object.defineProperty(e,i,a),a};import{css as t,html as e}from"lit";import{customElement as i}from"lit/decorators.js";import{OrAssetWidget as s}from"../util/or-asset-widget";import{TableSettings as r}from"../settings/table-settings";import{i18next as o}from"@openremote/or-translate";import{Util as a}from"@openremote/core";import{AssetModelUtil as l}from"@openremote/model";import"@openremote/or-mwc-components/or-mwc-table";function getDefaultConfig(){return{assetType:void 0,allOfType:!0,assetIds:[],attributeNames:[],tableSize:10,tableOptions:[10,25,100]}}let styling=t`
    #widget-wrapper {
      height: 100%;
      overflow: hidden;
    }
`,TableWidget=TableWidget_1=class extends s{static getManifest(){return{displayName:"Table",displayIcon:"table",getContentHtml:t=>new TableWidget_1(t),getDefaultConfig:()=>getDefaultConfig(),getSettingsHtml:t=>new r(t)}}static get styles(){return[...super.styles,styling]}refreshContent(t){this.widgetConfig=JSON.parse(JSON.stringify(this.widgetConfig))}willUpdate(t){return t.has("widgetConfig")&&this.widgetConfig&&this.loadAssets(),super.willUpdate(t)}loadAssets(){this.widgetConfig.allOfType?this.queryAssets({types:[this.widgetConfig.assetType],select:{attributes:this.widgetConfig.attributeNames}}).then(t=>{this.loadedAssets=t}):this.widgetConfig.assetIds.find(t=>!this.isAssetLoaded(t))&&this.queryAssets({ids:this.widgetConfig.assetIds,select:{attributes:this.widgetConfig.attributeNames}}).then(t=>{this.loadedAssets=t})}getColumns(t){let e=this.loadedAssets[0],i=t.map(t=>{var i,s;let r=t,o=!1;if(this.widgetConfig.assetType&&e&&e.attributes&&e.attributes[t]){let n=l.getAttributeDescriptor(t,this.widgetConfig.assetType);r=a.getAttributeLabel(e.attributes[t],n,this.widgetConfig.assetType,!0),o=(null==(i=null==n?void 0:n.format)?void 0:i.asNumber)||(null==(s=null==n?void 0:n.format)?void 0:s.asSlider)||!1}return{title:r,isSortable:!0,isNumeric:o}});return Array.of({title:o.t("assetName"),isSortable:!0},...i)}getRows(t){return this.loadedAssets.map(e=>{let i=t.map(t=>{if(!e.attributes||!e.attributes[t])return"N.A.";{let i=l.getAttributeDescriptor(t,e.type),s=a.getAttributeValueAsString(e.attributes[t],i,e.type,!1),r=Number(s);return isNaN(r)?s:r}});return{content:Array.of(e.name,...i)}})}render(){let t={fullHeight:!0,pagination:{enable:!0,options:this.widgetConfig.tableOptions}},i=this.getColumns(this.widgetConfig.attributeNames),s=this.getRows(this.widgetConfig.attributeNames);return e`
            <div id="widget-wrapper">
                <or-mwc-table .columns="${i}" .rows="${s}" .config="${t}" .paginationSize="${this.widgetConfig.tableSize}"
                              @or-mwc-table-row-click="${t=>this.onTableRowClick(t)}"
                ></or-mwc-table>
            </div>
        `}onTableRowClick(t){}};TableWidget=TableWidget_1=__decorate([i("table-widget")],TableWidget);export{TableWidget};