import type { CustomSongMeta } from '../../stores/custom-songs/types'
import { estimateDuration } from './file-utils'
import type { SongBundle } from './types'

const PALETTE = [
  '#d946ef',
  '#e91e63',
  '#f44336',
  '#ff9800',
  '#4caf50',
  '#2196f3',
  '#9c27b0',
  '#00bcd4',
  '#ff5722',
  '#607d8b',
]

export function buildCustomSongMeta(bundle: SongBundle, bgName: string, bannerName: string): CustomSongMeta {
  return {
    id: bundle.id,
    title: bundle.chart.title,
    artist: bundle.chart.artist,
    bpm: bundle.chart.bpm,
    difficulty: Math.min(
      5,
      Math.max(
        1,
        Math.round(
          (bundle.chart.difficulties.find((d) => d.difficulty === bundle.selectedDifficulty)?.meter ?? 5) /
            2.4,
        ),
      ),
    ),
    duration: estimateDuration(bundle.chart),
    genre: 'Custom',
    color: PALETTE[Math.floor(Math.random() * PALETTE.length)],
    selectedDifficulty: bundle.selectedDifficulty,
    simplify: bundle.simplify,
    audioFile: bundle.audioFile.name,
    chartFile: bundle.chartFile.name,
    bgFile: bgName,
    bannerFile: bannerName,
  }
}
