Repository navigation
Expand file tree
/
Copy pathplaybackDeleteAllAbove.ts
More file actions
70 lines (64 loc) · 2.47 KB
/
Copy pathplaybackDeleteAllAbove.ts
File metadata and controls
70 lines (64 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { normalizeFilePathForComparison } from './filePathComparison'
export type PlaybackDeleteAllAboveSong = {
filePath?: string | null
setItemId?: string | null
mixtapeItemId?: string | null
}
export type PlaybackDeleteAllAboveTarget<
T extends PlaybackDeleteAllAboveSong = PlaybackDeleteAllAboveSong
> = {
listUuid: string
playingIndex: number
songs: T[]
}
const normalizePlaybackFilePath = (
filePath: string | undefined | null,
caseInsensitiveFilePath: boolean
) => normalizeFilePathForComparison(filePath, caseInsensitiveFilePath)
export const isPlaybackDeleteAllAboveBlockedLibraryType = (type?: string | null) =>
type === 'setList' || type === 'mixtapeList'
export const findPlayingSongIndexInList = <T extends PlaybackDeleteAllAboveSong>(
list: readonly T[],
playingSong: T | null | undefined,
caseInsensitiveFilePath = false
) => {
if (!playingSong || !Array.isArray(list) || list.length === 0) return -1
const byRef = list.indexOf(playingSong)
if (byRef !== -1) return byRef
const setItemId = String(playingSong.setItemId || '')
if (setItemId) {
const index = list.findIndex((item) => String(item.setItemId || '') === setItemId)
if (index !== -1) return index
}
const mixtapeItemId = String(playingSong.mixtapeItemId || '')
if (mixtapeItemId) {
const index = list.findIndex((item) => String(item.mixtapeItemId || '') === mixtapeItemId)
if (index !== -1) return index
}
const filePath = normalizePlaybackFilePath(playingSong.filePath, caseInsensitiveFilePath)
if (!filePath) return -1
return list.findIndex(
(item) => normalizePlaybackFilePath(item.filePath, caseInsensitiveFilePath) === filePath
)
}
export const resolvePlaybackDeleteAllAboveTarget = <T extends PlaybackDeleteAllAboveSong>(params: {
listUuid?: string | null
listData?: readonly T[] | null
playingSong?: T | null
libraryType?: string | null
caseInsensitiveFilePath: boolean
}): PlaybackDeleteAllAboveTarget<T> | null => {
const listUuid = String(params.listUuid || '')
if (!listUuid) return null
if (isPlaybackDeleteAllAboveBlockedLibraryType(params.libraryType)) return null
const listData = Array.isArray(params.listData) ? params.listData : []
const playingIndex = findPlayingSongIndexInList(
listData,
params.playingSong || null,
params.caseInsensitiveFilePath
)
if (playingIndex <= 0) return null
const songs = listData.slice(0, playingIndex)
if (songs.length === 0) return null
return { listUuid, playingIndex, songs }
}