import {
getPlaybackState,
hasDeviceSelector,
pausePlayback,
startOrResumePlayback,
} from '../lib/spotify-api.ts'
import type { PlayPauseParams } from '../lib/export-types.ts'
/**
* Toggle Spotify playback between play and pause on the active or specified device.
* Prefer `deviceName` when targeting a device; omit selectors for the active device.
* Pass `dryRun: true` to preview the action without changing playback.
* @example
* import playPause from 'kody:@kody/spotify/play-pause'
* const preview = await playPause({ dryRun: true })
*/
export default async function playPause(params: PlayPauseParams = {}) {
const current = await getPlaybackState(params)
const playbackDeviceId = current.playback?.device?.id ?? undefined
const targetedParams = hasDeviceSelector(params)
? params
: { ...params, deviceId: params.deviceId ?? playbackDeviceId }
if (current.nothingPlaying) {
return await startOrResumePlayback(params)
}
if (current.playback?.is_playing) {
return await pausePlayback(targetedParams)
}
return await startOrResumePlayback({
...targetedParams,
forceCollection: false,
})
}