import React from "react";
//import { getRowsCount } from "../../../auto/js/events";
import { getEntityIdCol, getServiceUri } from "../../../auto/js/metadata";
import { rest } from "../../../auto/js/services";
import { entityImageUrl } from "../metadata/CustomMetadata";

export const getSpreadSheetData = async (subsection, filter) => {
    switch(subsection) {
        case "vital-record":
            let data;
            let rowsCount;
            return await getVitalRecordData(filter).then(response => {
                data = filterData(response, "vital-record");
                rowsCount = response.length;
            }).then(() => {return {data:data, rowsCount:rowsCount}});
        case "civil-status-mtlb":
            let civiltStatusData;
            let civiltStatusRowsCount;
            console.log(filter);
            return await getCivilStatusMtlbData(filter).then(response => {
                civiltStatusData = filterData(response, "civil-status-mtlb");
                civiltStatusRowsCount = response.length;
            }).then(() => {return {data:civiltStatusData, rowsCount:civiltStatusRowsCount}});
    }
}

const filterData = (DefaultRows, subsection) => {
    const newRows = [];
    for (let i in DefaultRows) {
        let row = DefaultRows[i];
        row.imageUrl = getServiceUri() + entityImageUrl[subsection] + row[getEntityIdCol(subsection)];
        let date = row.birthdate
        if ( date !== null)
        row.birthdate = date[2] + '-' + date[1] + '-' + date[0]; 
      newRows.push(row);
    }
    return newRows;
}

const getVitalRecordData = async (filter) => {
    return await rest.search("vital-record", filter)

}