import{get}from"svelte/store";import{notifications}from"./../../table/svelte/notification/notifications";import page from"./page";var tableProps=require("./props"),ajaxEvents={alertMessage:"Table Created, Successfully",saveTable:function(e,t="backend"){const o=this;let n=this.getModifiedRecords(get(e.rowStore.rows)),s=get(e.workflow),a=this.getTriggers(s.triggers);const r=parseInt(e.tableID);let i={table_id:r,records_deleted:get(e.rowStore.recordsDeleted),records_inserted:n.insertedRecords,records_updated:n.updatedRecords,columns_inserted:get(e.columnStore.columnsInserted),columns_deleted:get(e.columnStore.columnsDeleted),columns_duplicated:get(e.columnStore.columnsDuplicated),mode:get(e.mode)};const c={table_id:r,table_title:tableProps.getTitle(),columns:get(e.columnStore.columns),last_column_id:get(e.columnStore.lastColumnID),triggers:a,editorState:get(e.editorState),access_control:get(e.accessControl).fields,display:get(e.display),style:get(e.style),mode:get(e.mode),sort:{order:get(e.sortOrder),field:get(e.sortField),index:get(e.activeColumnIndex)},recordsData:i,origin_location:t};r>0&&(o.alertMessage="Table Updated, Successfully"),fetch(tablesome_ajax_object.api_endpoints.save_table,{method:"post",credentials:"same-origin",headers:{"Content-Type":"application/json","X-WP-Nonce":tablesome_ajax_object.rest_nonce},body:JSON.stringify(c)}).then((function(e){return e.ok?e.json():e.json().then((function(t){return{status:"failed",message:t.message||t.data?.message||"Request failed with status "+e.status,error_code:t.code||t.data?.status||"HTTP_ERROR"}})).catch((function(t){return{status:"failed",message:"Request failed with status "+e.status,error_code:"HTTP_ERROR"}}))})).then((function(e){return e})).then((function(e){o.saveNotice(e),"success"==e.status&&e.table_id>0&&o.afterSave(e.table_id,c.mode,t),"frontend"===t&&page.hideSpinner()})).catch((function(e){o.errorNotice(e),"frontend"===t&&page.hideSpinner()}))},errorNotice:function(e){let t=jQuery("<span class='save-notice failed'>"+e+"</span>");jQuery(".tablesome__button--wrapper").append(t)},capitalize:function(e){return e.split(" ").map((function(e){return e.charAt(0).toUpperCase()+e.slice(1)})).join(" ")},saveNotice:function(e){let t=jQuery("<span class='save-notice "+e.status+"'>"+this.capitalize(e.status)+" : "+e.message+"</span>");jQuery(".tablesome__button--wrapper").append(t)},sendRecords:function(e,t="editor"){var o=this;fetch(tablesome_ajax_object.api_endpoints.prefix+e.table_id+"/records",{method:"post",credentials:"same-origin",headers:{"Content-Type":"application/json","X-WP-Nonce":tablesome_ajax_object.rest_nonce},body:JSON.stringify(e)}).then((function(t){o.afterSave(e.table_id)})).then((function(e){})).catch((function(e){}))},afterSave:function(e,t,o="backend"){notifications.success(this.alertMessage,"",5e3);let n=window.location.href;"editor"==t&&(n=new URL(tablesome_ajax_object.edit_table_url),n.searchParams.set("post",e)),window.location.replace(n)},getModifiedRecords:function(e){let t=[],o=[];return e.map((function(e,n){0==e.record_id?t.push(e):o.push(e)})),{insertedRecords:t,updatedRecords:o}},getTriggers:function(e){return e.map((function(e){return e.actions&&e.actions.length>0&&(e.actions=e.actions.map((function(e){return e.match_columns&&e.match_columns.length>0&&(e.match_columns=e.match_columns.map((function(e){return{...e}}))),e.match_fields&&e.match_fields.length>0&&(e.match_fields=e.match_fields.map((function(e){return{...e}}))),e.conditions&&e.conditions.length>0&&(e.conditions=e.conditions.map((function(e){return{...e}}))),{...e}}))),{...e}}))}};export default ajaxEvents;