import { S } from './state.js';
import { esc, toast } from './utils.js';
export function loadSoundCloudImporter() {
// Hint is managed by the unified importer input in spotify.js
}
export function clearSoundCloudPreview() {
// no-op — handled by onImporterInput
}
export async function resolveSoundCloudLink() {
const url = document.getElementById('sc-link-input')?.value.trim()
|| document.getElementById('importer-link-input')?.value.trim();
if (!url) return;
if (!url.includes('soundcloud.com')) { toast('⚠ Kein SoundCloud-Link'); return; }
if (!S.cfg.backendUrl) { toast('⚠ Backend URL in Einstellungen konfigurieren'); return; }
const preview = document.getElementById('sc-preview');
const inner = document.getElementById('sc-preview-inner');
const section = document.getElementById('sc-tracks-section');
preview.style.display = '';
section.style.display = 'none';
inner.innerHTML = '
';
try {
const res = await fetch(`${S.cfg.backendUrl}/api/soundcloud/resolve`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url }),
signal: AbortSignal.timeout(35000),
});
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err.error || `HTTP ${res.status}`);
}
const data = await res.json();
window._scResolved = data;
_renderSoundCloudPreview(data);
} catch(e) {
if (e.name === 'TimeoutError' || e.name === 'AbortError') {
inner.innerHTML = `Zeitüberschreitung — Backend antwortet nicht. Läuft yt-dlp?
`;
return;
}
inner.innerHTML = `${esc(e.message)}
`;
}
}
export function scDlAll() {
const data = window._scResolved;
if (!data) return;
window.executeSpotifyDownload({
type: 'playlist',
title: data.name,
artist: data.artist,
artworkUrl: data.artworkUrl,
tracks: data.tracks,
});
}
export function scDlSingle() {
const data = window._scResolved;
if (!data?.tracks?.[0]) return;
const t = data.tracks[0];
window.executeSpotifyDownload({
type: 'track',
title: t.trackName,
artist: t.artistName,
artworkUrl: t.artworkUrl,
tracks: [t],
});
}
export function scDlTrack(idx) {
const data = window._scResolved;
const t = data?.tracks?.[idx];
if (!t) return;
window.executeSpotifyDownload({
type: 'track',
title: t.trackName,
artist: t.artistName,
artworkUrl: t.artworkUrl,
tracks: [t],
});
}
function _renderSoundCloudPreview(data) {
const inner = document.getElementById('sc-preview-inner');
const section = document.getElementById('sc-tracks-section');
const list = document.getElementById('sc-tracks-list');
const titleEl = document.getElementById('sc-tracks-title');
const count = data.tracks?.length || 0;
const art = data.artworkUrl || '';
let actionsHtml = '';
if (data.type === 'track' && count > 0) {
const t = data.tracks[0];
actionsHtml = `
`;
} else {
actionsHtml = `
`;
}
const artHtml = art
? `
`
: '';
inner.innerHTML = `
SoundCloud ${data.type === 'playlist' ? 'Playlist' : 'Track'}
${esc(data.name)}
${esc(data.artist)}${count > 1 ? ` · ${count} Tracks` : ''}
${actionsHtml}
`;
titleEl.textContent = `${count} Track${count !== 1 ? 's' : ''}`;
list.innerHTML = '';
(data.tracks || []).forEach((t, i) => {
const el = document.createElement('div');
el.className = 'gtrack-item';
const artT = t.artworkUrl || '';
el.innerHTML = `
${artT ? `

` : '
🎵
'}
${esc(t.trackName || '—')}
${esc(t.artistName || '—')}${t.genre ? ' · ' + esc(t.genre) : ''}${t.duration ? ' · ' + Math.round(t.duration / 60) + ' min' : ''}
`;
list.appendChild(el);
});
section.style.display = '';
}