/**
 * @ai-summary Helper utilities for working with saved stations data
 */

import { OctanePreference, SavedStation, SaveStationData } from '../types/stations.types';

export function resolveSavedStationPlaceId(station: SavedStation): string | undefined {
  return station.placeId || station.station?.placeId || station.stationId;
}

export function resolveSavedStationName(station: SavedStation): string {
  return (
    station.nickname ||
    station.name ||
    station.station?.name ||
    'Saved Station'
  );
}

export function resolveSavedStationAddress(station: SavedStation): string {
  return station.address || station.station?.address || '';
}

export function resolveSavedStationCoordinates(
  station: SavedStation
): { latitude: number; longitude: number } | undefined {
  const lat = station.latitude ?? station.station?.latitude;
  const lng = station.longitude ?? station.station?.longitude;

  if (lat === undefined || lng === undefined) {
    return undefined;
  }

  return { latitude: lat, longitude: lng };
}

export function getOctanePreferenceFromFlags(
  has93Octane: boolean,
  has93OctaneEthanolFree: boolean
): OctanePreference {
  if (!has93Octane) {
    return 'none';
  }

  return has93OctaneEthanolFree ? 'ethanol_free' : 'with_ethanol';
}

export function octanePreferenceToFlags(
  preference: OctanePreference
): Pick<SaveStationData, 'has93Octane' | 'has93OctaneEthanolFree'> {
  if (preference === 'none') {
    return {
      has93Octane: false,
      has93OctaneEthanolFree: false
    };
  }

  if (preference === 'ethanol_free') {
    return {
      has93Octane: true,
      has93OctaneEthanolFree: true
    };
  }

  return {
    has93Octane: true,
    has93OctaneEthanolFree: false
  };
}
