import httpInstance from '@/utils/http'

export const getResourceCatalogList = (params = {}) => {
  return httpInstance({
    url: '/portal/resource/catalog/pageList',
    params
  })
}


export const getResourceList = (params = {}) => {
  return httpInstance({
    url: '/portal/resource/list',
    params
  })
}

export const getTreeList = (params = {}) => {
  return httpInstance({
    url: '/portal/resource/catalog/tree',
    params
  })
}

export const getResourceInfo = (id) => {
  return httpInstance({
    url: `/portal/resource/${id}`,
  })
}

export function resourcePreview(id) {
  return httpInstance({
    url: `/portal/resource/preview/${id}`,
    method: 'post',
    // responseType: 'blob'
  });
}

export function resourcePreviewTxt(id) {
  return httpInstance({
    url: `/portal/resource/preview/${id}`,
    method: 'post',
    responseType: 'blob',
    transformResponse: [
      async function (data) {
        return await transformData(data)
      }
    ]
  });
}

const transformData = (data) => {
  return new Promise((resolve) => {
    let reader = new FileReader()
    reader.readAsText(data)
    reader.onload = function () {
      resolve(reader.result)
    }
  })
}