import { openMasterDashboardPath } from "../config/satPortal.js";
import {
  SWI_PERSONAL_DASHBOARD_WIDGETS,
  SWI_WIDGET_IDS,
  getSwiWidgetMeta,
} from "../config/personalizedDashboard.js";

/** Same keys as Master Personal Dashboard (stored on Master origin). */
export const PD_WIDGETS_KEY = "personalizedDashboardWidgets";
export const PD_LAYOUT_KEY = "personalizedDashboardLayout";

export function isSwiPersonalDashboardWidget(widgetId) {
  return SWI_WIDGET_IDS.includes(widgetId);
}

/**
 * Open Master Personal Dashboard and queue a widget add via ?addWidget=.
 * @param {string} widgetId
 */
export function openPersonalDashboardWithWidget(widgetId) {
  if (!isSwiPersonalDashboardWidget(widgetId)) {
    console.warn(`Unknown SWI personal dashboard widget: ${widgetId}`);
    return;
  }

  openMasterDashboardPath("/personalized-dashboard", { addWidget: widgetId });
}

export function buildSwiWidgetCatalogBySection() {
  return SWI_PERSONAL_DASHBOARD_WIDGETS.reduce((acc, widget) => {
    const key = widget.section || "Other";
    if (!acc[key]) acc[key] = [];
    acc[key].push(widget);
    return acc;
  }, {});
}

export { getSwiWidgetMeta, SWI_PERSONAL_DASHBOARD_WIDGETS, SWI_WIDGET_IDS };
