xref: /MusicFree/src/utils/mediaIndexMap.ts (revision 740e39476f71e0e17304d812ac0a4c4cdc183ed1)
1*740e3947S猫头猫export interface IIndexMap {
25500cea7S猫头猫    getIndexMap: () => Record<string, Record<string, number>>;
3*740e3947S猫头猫    getIndex: (mediaItem: ICommon.IMediaBase) => number;
4bef6dda8S猫头猫    has: (mediaItem: ICommon.IMediaBase) => boolean;
55500cea7S猫头猫}
65500cea7S猫头猫
75500cea7S猫头猫export function createMediaIndexMap(
8bef6dda8S猫头猫    mediaItems: ICommon.IMediaBase[],
95500cea7S猫头猫): IIndexMap {
105500cea7S猫头猫    const indexMap: Record<string, Record<string, number>> = {};
115500cea7S猫头猫
12bef6dda8S猫头猫    mediaItems.forEach((item, index) => {
135500cea7S猫头猫        // 映射中不存在
145500cea7S猫头猫        if (!indexMap[item.platform]) {
155500cea7S猫头猫            indexMap[item.platform] = {
165500cea7S猫头猫                [item.id]: index,
175500cea7S猫头猫            };
185500cea7S猫头猫        } else {
195500cea7S猫头猫            // 修改映射
205500cea7S猫头猫            indexMap[item.platform][item.id] = index;
215500cea7S猫头猫        }
225500cea7S猫头猫    });
235500cea7S猫头猫
245500cea7S猫头猫    function getIndexMap() {
255500cea7S猫头猫        return indexMap;
265500cea7S猫头猫    }
275500cea7S猫头猫
28bef6dda8S猫头猫    function getIndex(mediaItem: ICommon.IMediaBase) {
29bef6dda8S猫头猫        if (!mediaItem) {
305500cea7S猫头猫            return -1;
315500cea7S猫头猫        }
32bef6dda8S猫头猫        return indexMap[mediaItem.platform]?.[mediaItem.id] ?? -1;
335500cea7S猫头猫    }
345500cea7S猫头猫
35bef6dda8S猫头猫    function has(mediaItem: ICommon.IMediaBase) {
36bef6dda8S猫头猫        if (!mediaItem) {
375500cea7S猫头猫            return false;
385500cea7S猫头猫        }
395500cea7S猫头猫
40bef6dda8S猫头猫        return indexMap[mediaItem.platform]?.[mediaItem.id] > -1;
415500cea7S猫头猫    }
425500cea7S猫头猫
435500cea7S猫头猫    return {
445500cea7S猫头猫        getIndexMap,
455500cea7S猫头猫        getIndex,
465500cea7S猫头猫        has,
475500cea7S猫头猫    };
485500cea7S猫头猫}
49