import TableStore from"./src/store";import tableSvelteApp from"./app.svelte";import utils from"./src/utils";import ActionsHandler from"./src/actions";import plugins from"./src/plugins/plugins";import{LexoRank}from"lexorank";import{createHooks}from"@wordpress/hooks";const hooks=createHooks();import"./style/style.scss";class Table{constructor(e){this.args=e,this.initPlugins(hooks)}init(e){const t=this;e=Object.assign(t.getDefaultProps(),e);new Promise((function(o){if(0==e.tableID)return o(e);const s={...e};delete s.container;const r=window.tablesomeTables.findIndex((e=>parseInt(e.collection.post_id)===parseInt(s.tableID)||parseInt(e.collection.table_id)===parseInt(s.tableID))),n=window.tablesomeTables[r],l=t.getRecords(n),i=l.rows,a=i.length>0?i.length:1;o(e=Object.assign(e,{rows:i,columns:t.getColumns(n),lastStateRecordID:a,lastColumnID:parseInt(n.collection.last_column_id),tableTitle:n.collection.table_title,recordsUpdated:l.generatedRankOrderRows,rowLimit:n.collection.rowLimit,columnLimit:n.collection.columnLimit,customStyle:!utils.getBool(n.collection.customStyle),accessControl:n.collection.access_control,editorState:n.collection.editorState,display:n.collection.display,style:n.collection.style,mode:n.collection.mode,derivedPermissions:n.collection.derived_permissions,sort:n.collection.sort,isAdminUser:n.collection.is_admin_user,siteTimezone:n.collection.site_timezone,excludeColumnsFromSearch:n.collection.exclude_columns_from_search||""}))})).then((e=>(e.hooks=hooks,e.store=t.getStore(e),e.actionsHandler=new ActionsHandler(e.store),e))).then((e=>{t.initDom(e),t.initComponentsOfStoreDependent(t.args,e.store)}))}getStore(e){return new TableStore(e)}initPlugins(e){new plugins(e)}initDom(e){const t=e.container;return t.innerHTML="",delete e.tableID,delete e.columns,delete e.rows,delete e.rowLimit,delete e.columnLimit,delete e.numOfRecordsPerPage,delete e.container,delete e.excludeColumnIDs,delete e.lastStateRecordID,delete e.lastColumnID,new tableSvelteApp({props:e,target:t})}initComponentsOfStoreDependent(e,t){if(0!=e.length)for(const o of e)o.init(t)}getDefaultProps(){const e=window.tablesomeTriggers?window.tablesomeTriggers.triggers:[];return{tableID:0,columns:[],rows:[],rowLimit:tablesome_settings.rowLimit?parseInt(tablesome_settings.rowLimit):2e4,columnLimit:tablesome_settings.columnLimit?parseInt(tablesome_settings.columnLimit):100,mode:"editor",customStyle:!tablesome_settings.customStyle||utils.getBool(tablesome_settings.customStyle),accessControl:tablesome_settings.access_control,editorState:tablesome_settings.editorState,display:tablesome_settings.display,style:tablesome_settings.style,triggers:e}}getColumns(e){return e.items.columns}getRecords(e){const t=this,o={rows:[],generatedRankOrderRows:[],lastRankOrder:e.collection.last_rank_order?e.collection.last_rank_order:LexoRank.min().genNext().toString()};return Array.from(e.items.rows).map(((s,r)=>{const n=null==s.rank_order||""==s.rank_order||0==s.rank_order.length;let l={...s,stateRecordID:r+1,record_id:Number(s.record_id),content:t.getIndexedRow(e.items.columns,s.content),rank_order:n?"":s.rank_order};n?(o.lastRankOrder=LexoRank.parse(o.lastRankOrder).genNext().toString(),l.rank_order=o.lastRankOrder,o.generatedRankOrderRows.push(l)):o.rows.push(l)})),o.rows=o.rows.concat(o.generatedRankOrderRows),o}getIndexedRow(e,t){const o=[];for(const s of e)o.push(t[s.id]);return o}}export default Table;