/** Priority1 三种 Shipment Type 的页面配置 */

export type Priority1ShipmentType = "ltl" | "ftl" | "expedited";

export type FtlTrailerType =
  | "dry_van"
  | "open_deck"
  | "temperature_controlled"
  | "ftl_expedited";

export type ExpeditedTrailerType =
  | "sprinter_van"
  | "small_straight"
  | "large_straight";

export type FtlAdditionalService = "full_ftl" | "partial_ptl" | "team_drivers";

export type OpenDeckSubtype =
  | "48 Foot Flatbed"
  | "53 Foot Flatbed"
  | "48 Foot Step Deck"
  | "53 Foot Step Deck"
  | "Removable Gooseneck"
  | "Double Drop"
  | "Conestoga"
  | "Hot Shot/Gooseneck";

export const OPEN_DECK_SUBTYPES: OpenDeckSubtype[] = [
  "48 Foot Flatbed",
  "53 Foot Flatbed",
  "48 Foot Step Deck",
  "53 Foot Step Deck",
  "Removable Gooseneck",
  "Double Drop",
  "Conestoga",
  "Hot Shot/Gooseneck",
];

export type ShipmentTypeConfig = {
  id: Priority1ShipmentType;
  displayName: string;
  checkboxSelector: string;
  cardText: RegExp;
  step2HashPath: string;
  step2ReadySelector: string;
};

export const SHIPMENT_TYPE_CONFIG: Record<
  Priority1ShipmentType,
  ShipmentTypeConfig
> = {
  ltl: {
    id: "ltl",
    displayName: "Less Than Truckload（零担 LTL）",
    checkboxSelector: "#cb_st_ltl",
    cardText: /^Less Than Truckload/i,
    step2HashPath: "#V4.1%20Step%202%20-%20LTL",
    step2ReadySelector: "#length",
  },
  ftl: {
    id: "ftl",
    displayName: "Full Truckload（整车 FTL）",
    checkboxSelector: "#cb_st_ftl",
    cardText: /^Full Truckload/i,
    step2HashPath: "#New%20Step%202%20-%20Truckload",
    step2ReadySelector: "#Commodity",
  },
  expedited: {
    id: "expedited",
    displayName: "Expedited（加急）",
    checkboxSelector: "#cb_st_exp",
    cardText: /^Expedited/i,
    step2HashPath: "#New%20Step%202%20-%20Expedited",
    step2ReadySelector: "#cb_stt_sv",
  },
};

export const FTL_TRAILER: Record<
  FtlTrailerType,
  { selector: string; label: string }
> = {
  dry_van: { selector: "#cb_stt_ftl1", label: "Dry Van" },
  open_deck: { selector: "#cb_stt_ftl2", label: "Open Deck" },
  temperature_controlled: {
    selector: "#cb_stt_ftl3",
    label: "Temperature Controlled",
  },
  ftl_expedited: { selector: "#cb_stt_ftl4", label: "Expedited" },
};

export const FTL_ADDITIONAL: Record<
  FtlAdditionalService,
  { selector: string; label: string }
> = {
  full_ftl: { selector: "#cb_as_ftl1", label: "Full FTL" },
  partial_ptl: { selector: "#cb_as_ftl2", label: "Partial PTL" },
  team_drivers: { selector: "#cb_as_ftl3", label: "Team Drivers" },
};

export const EXPEDITED_TRAILER: Record<
  ExpeditedTrailerType,
  { selector: string; label: string }
> = {
  sprinter_van: { selector: "#cb_stt_sv", label: "Sprinter Van" },
  small_straight: {
    selector: "#cb_stt_ssbt",
    label: "Small Straight Box Truck",
  },
  large_straight: {
    selector: "#cb_stt_lsbt",
    label: "Large Straight Box Truck",
  },
};

export function getShipmentConfig(
  type: Priority1ShipmentType,
): ShipmentTypeConfig {
  return SHIPMENT_TYPE_CONFIG[type];
}

/** 加急 / FTL 内嵌加急 — 仅 Large Straight 出现「附加服务 · 尾板」 */
export function expeditedLiftgateAvailable(
  trailer: ExpeditedTrailerType,
): boolean {
  return trailer === "large_straight";
}
