export function getMap<K extends string | number, V>(record: () => Record<K, V>) {
  let map: Map<K, V> | undefined
  let entries: [K, V][] | undefined

  function getMap(): Map<K, V> {
    map ??= new Map(Object.entries(record())) as Map<K, V>
    return map!
  }

  function getEntries(): [K, V][] {
    return (entries ??= Array.from(getMap().entries()))
  }

  return {
    get(key: K): V {
      return getMap().get(key)!
    },
    has(key: K): boolean {
      return getMap().has(key)
    },
    entries(): [K, V][] {
      return getEntries()
    },
    map<T>(fn: (value: V, key: K) => T): T[] {
      return getEntries().map(([key, value]) => fn(value, key))
    },
  }
}
