
import { CombinedKPIs, FilterState } from '../types';

export const filterCombinedData = (data: CombinedKPIs[], filters: FilterState): CombinedKPIs[] => {
    return data.filter(item => {
        // 1. Month Logic
        const recordMonth = item.month; // e.g. "Apr-23"
        const pureMonth = recordMonth.split('-')[0]; // "Apr"

        // 2. Filter Checks
        const customerMatch = filters.customer.length === 0 || filters.customer.includes(item.marketplace);
        const yearMatch = filters.year.length === 0 || filters.year.includes(item.year.toString());

        // Exact or partial month match
        const monthMatch = filters.month.length === 0 || filters.month.includes(pureMonth) || filters.month.includes(recordMonth);

        const lineMatch = filters.line.length === 0 || filters.line.includes(item.line);
        const asinMatch = filters.asin.length === 0 || filters.asin.includes(item.asin);
        const skuMatch = filters.sku.length === 0 || filters.sku.includes(item.sku);
        const titleMatch = filters.title.length === 0 || filters.title.includes(item.title);

        return customerMatch && yearMatch && monthMatch && lineMatch && asinMatch && skuMatch && titleMatch;
    });
};
