import type {
  ShipBehaviorDefinitionSnapshot,
  ShipOrderDefinitionSnapshot,
} from "./contractsShipAutomation";
import { viewerPinia } from "./ui/stores/pinia";
import { useShipAutomationCatalogStore } from "./ui/stores/shipAutomationCatalogStore";

function titleCaseAutomationId(value: string): string {
  return value
    .replace(/([a-z])([A-Z])/g, "$1 $2")
    .replace(/[-_]+/g, " ")
    .replace(/\s+/g, " ")
    .trim()
    .replace(/\b\w/g, (part) => part.toUpperCase());
}

function titleCaseStatus(value: string): string {
  return value
    .replace(/([a-z])([A-Z])/g, "$1 $2")
    .trim();
}

export function formatShipAutomationSupportStatus(value: string | null | undefined): string | null {
  return value ? titleCaseStatus(value) : null;
}

export function getShipBehaviorDefinition(id: string): ShipBehaviorDefinitionSnapshot | null {
  return useShipAutomationCatalogStore(viewerPinia).behaviorMap.get(id) ?? null;
}

export function getShipOrderDefinition(id: string): ShipOrderDefinitionSnapshot | null {
  return useShipAutomationCatalogStore(viewerPinia).orderMap.get(id) ?? null;
}

export function getShipBehaviorLabel(id: string): string {
  return getShipBehaviorDefinition(id)?.label ?? titleCaseAutomationId(id);
}

export function getShipOrderLabel(id: string): string {
  return getShipOrderDefinition(id)?.label ?? titleCaseAutomationId(id);
}

export function getShipBehaviorSupportStatusLabel(id: string): string | null {
  const status = getShipBehaviorDefinition(id)?.supportStatus;
  return formatShipAutomationSupportStatus(status);
}

export function getShipOrderSupportStatusLabel(id: string): string | null {
  const status = getShipOrderDefinition(id)?.supportStatus;
  return formatShipAutomationSupportStatus(status);
}

export function getShipBehaviorNotes(id: string): string | null {
  return getShipBehaviorDefinition(id)?.notes ?? null;
}

export function getShipOrderNotes(id: string): string | null {
  return getShipOrderDefinition(id)?.notes ?? null;
}
