import { createPinia } from 'pinia'
import { useUsersStore } from '@/api'

const pinia = createPinia()
const { getUsers } = useUsersStore(pinia)

/**
 * Fetch the user list for wishlist share/edit popups.
 *
 * `getUsers()` resolves with the user array directly; the previous
 * `response.data` access wrote `undefined` into the caller's `users`
 * state, which then crashed the next render that read `users.length`.
 *
 * @since 1.0.7
 */
export const fetchUsers = async (): Promise<any[]> => {
  let users: any[] = []

  await getUsers().then((response: any) => {
    users = Array.isArray(response) ? response : []
  })

  return users
}
