import{r as i,c as t,h as e,H as s}from"./p-cb5076d4.js";import{g as l}from"./p-d9adb303.js";import{a}from"./p-54805fa4.js";import{L as n}from"./p-5eec4552.js";import{r as o,c as d}from"./p-f46dff5e.js";import"./p-d343a062.js";import"./p-d50d0ec8.js";function r(i){if(i&&i.sort)return i.sort}const c=class{constructor(e){i(this,e),this.action=t(this,"action",7),this.list=n.init(),this.getLogger=()=>this.logger.new("DvnAppViewListViewTable").setColor("river"),this.isQuickFilterBlockingFetch=()=>{var i;return(null===(i=this.view.ui.quickSearch)||void 0===i?void 0:i.requireFilters)&&!this.quickFilter},this.onSimpleFilterChange=({filter:i})=>{this.list=n.init(),this.quickFilter=i}}async invalidate(i){this.fetchList(!0,!0)}handleScroll(){this.fetchList()}onModifyItem(i){const{field:t,item:e,value:s}=i.detail;this.updateItem(e,t,s)}componentWillLoad(){this.isQuickFilterBlockingFetch()||this.fetchList()}componentDidUpdate(){if(this.isQuickFilterBlockingFetch())return;const{list:i}=this;i.data||i.isPending||i.error||this.fetchList()}fetchList(i=!1,t=!1){const{list:e,quickFilter:s}=this;if(e.isPending||e.isEndReached()||e.error&&!i)return;const{query:a,table:n,ui:c}=this.view,p=c.quickSearch?l(c.quickSearch,n.fields):void 0,h=function(i,t,e){let s=i?i.filter:void 0;if(t&&(s=i&&i.filter?{operator:"and",filters:[i.filter,t]}:t),s)return o(d(s),e)}(a,s||p,this.params);this.list=e.beforeFetch(),this.apiData.query(n.name,{fields:a.fields,filter:h,limit:25,skip:t?0:e.data?e.data.length:0,sort:r(a)}).then((i=>{this.list=e.afterFetch(i,!t)}),(i=>{this.list=e.afterFetchError(i)}))}async updateItem(i,t,e){const s={[t.id]:e},l={[t.id]:i[t.id]};this.list=this.list.afterUpdateItem(i._id,s);const a={operation:"update",data:s};await this.apiData.update(this.view.table.name,i._id,a).catch((()=>{this.getLogger().errorGroup("Failed to update item.",{field:t,item:i,value:e}),this.notifier.alert("Update failed."),this.list=this.list.afterUpdateItem(i._id,l)}))&&this.getLogger().debugGroup("Updated item.",{field:t,item:i,value:e})}render(){const{view:{title:i,ui:t}}=this,l=t.header&&t.header.actions&&t.header.actions,{list:n}=this;return e(s,{class:{"leaned-vertically":void 0===t.leanedVertically||t.leanedVertically,"leaned-horizontally":void 0===t.leanedHorizontally||t.leanedHorizontally}},e("div",{class:"d-flex align-items-center pt-3 pb-3"},e("h2",null,i),e("div",{class:"header-actions-container d-flex flex-grow-1 align-items-center justify-content-end pl-3"},t.quickSearch&&e("dvn-filter-simple",{fields:this.view.table.fields,leaned:!l||l.length%2==0,config:t.quickSearch,onFilterChange:i=>this.onSimpleFilterChange(i.detail)}),t.header&&t.header.actions&&t.header.actions&&t.header.actions.map((i=>a(0,this.params,void 0,this.action,i))))),this.isQuickFilterBlockingFetch()&&e("p",{class:"text-centered"},"You need to apply a filter to fetch data."),!this.isQuickFilterBlockingFetch()&&e("div",{class:"table-container"},e("dvn-list-view-table",{config:t.main,fields:this.view.table.fields,items:n.data||[],isEndReached:n.isEndReached(),isProgressing:n.isPending,error:n.error?"Fetching list failed":void 0,onRetry:()=>this.fetchList(!0),params:this.params})))}};c.style='.sc-dvn-app-view-list-view-table-h{-ms-flex:1 1 1px;flex:1 1 1px;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;margin:calc(4 * var(--spacing-unit));overflow:hidden}.leaned-horizontally.sc-dvn-app-view-list-view-table-h{margin-left:0;margin-right:0}.leaned-vertically.sc-dvn-app-view-list-view-table-h{margin-bottom:0;margin-top:0}.sc-dvn-app-view-list-view-table-h h2.sc-dvn-app-view-list-view-table{margin:0;line-height:1.2em}.sc-dvn-app-view-list-view-table-h h2.sc-dvn-app-view-list-view-table:before{content:"";display:block;height:0;width:0;margin-top:calc(calc(1 - 1.2) * 0.5em)}.sc-dvn-app-view-list-view-table-h dvn-filter-simple.sc-dvn-app-view-list-view-table{-ms-flex:1;flex:1}.sc-dvn-app-view-list-view-table-h .header-actions-container.sc-dvn-app-view-list-view-table dvn-button.sc-dvn-app-view-list-view-table{--margin:0 calc(1.5 * var(--spacing-unit))}.sc-dvn-app-view-list-view-table-h .table-container.sc-dvn-app-view-list-view-table{-ms-flex:1 1 1px;flex:1 1 1px;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;overflow:hidden}';export{c as dvn_app_view_list_view_table}