1const TableFilterMixin = {
2  methods: {
3    getFilteredTableData(tableData = [], filters = []) {
4      const filterItems = filters.reduce((arr, filter) => {
5        return [...arr, ...filter.values];
6      }, []);
7      // If no filters are active, then return all table data
8      if (filterItems.length === 0) return tableData;
9
10      const selectedValues = {};
11      for (const { key, values } of filters) {
12        if (values.length > 0) {
13          selectedValues[key] = values;
14        }
15      }
16
17      // Check if row property value is included in list of
18      // active filters
19      return tableData.filter((row) => {
20        for (const [key, values] of Object.entries(selectedValues)) {
21          const rowProperty = row[key];
22          if (rowProperty && !values.includes(rowProperty)) {
23            return false;
24          }
25        }
26        return true;
27      });
28    },
29    getFilteredTableDataByDate(
30      tableData = [],
31      startDate,
32      endDate,
33      propertyKey = 'date',
34    ) {
35      if (!startDate && !endDate) return tableData;
36      let startDateInMs = startDate ? startDate.getTime() : 0;
37      let endDateInMs = endDate ? endDate.getTime() : Number.POSITIVE_INFINITY;
38
39      const isUtcDisplay = this.$store.getters['global/isUtcDisplay'];
40
41      //Offset preference selected
42      if (!isUtcDisplay) {
43        startDateInMs = startDate
44          ? startDate.getTime() + startDate.getTimezoneOffset() * 60000
45          : 0;
46        endDateInMs = endDate
47          ? endDate.getTime() + endDate.getTimezoneOffset() * 60000
48          : Number.POSITIVE_INFINITY;
49      }
50
51      return tableData.filter((row) => {
52        const date = row[propertyKey];
53        if (!(date instanceof Date)) return;
54        const dateInMs = date.getTime();
55        if (dateInMs >= startDateInMs && dateInMs <= endDateInMs) return row;
56      });
57    },
58  },
59};
60
61export default TableFilterMixin;
62