// The tabbed pages and the rule for which tab each one opens on.
//
// Every bar in the app is arrangeable, and a bar the user dragged says what they
// want to see first — so a page opens on whichever tab now leads it. The Settings
// picker overrides that per page, for the case where the reading order and the
// landing tab are genuinely different wishes.
//
// The key lists live here rather than in the three views because Settings offers
// the picker for all of them and would otherwise duplicate every list.

import { prefs } from "../prefs.js";

export const CHARGING_TABS = ["public", "home"];

// A car's tabs in their default order. Information sits second because the
// connected service, when there is one, is what you came to look at.
export const CAR_TABS = [
  "provider", "info", "services", "technical", "maintenance", "fuel",
  "charging", "documents", "parts", "reminders",
];

export const SETTINGS_TABS = ["personal", "users", "organization", "integrations"];

// The pages the picker offers, with the i18n key for each tab's label. A car's
// connected-service tab was translated as car.tabs.connected long before it
// became a key, hence the one remapping.
export const TAB_SURFACES = [
  { surface: "charging", keys: CHARGING_TABS, labelKey: (key) => `charging.tabs.${key}` },
  {
    surface: "car",
    keys: CAR_TABS,
    labelKey: (key) => `car.tabs.${key === "provider" ? "connected" : key}`,
  },
  { surface: "settings", keys: SETTINGS_TABS, labelKey: (key) => `settings.tabs.${key}` },
];

// The tab a page should open on. `available` is the bar as it will actually
// render — already arranged, with hidden tabs and ones that don't apply dropped
// — so a saved default that no longer has a button falls through to the front of
// the bar rather than opening nothing.
export function defaultTabFor(surface, available) {
  const keys = available || [];
  const chosen = prefs.defaultTabs?.[surface];
  if (chosen && keys.includes(chosen)) return chosen;
  return keys[0] || "";
}
