import request from './request'
import { normalizeSourceCategories, normalizeSourceProject, normalizeSourceProjects } from './sourceAdapter'

export function listSourceCategories() {
  return request({
    url: '/front/source/categories',
    method: 'get'
  }).then((result) => normalizeSourceCategories(result.data))
}

export function listSourceProjects(params = {}) {
  return request({
    url: '/front/source/projects',
    method: 'get',
    params
  }).then((result) => normalizeSourceProjects(result.data))
}

export function getSourceProject(slug) {
  return request({
    url: `/front/source/projects/${slug}`,
    method: 'get'
  }).then((result) => normalizeSourceProject(result.data))
}

export function purchaseSourceProject(projectId) {
  return request({
    url: `/front/source/projects/${projectId}/purchase`,
    method: 'post'
  })
}

export function listSourcePurchases() {
  return request({
    url: '/front/source/purchases',
    method: 'get'
  }).then((result) => result.data || [])
}

export function getSourceDownloadInfo(projectId) {
  return request({
    url: `/front/source/projects/${projectId}/download`,
    method: 'get'
  }).then((result) => result.data)
}
