import { get } from 'lodash'

const marketplaceUrl = process.env.REACT_APP_MARKETPLACE_URL

export async function getProfile (didId) {
  console.log("getting profile for didID: " + didId)
  const response = await fetch(`${marketplaceUrl}/vendors/${didId}`)
  if (response.ok) {
    const data = await response.json()
    console.log(data)
    return get(data, 'profile', null)
  } else {
    return null
  }
}

export async function setProfile (profileData) {
  throw new Error('Not implemented')
}

export async function updateProfile (didId, profileData) {
  throw new Error('Not implemented')
}

export async function searchProfiles (type, location, name) {
  console.warn('Search filters not implemented')
  const response = await fetch(`${marketplaceUrl}/vendors`)
  if (response.ok) {
    const data = await response.json()
    const vendorProfiles = await Promise.all(data.map(async did => {
      const profile = await getProfile(did)
      profile.did = did
      return profile
    }))
    const vendorProfilesFiltered = vendorProfiles.filter(p => !!p)
    console.log(vendorProfilesFiltered)
    return vendorProfilesFiltered
  } else {
    return []
  }
}

const profileProvider = {
  getProfile,
  setProfile,
  updateProfile,
  searchProfiles
}

export default profileProvider
@@ -634,507 +634,395 @@
 es (
-type, location, name) %7B%0A  console.warn('Search filters not implemented')%0A  const response = await fetch(%60$%7BmarketplaceUrl%7D/vendors%60)%0A  if (response.ok) %7B%0A    const data = await response.json()%0A    const vendorProfiles = await Promise.all(data.map(async did =%3E %7B%0A      const profile = await getProfile(did)%0A      profile.did = did%0A      return profile%0A    %7D))%0A    const vendorProfilesFiltered = vendorProfiles.filter(p =%3E !!p)%0A    console.log(vendorProfilesFiltered)%0A    return vendorProfilesFiltered
+query, limit, offset ) %7B%0A  const response = await fetch(%60$%7BmarketplaceUrl%7D/search%60, %7B%0A    method: 'POST',%0A    body: JSON.stringify(%7B query, limit, offset %7D),%0A    headers: %7B%0A      'Accept': 'application/json',%0A      'Content-Type': 'application/json'%0A    %7D,%0A  %7D)%0A  if (response.ok) %7B%0A    const data = await response.json()%0A    return %7B%0A      count: data.count,%0A      rows: data.rows%0A    %7D
 %0A  %7D
@@ -1044,10 +1044,30 @@
 urn 
-%5B%5D
+%7B count: 0, rows: %5B%5D %7D
 %0A  %7D

implement searchProfiles