import { ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useSlidesStore, useScreenStore } from '@/store'
import { getHtmlPlainText } from '@/utils/common'

export default () => {
  const slidesStore = useSlidesStore()
  const sourceLoadingStore = useScreenStore()
  const { slides, slideIndex } = storeToRefs(slidesStore)
  const { SourceLoading } = storeToRefs(useScreenStore())

  // 数据源更新
  const setNewDatasList = (DataSource:any,typeObj:any) => {
    console.log(typeObj,'----1111')
    sourceLoadingStore.setSourceLoading(false)
    const slidesData = slides.value
    const cursors = [] as Array<any>
    // 更新Slides数据
    let savelides = [] as any
    let newSlides = [] as any
    const obj = slidesData.find((x,index) =>{
        return slideIndex.value == index
    })
    if(obj){
        obj.elements.forEach(x=>{
            let dataObj = DataSource.DataSourceList.find((y:any)=>{
                return y.id == x.id
            })
            if(dataObj){
              x = dataObj
            }
            
            savelides.push(x)
      })
        obj.elements = JSON.parse(JSON.stringify(savelides))
        slidesData.forEach((x:any,index)=>{
            if(slideIndex.value == index){
                x.typeId = DataSource.typeId
                x.pgaeType = DataSource.pgaeType
                x.elements = obj.elements
            }
            newSlides.push(JSON.parse(JSON.stringify(x)))
        })
        slidesStore.setSlides(newSlides)
        
    }
  }
  

  return {
    setNewDatasList,
  }
}