import { sleep } from '@/utils';
import type { City, District, GeographyApi, Province } from '../types';
import { SEED_CITIES, SEED_DISTRICTS, SEED_PROVINCES } from './seed';

const MOCK_LATENCY_MS = 250;

const bySortOrder = <T extends { sortOrder: number }>(a: T, b: T) => a.sortOrder - b.sortOrder;

/**
 * In-memory mock behind the GeographyApi seam — returns the canned, active-only,
 * `sortOrder`-ordered hierarchy. Mirrors the real lookups exactly (empty district list for a
 * whole-city-only city), so swapping to the live endpoints is a one-line change in constants.ts.
 */
export const geographyMockApi: GeographyApi = {
  listProvinces: async (): Promise<Province[]> => {
    await sleep(MOCK_LATENCY_MS);
    return [...SEED_PROVINCES].sort(bySortOrder);
  },

  listCities: async (provinceId: number): Promise<City[]> => {
    await sleep(MOCK_LATENCY_MS);
    return SEED_CITIES.filter((city) => city.provinceId === provinceId).sort(bySortOrder);
  },

  listDistricts: async (cityId: number): Promise<District[]> => {
    await sleep(MOCK_LATENCY_MS);
    return SEED_DISTRICTS.filter((district) => district.cityId === cityId).sort(bySortOrder);
  },
};
