import * as Sentry from '@sentry/node' ; import { Field , NumberField , StringField } from 'src/remotedb/fields' ; /**
 * Item types associated to the MenuItem message type.
 */ export enum ItemType { Path = 0x0000 , Folder = 0x0001 , AlbumTitle = 0x0002 , Disc = 0x0003 , TrackTitle = 0x0004 , Genre = 0x0006 , Artist = 0x0007 , Playlist = 0x0008 , Rating = 0x000a , Duration = 0x000b , Tempo = 0x000d , Label = 0x000e , Key = 0x000f , BitRate = 0x0010 , Year = 0x0011 , Comment = 0x0023 , HistoryPlaylist = 0x0024 , OrigianlArtist = 0x0028 , Remixer = 0x0029 , DateAdded = 0x002e , Unknown01 = 0x002f , Unknown02 = 0x002a , ColorNone = 0x0013 , ColorPink = 0x0014 , ColorRed = 0x0015 , ColorOrange = 0x0016 , ColorYellow = 0x0017 , ColorGreen = 0x0018 , ColorAqua = 0x0019 , ColorBlue = 0x001a , ColorPurple = 0x001b , MenuGenre = 0x0080 , MenuArtist = 0x0081 , MenuAlbum = 0x0082 , MenuTrack = 0x0083 , MenuPlaylist = 0x0084 , MenuBPM = 0x0085 , MenuRating = 0x0086 , MenuYear = 0x0087 , MenuRemixer = 0x0088 , MenuLabel = 0x0089 , MenuOriginal = 0x008a , MenuKey = 0x008b , MenuColor = 0x008e , MenuFolder = 0x0090 , MenuSearch = 0x0091 , MenuTime = 0x0092 , MenuBit = 0x0093 , MenuFilename = 0x0094 , MenuHistory = 0x0095 , MenuAll = 0x00a0 , TrackTitleAlbum = 0x0204 , TrackTitleGenre = 0x0604 , TrackTitleArtist = 0x0704 , TrackTitleRating = 0x0a04 , TrackTitleTime = 0x0b04 , TrackTitleBPM = 0x0d04 , TrackTitleLabel = 0x0e04 , TrackTitleKey = 0x0f04 , TrackTitleBitRate = 0x1004 , TrackTitleColor = 0x1a04 , TrackTitleComment = 0x2304 , TrackTitleOriginalArtist = 0x2804 , TrackTitleRemixer = 0x2904 , TrackTitleDJPlayCount = 0x2a04 , MenuTrackTitleDateAdded = 0x2e04 } /**
 * All items have 12 arguments of these types
 */ type ItemArgs = [ NumberField , NumberField , NumberField , StringField , NumberField , StringField , NumberField < ItemType > , NumberField , NumberField , NumberField , NumberField , NumberField ] ; /**
 * Convert a message item argument lists to a structured intermediate object
 * for more clear access.
 */ const makeItemData = ( args ) => ( { parentId : args [ 0 ] . value , mainId : args [ 1 ] . value , label1 : args [ 3 ] . value , label2 : args [ 5 ] . value , type : args [ 6 ] . value , artworkId : args [ 8 ] . value , } ) ; type ItemData = ReturnType < typeof makeItemData > ; /**
 * Generic transformer for items that include just an id and label
 */ const mapIdName = ( a ) => ( { id : a . mainId , name : a . label1 , } ) ; /**
 * Maps item types to structured objects
 */ const transformItem = { [ ItemType . Path ] : ( a ) => ( { path : a . label1 } ) , [ ItemType . TrackTitle ] : ( a ) => ( { id : a . mainId , title : a . label1 , artworkId : a . artworkId , } ) , [ ItemType . AlbumTitle ] : mapIdName , [ ItemType . Artist ] : mapIdName , [ ItemType . Genre ] : mapIdName , [ ItemType . Label ] : mapIdName , [ ItemType . Key ] : mapIdName , [ ItemType . OrigianlArtist ] : mapIdName , [ ItemType . Remixer ] : mapIdName , [ ItemType . BitRate ] : ( a ) => ( { bitrate : a . mainId } ) , [ ItemType . Comment ] : ( a ) => ( { comment : a . label1 } ) , [ ItemType . Year ] : ( a ) => ( { year : Number ( a . label1 ) } ) , [ ItemType . Rating ] : ( a ) => ( { rating : a . mainId } ) , [ ItemType . Tempo ] : ( a ) => ( { bpm : a . mainId / 100 } ) , [ ItemType . Duration ] : ( a ) => ( { duration : a . mainId } ) , [ ItemType . Unknown01 ] : ( _ ) => null , [ ItemType . Unknown02 ] : ( _ ) => null , [ ItemType . ColorNone ] : mapIdName , [ ItemType . ColorPink ] : mapIdName , [ ItemType . ColorRed ] : mapIdName , [ ItemType . ColorOrange ] : mapIdName , [ ItemType . ColorYellow ] : mapIdName , [ ItemType . ColorGreen ] : mapIdName , [ ItemType . ColorAqua ] : mapIdName , [ ItemType . ColorBlue ] : mapIdName , [ ItemType . ColorPurple ] : mapIdName , [ ItemType . Folder ] : mapIdName , [ ItemType . Playlist ] : mapIdName , [ ItemType . Disc ] : ( a ) => a , [ ItemType . HistoryPlaylist ] : ( a ) => a , [ ItemType . DateAdded ] : ( a ) => a , [ ItemType . MenuGenre ] : ( a ) => a , [ ItemType . MenuArtist ] : ( a ) => a , [ ItemType . MenuAlbum ] : ( a ) => a , [ ItemType . MenuTrack ] : ( a ) => a , [ ItemType . MenuPlaylist ] : ( a ) => a , [ ItemType . MenuBPM ] : ( a ) => a , [ ItemType . MenuRating ] : ( a ) => a , [ ItemType . MenuYear ] : ( a ) => a , [ ItemType . MenuRemixer ] : ( a ) => a , [ ItemType . MenuLabel ] : ( a ) => a , [ ItemType . MenuOriginal ] : ( a ) => a , [ ItemType . MenuKey ] : ( a ) => a , [ ItemType . MenuColor ] : ( a ) => a , [ ItemType . MenuFolder ] : ( a ) => a , [ ItemType . MenuSearch ] : ( a ) => a , [ ItemType . MenuTime ] : ( a ) => a , [ ItemType . MenuBit ] : ( a ) => a , [ ItemType . MenuFilename ] : ( a ) => a , [ ItemType . MenuHistory ] : ( a ) => a , [ ItemType . MenuAll ] : ( a ) => a , [ ItemType . TrackTitleAlbum ] : ( a ) => a , [ ItemType . TrackTitleGenre ] : ( a ) => a , [ ItemType . TrackTitleArtist ] : ( a ) => a , [ ItemType . TrackTitleRating ] : ( a ) => a , [ ItemType . TrackTitleTime ] : ( a ) => a , [ ItemType . TrackTitleBPM ] : ( a ) => a , [ ItemType . TrackTitleLabel ] : ( a ) => a , [ ItemType . TrackTitleKey ] : ( a ) => a , [ ItemType . TrackTitleBitRate ] : ( a ) => a , [ ItemType . TrackTitleColor ] : ( a ) => a , [ ItemType . TrackTitleComment ] : ( a ) => a , [ ItemType . TrackTitleOriginalArtist ] : ( a ) => a , [ ItemType . TrackTitleRemixer ] : ( a ) => a , [ ItemType . TrackTitleDJPlayCount ] : ( a ) => a , [ ItemType . MenuTrackTitleDateAdded ] : ( a ) => a , } ; /**
 * Represents a generic Item, specialized to a specific item by providing a
 * ItemType to the template.
 */ export type Item < T extends ItemType > = ReturnType < typeof transformItem [ T ] > & { type : T ; } ; /**
 * Maps ItemTypes to Items
 */ export type Items = { [ T in keyof typeof transformItem ] : Item < T > ; } ; /**
 * Translate a list of fields for an item response into a structure object,
 * making items more clear to work with.
 */ export const fieldsToItem = ( args ) => { const itemData = makeItemData ( args as ItemArgs ) ; const { type } = itemData ; let transformer = transformItem [ type ] ; if ( transformer === undefined ) { transformer = ( ) => null ; Sentry . captureMessage ( `No item transformer registered for item type ${ type }` , Sentry . Severity . Error ) ; } return { ... transformer ( itemData ) , type } as Items [ ItemType ] ; } ;