import moment from "moment"


const getAnniversaryFromMonth = (apiData: any, month: any) => {
    let monthArray = []
    for (let i = 0; i < apiData.length; i++) {
        let monthA = Number(moment(apiData[i].fechaInic, 'YYYY-MM-DD').format('M')) - 1
        if (monthA === month) {
            let dayA = Number(moment(apiData[i].fechaInic, 'YYYY-MM-DD').format('D'))
            apiData[i].day = dayA
            apiData[i].month = month
            apiData[i].name = `${apiData[i].nombre} ${apiData[i].apellidoPate}`
            apiData[i].position = apiData[i].nombreCargo
            apiData[i].image = apiData[i].foto
            monthArray.push(apiData[i])
        }
    }
    monthArray.sort((a, b) => {
        if (a.day < b.day) return -1
        else if (a.day === b.day) return 0
        else return 1
    })


    return monthArray
}

export const parseFromApi = (apiData: any) => {
    let monthsArray = []
    for (let month = 0; month < 12; month++) {
        let monthArray = getAnniversaryFromMonth(apiData, month)
        if (monthArray.length > 0) monthsArray.push(...monthArray)
    }


    return monthsArray
}

