import axios, { AxiosRequestConfig } from 'axios'

/**
 * Returns the filename from a path (without extension)
 * Example: 'path/to/file.svg' or 'file.png' become 'file'
 */
export const getFilenameFromPath = (inputPath: string): string => {
  // Use a regular expression to match the full path and capture the filename without extension
  const regex = /(?:.*[\\/])?(.+?)(?:\.[^.]*$|$)/
  const match = RegExp(regex).exec(inputPath)

  // Check if there is a match, else case returns an empty string
  return match?.[1] ?? ''
}

/**
 * Gets the ecogesture images URLs
 */
export const getEcogestureImages = async (
  axiosHeaders: AxiosRequestConfig
): Promise<string[]> => {
  try {
    const { data: imageNames } = await axios.get<string[]>(
      `/api/animator/imageNames`,
      axiosHeaders
    )
    if (imageNames && imageNames !== null) {
      return imageNames.map(image => `/assets/ecogesture/${image}`)
    }
    return []
  } catch (e) {
    console.error('error', e)
    return []
  }
}