import { auctionEnquiryCategoryOptions } from '@/constants/options';
import type { SelectOption } from '@/constants/options';
import {
    IN_PERSON_AUCTION_GRID_PREFILL_KEY,
    ONLINE_AUCTION_GRID_PREFILL_KEY,
    readAuctionGridPrefill,
} from '@/utils/customerAuctionNavigation';

interface AuctionGridFiltersWithUser {
    user_id: number | null;
    category: SelectOption;
}

export function applyAuctionGridPrefill(
    storageKey: typeof ONLINE_AUCTION_GRID_PREFILL_KEY | typeof IN_PERSON_AUCTION_GRID_PREFILL_KEY,
    filters: AuctionGridFiltersWithUser,
): boolean {
    const prefill = readAuctionGridPrefill(storageKey);

    if (!prefill) {
        return false;
    }

    filters.user_id = prefill.userId;

    if (prefill.category) {
        const categoryOption = auctionEnquiryCategoryOptions.find((option) => option.id === prefill.category);

        if (categoryOption) {
            filters.category = { ...categoryOption };
        }
    }

    return true;
}
