import type { ActualListing } from '../types';

/**
 * Which slice of the live for-sale listings to render on the map.
 *
 * - `all`: every listing in view
 * - `new`: new-build listings only
 * - `non-new`: everything that isn't a new build (resale, OnTheMarket, …)
 * - `none`: hide listings entirely (also skips the fetch)
 */
export type ListingsMode = 'all' | 'new' | 'non-new' | 'none';

export const LISTINGS_MODES: readonly ListingsMode[] = ['all', 'new', 'non-new', 'none'];

export const DEFAULT_LISTINGS_MODE: ListingsMode = 'all';

/**
 * Rightmove encodes the sales channel in the listing URL: `?channel=RES_NEW` for
 * new-build developments and `?channel=RES_BUY` for ordinary resale. Anything
 * without `RES_NEW` (resale, OnTheMarket and other portals) is treated as non-new.
 */
export function isNewBuildListing(listing: Pick<ActualListing, 'listing_url'>): boolean {
  return listing.listing_url.includes('RES_NEW');
}

/** Apply a {@link ListingsMode} to a list of listings. */
export function filterListingsByMode(
  listings: ActualListing[],
  mode: ListingsMode
): ActualListing[] {
  switch (mode) {
    case 'all':
      return listings;
    case 'new':
      return listings.filter(isNewBuildListing);
    case 'non-new':
      return listings.filter((listing) => !isNewBuildListing(listing));
    case 'none':
      return [];
  }
}
