import axios from "axios"
import { useSlidesStore } from "@/store"
import { slides as slidesData } from "@/mocks/slides"
/**
 * http 请求json
 */

export function getHttpJson(url: string): Promise<{ code: number; data: Record<string, any> }> {
  return new Promise((res, rej) => {
    axios
      .get(url)
      .then(resData => {
        if (resData.status === 200 && typeof resData.data === "object") {
          res({
            code: 200,
            data: resData.data
          })
        } else {
          res({
            code: 500,
            data: {}
          })
        }
      })
      .catch(() => {
        res({
          code: 500,
          data: {}
        })
      })
  })
}

export function jsonToPpt(jsonData: Record<string, any>) {
  const slidesStore = useSlidesStore()
  const { width, theme, slides } = jsonData
  slidesStore.updateSlideIndex(0)
  slidesStore.setViewportSize(width || 1920)
  slidesStore.setViewportRatio(theme.viewportRatio)
  slidesStore.setTheme(theme)
  slidesStore.setSlides(slides.length ? slides : slidesData)
}

export function getJsonToBlob() {
  const slidesStore = useSlidesStore()
  const { slides, theme, viewportRatio, viewportSize, title } = slidesStore
  const json = {
    width: viewportSize,
    height: viewportSize * viewportRatio,
    slides: slides,
    theme: Object.assign(theme, { viewportRatio })
  }
  return {
    blob: new Blob([JSON.stringify(json)]),
    title
  }
}
