import { getFilterType } from "@/lib/peqAudio"; import { normalizePeqFiltersForSubmit, type PeqFilter, type PeqState } from "@/lib/luxsinApi"; import type { EqBand } from "./types"; import { clampPeqPresetName } from "./peqPresetName"; export function cloneBands(source: EqBand[]) { return source.map((band) => ({ ...band })); } /** Dev: log decoded syncPeq JSON after EQ page fetches device PEQ state. */ export function logEqSyncPeqDev(source: string, data: PeqState): void { if (!import.meta.env.DEV) return; console.log(`[EQ syncPeq] ${source}`, data); } export async function fetchEqSyncPeq( api: { getPeqState: () => Promise }, source: string, ): Promise { const data = await api.getPeqState(); logEqSyncPeqDev(source, data); return data; } export function buildPeqCatalogSyncKey( remote: Pick | null | undefined, devicePeqSelect?: number, ): string { const items = remote?.peq; if (!items?.length) return items ? "empty" : ""; const select = devicePeqSelect ?? remote?.peqSelect ?? 0; return `${select}|${items.map((p) => p.name).join("\u0001")}`; } export function getUniquePresetName(base: string, existingNames: string[]) { const clippedBase = clampPeqPresetName(base); if (!existingNames.includes(clippedBase)) return clippedBase; let index = 1; while (existingNames.includes(clampPeqPresetName(`${clippedBase}_${index}`))) { index += 1; } return clampPeqPresetName(`${clippedBase}_${index}`); } /** UI band row → device `PeqFilter` (fc + numeric type), same as legacy `getFilterVal` mapping via `getFilterType`. */ export function bandToPeqFilter(b: { freq: number; gain: number; q: number; type: string | number; }): PeqFilter { return { fc: b.freq, gain: b.gain, q: b.q, type: getFilterType(b.type), }; } export function importedFiltersToPeqFilters(filters: unknown): PeqFilter[] { return normalizePeqFiltersForSubmit(filters).map((f) => { const item = f as unknown as Record; return { fc: Number(item.fc ?? item.freq ?? item.frequency ?? 1000), gain: Number(item.gain ?? 0), q: Number(item.q ?? 1), type: getFilterType(item.type as string | number), }; }); }