/**
 * 获取开屏广告
 * @param {*} cityId 
 */
import { useEffect, useState, useCallback } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { queryExtContents } from '@/services/common'

export default function useScreen(cityId, person) {
  const dispatch = useDispatch()

  const [firstScreen, setFirstScreen] = useState()
  const cityShow = useSelector(s => s.system.firstScreen.filter(x => x.cityId === cityId)[0])
  const toggle = useCallback(
    () => dispatch({ type: 'TOGGLE_FIRSTSCREEN', payload: { cityId } }),
    [cityId, dispatch],
  )

  useEffect(() => {
    if (cityId && person?.personId) {
      queryExtContents({cityId}).then((res) => {
        const screen = (res || []).filter(x => x.showType === 'screen' && x.showPosition === 'index')[0]
        if (!screen) {
          return;
        }

        setFirstScreen(screen)
  
        if (screen) {
          toggle()
        }
      })
    }
  }, [cityId, toggle, person?.personId])

  return [firstScreen, cityShow?.visible, toggle]
}
