import isEqual from 'lodash.isequal';

// export const state = () => ({});

export const getters = {
    // console.log(this.$store.getters['utils/areEquals'](1, 1)) // It works
    areEquals: (state) => (input1, input2) => isEqual(input1, input2),
    isNotEmptyObj: (state) => (obj) => Object.keys(obj).length > 0,

    filterArrayObjsByArrayOfProperties: (state) => (arrayObjs, arrayProperties) => {

        const filteredData = arrayObjs.map((row) => {

            const obj = {}

            arrayProperties.forEach(property => obj[property] = row[property]);

            return obj;
        })

        return filteredData;
    },

    arrayOfObjectToCsv: (state) => (data) => {
        const headers = Object.keys(data[0])

        const csvRows = []

        for (const row of data) {
            const values = headers.map((header) => {

                const tmpArray = []

                if (row[header]) {
                    // Replacing commas with whitespaces
                    tmpArray.push(row[header].replace(/,/g, ""))
                    return tmpArray
                }
                return '-'
            })
            csvRows.push([values.join(',')])
        }

        const csvString = [
            [...headers],
            ...csvRows,
        ]
            .map(e => e.join(","))
            .join("\n");

        return csvString
    },
}