/**
 * Cached fetch for near-static reference/lookup data (countries, organizations,
 * order-sources, …) that populate dropdowns across many pages.
 *
 * Why: the same lookup URL is fetched independently on dozens of pages. Nuxt 4
 * dedupes within a single navigation, but not across navigations — so plain
 * useFetch re-hits iDempiere for the same list every time you change page.
 * useLookup keys by URL and serves the cached payload on later navigations via
 * getCachedData, and is lazy so it never blocks navigation.
 *
 * Do NOT use this on an entity's own CRUD/management list page (e.g.
 * settings/order-sources/index.vue) — those must always refetch to reflect
 * just-created/edited rows. Use plain useFetch there. This is for the *consumer*
 * dropdown fetches only.
 */
export const useLookup = (url: string, opts: any = {}) => {
  return useFetch(url, {
    key: `lookup:${url}`,
    headers: useRequestHeaders(['cookie']),
    lazy: true,
    getCachedData: (key, nuxtApp) => nuxtApp.payload.data[key] ?? nuxtApp.static.data[key],
    ...opts
  })
}
