export function kebabify(str: string) {
  return str.replace(/\/|\s/g, '-').replace(/\(|\)/g, '')
}

export function ignoreStyles(mode: { name: string }) {
  if (mode.name === 'Style') {
    // Ignore Figma Styles, we only want to process variables.
    // But the exported variables.json includes Figma Styles too.
    return false
  }
  return true
}

// Some tokens are the same for all themes. Group them into this theme.
export const FALLBACK_THEME = 'base'

// The variables exported from Figma are not grouped by theme.
// We used this function to help us group by theme.
// Returns the theme the given token belongs to by matching theme names inside
// the token name.
export function getThemeForToken(
  token: string | { name: string },
  mode: { name: string },
  themes: Map<string, unknown>
) {
  // If the given value is an object and has a name property, use that as comparison value.
  const compare = typeof token === 'string' ? token : token.name
  const theme = Array.from(themes.keys()).find((theme) => {
    // Match against "theme/", use that if it matches
    if (compare.indexOf(theme + '/') >= 0) {
      return theme
    }

    // Match against mode, use that if it matches
    if (mode.name === theme) {
      return theme
    }
  })

  // If a theme was found, use that
  if (theme) {
    return theme
  }

  // If no theme was found, return the fallback
  return FALLBACK_THEME
}
