import httpInstance from '@/utils/http'

/**
 *查询目录-同步教材
 */
export const getTextbookAPI = () => {
  return httpInstance({
    url: '/portal/textbook/catalog/list'
  })
}

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

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

export const getTextbookInfoAPI = (id) => {
  return httpInstance({
    url: `/portal/textbook/${id}`
  })
}

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

export function textbookPreviewTxt(id) {
  return httpInstance({
    url: `/portal/textbook/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)
    }
  })
}