import { useState } from 'react'
import type { MediaAsset } from '@/types/domain'
import { chooseImages } from '@/services/publish.service'

export function useUploadMedia(initial: MediaAsset[] = []) {
  const [media, setMedia] = useState<MediaAsset[]>(initial)

  const addImages = async () => {
    const selected = await chooseImages(media.length)
    setMedia(current => [...current, ...selected])
  }

  const removeMedia = (id: string) => {
    setMedia(current => current.filter(item => item.id !== id))
  }

  const replaceMedia = (next: MediaAsset[]) => {
    setMedia(Array.isArray(next) ? next : [])
  }

  return { media, replaceMedia, addImages, removeMedia }
}
