import { describe, it, expect } from "vitest";
import {
  shouldShowTransition,
  shouldShowAircraft,
  SERVICE_ICON_MAP,
  MEAL_LINKS,
} from "./shared.js";
import type { IFlightTransitionItem } from "../../types.js";

describe("shouldShowTransition", () => {
  const validItem: IFlightTransitionItem = {
    start: {} as never,
    end: {} as never,
    status: "InProgress",
    isActual: true,
  };

  it("returns false for Schedule viewType", () => {
    expect(shouldShowTransition(validItem, "Scheduled", "Schedule")).toBe(false);
  });

  it("returns false when leg is Cancelled", () => {
    expect(shouldShowTransition(validItem, "Cancelled", "Onlineboard")).toBe(false);
  });

  it("returns false when item is undefined", () => {
    expect(shouldShowTransition(undefined, "Scheduled", "Onlineboard")).toBe(false);
  });

  // TIRREDESIGN-7: the rule now reads the API's isActual flag rather
  // than inferring from status, so a transition marked inactive hides
  // even if its status has left Scheduled (e.g. a cancelled boarding
  // phase that hasn't been cleared to Scheduled).
  it("returns false when item.isActual is false", () => {
    const inactive = { ...validItem, isActual: false };
    expect(shouldShowTransition(inactive, "Scheduled", "Onlineboard")).toBe(false);
  });

  it("returns true when item.isActual is true on Onlineboard", () => {
    expect(shouldShowTransition(validItem, "Scheduled", "Onlineboard")).toBe(true);
  });
});

describe("shouldShowAircraft", () => {
  it("returns true when actual.title exists", () => {
    expect(shouldShowAircraft({ aircraft: { actual: { title: "A320" } } })).toBe(true);
  });

  it("returns true when scheduled.title exists", () => {
    expect(shouldShowAircraft({ aircraft: { scheduled: { title: "A320" } } })).toBe(true);
  });

  it("returns true when configuration exists", () => {
    expect(
      shouldShowAircraft({
        aircraft: { configuration: { seats: [{ type: "Economy", count: 120 }] } },
      }),
    ).toBe(true);
  });

  it("returns false when no aircraft info", () => {
    expect(shouldShowAircraft({})).toBe(false);
    expect(shouldShowAircraft({ aircraft: {} })).toBe(false);
  });

  it("returns false when titles are empty strings", () => {
    expect(shouldShowAircraft({ aircraft: { actual: { title: "" }, scheduled: { title: "" } } })).toBe(false);
  });
});

describe("SERVICE_ICON_MAP", () => {
  it("maps all 8 service IDs", () => {
    expect(SERVICE_ICON_MAP[1]).toBe("shopping");
    expect(SERVICE_ICON_MAP[2]).toBe("space");
    expect(SERVICE_ICON_MAP[3]).toBe("taxi");
    expect(SERVICE_ICON_MAP[4]).toBe("wifi");
    expect(SERVICE_ICON_MAP[5]).toBe("gsm");
    expect(SERVICE_ICON_MAP[6]).toBe("entertaintment");
    expect(SERVICE_ICON_MAP[7]).toBe("entertaintment");
    expect(SERVICE_ICON_MAP[8]).toBe("seat_reservation");
  });
});

describe("MEAL_LINKS", () => {
  it("has links for Economy, Comfort, Business", () => {
    expect(MEAL_LINKS.Economy).toContain("meal-type_0");
    expect(MEAL_LINKS.Comfort).toContain("meal-type_1");
    expect(MEAL_LINKS.Business).toContain("meal-type_2");
  });
});
