import type { Storage } from '@google-cloud/storage'

import path from 'path'

import type { GenerateURL } from '../../types.js'

interface Args {
  bucket: string
  getStorageClient: () => Storage
}

export const getGenerateURL =
  ({ bucket, getStorageClient }: Args): GenerateURL =>
  ({ filename, prefix = '' }) => {
    return decodeURIComponent(
      getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename)).publicUrl(),
    )
  }
