// Marker color logic for the shop-floor map, shared so the PDF export renders
// the exact same colors the map shows on screen. Mirrors the maps defined in
// ShopFloorMap.vue - keep the two in sync if the palette changes.

// Markers sit on TWO very different surfaces: the light blueprint on white, and
// the dark blueprint on the near-black navy card. Every step below clears 3:1
// against both (#ffffff and the dark card, ~#060628), so no marker washes out on
// either. The hues keep their established meaning - only the step changed. The
// old values were far too light to survive white: orange #FF9800 sat at 2.16:1
// and the grey default at 1.88:1.
export const assetTypeColorsMap = {
  machine: '#e03131',          // Red
  computer: '#1976d2',         // Blue
  printer: '#2e7d32',          // Green
  'network device': '#e65100', // Orange
  network_device: '#e65100',   // Orange (alternate key)
  measuring_tool: '#9c4dcc',   // Purple
  'measuring tool': '#9c4dcc'  // Purple (normalized key)
}

// Fallback for an asset with no subtype. Reads as neutral without vanishing:
// 5.4:1 on white, 3.7:1 on the dark card (the old #BDBDBD was 1.88:1 on white).
export const UNSPECIFIED_COLOR = '#546e7a'

const DEFAULT_COLOR = UNSPECIFIED_COLOR

// Canonical form for comparing asset-type strings. The API sends the machine
// type as 'network_device' but subtype keys and labels use 'network device',
// so normalize underscores to spaces before any comparison. Without this,
// network-device subtypes silently fail to match (machine/computer/printer
// are single words and were unaffected, which is why only network broke).
export function normalizeAssetType(assettype) {
  return (assettype || '').toLowerCase().replace(/_/g, ' ')
}

// Color for an asset by its top-level asset type (used when no type filter is
// active, so every type is distinguished by color).
export function getAssetTypeColor(assettype) {
  if (!assettype) return DEFAULT_COLOR
  return assetTypeColorsMap[assettype.toLowerCase()] || DEFAULT_COLOR
}

// The subtype id for an asset, read from the plugin-specific typedata block.
// Returns null when the asset has no subtype.
export function getSubtypeId(asset) {
  if (!asset || !asset.typedata) return null
  const typeLower = normalizeAssetType(asset.assettype)
  if (typeLower === 'machine') return asset.typedata.machinetypeid
  if (typeLower === 'computer') return asset.typedata.computertypeid
  if (typeLower === 'network device') return asset.typedata.networkdevicetypeid
  if (typeLower === 'printer') return asset.typedata.printertypeid
  if (typeLower === 'measuring tool') return asset.typedata.measuringtooltypeid
  return null
}

// Ring color for a marker, keyed on the SURFACE it sits on, not on its fill.
// The ring's job is to separate the marker from the background, so every marker
// in a theme wears the same one - keying it off the fill instead singled out the
// light colors with a black ring while their neighbours kept white ones.
//
// A ring close to its own fill is never a problem: a fill that resembles the
// ring is by definition far from the background, which is what carries the
// separation. So a dark ring on white and a light ring on the dark card work for
// every fill, including a washed-out color a user picked by hand for a subtype.
export function markerRingColor(theme) {
  return theme === 'light' ? '#1b1b1b' : '#ffffff'
}

// Resolve the marker color for an asset the same way ShopFloorMap does: when a
// type is selected, color by subtype (neutral fallback); otherwise by asset type.
export function resolveMarkerColor(asset, { selectedType, subtypeColors = {} }) {
  if (selectedType) {
    const id = getSubtypeId(asset)
    return (id != null && subtypeColors[id]) || DEFAULT_COLOR
  }
  return getAssetTypeColor(asset.assettype)
}
