import DateCellComponent from"./date.svelte";import Fields from"./fields.svelte";import utils from"../../utils";import{tablesome_isToday,tablesome_isYesterday,tablesome_isTomorrow,tablesome_differenceInDays,tablesome_isThisMonth,tablesome_isThisYear,tablesome_getMonth,tablesome_getYear,tablesome_isSameDay,tablesome_startOfDay,tablesome_isValid}from"../../wrapper/date-fns";class DatePlugin{constructor(e){this.hooks=e,this.namespace="tablesome",this.init()}init(){this.hooks.addFilter("getColumnFormats",this.namespace,this.getColumnFormats),this.hooks.addFilter("getCellComponent",this.namespace,this.getCellComponent),this.hooks.addFilter("getColumnOptionFields",this.namespace,this.getColumnOptionFields),this.hooks.addFilter("getSearchRecords",this.namespace,this.getSearchRecords),this.hooks.addFilter("getSortRecords",this.namespace,this.getSortRecords),this.hooks.addFilter("getFilterRecords",this.namespace,this.getFilterRecords)}getColumnFormats(e){return e.push({format:"date",class:"tablesome__option--basic tablesome__option--basic-date",icon:"calendar-alt",text:"Date"}),e}getColumnOptionFields(e){return"date"==e.format&&e.fields.push(Fields),e}getCellComponent(e){return"date"==e.cellType&&(e.component=DateCellComponent),e}getSearchRecords(e){if("date"==e.cellType&&!e.isContentMatch){let t=tablesome_isValid(parseInt(e.cell.value))?utils.getFormatedDate(parseInt(e.cell.value),e.column).toLowerCase():"";e.isContentMatch=t.indexOf(e.searchQuery)>=0}return e}getSortRecords(e){return"date"==e.cellType&&(e.valueA="value"in e.rowA&&e.rowA.value?e.rowA.value:"",e.valueB="value"in e.rowB&&e.rowB.value?e.rowB.value:"",e.sortType="number"),e}getFilterRecords(e){if("date"==e.filter.format){let t=e.filter.value,a="value"in e.cell&&e.cell.value?e.cell.value:"";e.filter.contextual||(e.isContentMatch=!0),e.isContentMatch=filterDate(e.filter.operator.value,e.filter.contextual.value,a,t)}return e}}export default DatePlugin;const filterDate=(e,t,a,s)=>{if(a=parseInt(a),!isNaN(a))return!(0!=s&&""!=s||"month"!=t&&"year"!=t&&"exact_date"!=t)||("="==e?doesDateMatchContext(t,a,s):"!="==e?!doesDateMatchContext(t,a,s):doesDateMatchContextForLesserAndGreaterOperators(e,t,a,s))},doesDateMatchContext=(e,t,a)=>{let s=!1,o=(new Date).getTime(),r=t,l=0;switch(e){case"today":s=tablesome_isToday(t);break;case"yesterday":s=tablesome_isYesterday(t);break;case"tomorrow":s=tablesome_isTomorrow(t);break;case"last_seven_days":l=tablesome_differenceInDays(o,r),s=l>0&&l<=7;break;case"last_thirty_days":l=tablesome_differenceInDays(o,r),s=l>0&&l<=30;break;case"current_month":s=tablesome_isThisMonth(t);break;case"current_year":s=tablesome_isThisYear(t);break;case"month":a="zero"==a?0:a,s=tablesome_getMonth(t)==a;break;case"year":s=tablesome_getYear(t)==a;break;case"exact_date":s=tablesome_isSameDay(t,a);break;case"today_and_after":s=tablesome_startOfDay(t).getTime()>=tablesome_startOfDay(new Date).getTime();break;case"today_and_before":s=tablesome_startOfDay(t).getTime()<=tablesome_startOfDay(new Date).getTime()}return s},doesDateMatchContextForLesserAndGreaterOperators=(e,t,a,s)=>{tablesome_startOfDay(new Date).getTime();switch(a=tablesome_startOfDay(a).getTime(),t){case"month":s="zero"==s?0:s,a=tablesome_getMonth(a);break;case"year":a=tablesome_getYear(a);break;case"exact_date":s=tablesome_startOfDay(s).getTime();break;default:return!0}return">"==e?a>s:"<"==e?a<s:">="==e?a>=s:"<="==e?a<=s:void 0};