// The parts a service record can say were changed.
//
// One list, because each part has to appear in three places at once — the
// Changed parts section of the service form, the column of the Service history
// table, and the panel that column opens — and three hand-kept copies would
// drift the first time somebody adds a part. Adding one here (plus its boolean
// on the API's service_records collection) is the whole job.
//
// `field` is the record's own boolean; `label` is the translation key, shared
// with the form so the table and the dialog can't end up wording the same part
// differently. `initial` is what a *new* record starts with: an oil change is
// the reason for most services, the filters are the exception.
export const SERVICE_PARTS = [
  { key: "oil", field: "changedOil", label: "forms.service.oil", initial: true },
  { key: "engineFilter", field: "changedEngineAirFilter", label: "forms.service.engineFilter", initial: false },
  { key: "cabinFilter", field: "changedCabinAirFilter", label: "forms.service.cabinFilter", initial: false },
];

// The parts a car actually records, which is every one it hasn't switched off.
// An EV changes no oil, and a checkbox nobody will ever tick is one more thing
// to read past on every service. The hidden set rather than the visible one, so
// a part added in a later release is on by default.
export function visibleParts(car) {
  const hidden = car?.hiddenServiceParts || [];
  return SERVICE_PARTS.filter((part) => !hidden.includes(part.key));
}

// The parts this record says were changed, among the ones the car still shows.
// A record written before a part existed simply doesn't carry its field, which
// reads as "not changed" rather than as a missing value — that service genuinely
// didn't change it.
//
// A part switched off disappears from the history as well as from the form, the
// same way a hidden column does: what "I don't record this" means is that it
// stops taking up room, not that it takes up room saying nothing. Its stored
// boolean is left alone, so switching it back on brings the old records' chips
// back with it.
export function changedParts(service, car) {
  return visibleParts(car).filter((part) => !!service?.[part.field]);
}
