import{r as i,c as t,h as e,H as s}from"./p-9f21f85f.js";import{g as l}from"./p-2bb9af72.js";import{a}from"./p-04f5d146.js";import{L as n}from"./p-e8a169b7.js";import{r as o,c as r}from"./p-f46dff5e.js";import"./p-d343a062.js";import"./p-aa5ffe14.js";const c={noItemInList:"No items in list."};function d(i){if(i&&i.sort)return i.sort}const h=class{constructor(e){i(this,e),this.action=t(this,"action",7),this.checkboxSelection=!1,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,rootField:s,value:l,apiValue:a}=i.detail;this.updateItem(e,s,t,l,a)}async componentWillLoad(){this.strings=await this.apiSozluk.getContent(this.view.i18nEntryId,c),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(t)e.meta=void 0;else if(e.isPending||e.isEndReached()||e.error&&!i)return;const{query:a,table:n,ui:c}=this.view,h=c.quickSearch?l(c.quickSearch,n.fields):void 0,p=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(r(s),e)}(a,s||h,this.params);this.list=e.beforeFetch();const v={fields:a.fields,filter:p,limit:25,skip:t?0:e.data?e.data.length:0,sort:d(a)};this.apiData.query(n.name,v).then((i=>{this.list=e.afterFetch(i,!t)}),(i=>{this.list=e.afterFetchError(i)}))}async updateItem(i,t,e,s,l){let a=i._id,n=this.view.table.name,o={[e.id]:s},r={[e.id]:i[e.id]},c={[e.id]:l||s};if("reverse-reference"===(null==t?void 0:t.type)){const l=i[t.id];a=l._id,n=t.otherCollection,o={[t.id]:Object.assign(Object.assign({},l),{[e.id]:s})},r={[t.id]:l},c={[e.id]:s}}this.list=this.list.afterUpdateItem(i._id,o);const d={operation:"update",data:c};await this.apiData.update(n,a,d).catch((()=>{this.getLogger().errorGroup("Failed to update item.",{field:e,item:i,value:s}),this.notifier.alert("Update failed."),this.list=this.list.afterUpdateItem(i._id,r)}))&&this.getLogger().debugGroup("Updated item.",{field:e,item:i,value:s})}render(){const{strings:i,view:{title:t,ui:l},onRowSelection:n,checkboxSelection:o}=this,r=l.header&&l.header.actions&&l.header.actions,{list:c}=this;return e(s,{class:{"leaned-vertically":void 0===l.leanedVertically||l.leanedVertically,"leaned-horizontally":void 0===l.leanedHorizontally||l.leanedHorizontally}},e("div",{class:"d-flex align-items-center pt-3 pb-3"},e("h2",null,t),e("div",{class:"header-actions-container d-flex flex-grow-1 align-items-center justify-content-end pl-3"},l.quickSearch&&e("dvn-filter-simple",{fields:this.view.table.fields,leaned:!r||r.length%2==0,config:l.quickSearch,onFilterChange:i=>this.onSimpleFilterChange(i.detail)}),l.header&&l.header.actions&&l.header.actions&&l.header.actions.map(((i,t)=>a(0,this.params,void 0,this.action,i,{leaned:t%2==1}))))),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:l.main,fields:this.view.table.fields,items:c.data||[],isEndReached:c.isEndReached(),isProgressing:c.isPending,error:c.error?"Fetching list failed":void 0,onRetry:()=>this.fetchList(!0),params:this.params,scrollToFit:!0,strings:i,onRowSelection:n,checkboxSelection:o})))}};h.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:-0.1em}.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{h as dvn_app_view_list_view_table}