import React from "react";
import LocalOfferOutlinedIcon from "@mui/icons-material/LocalOfferOutlined";
import ReportIcon from "@mui/icons-material/Report";
import WarningIcon from "@mui/icons-material/Warning";
import { Grid2 as Grid, Stack, Typography } from "@mui/material";
import Card from "../../../components/Card";
import CardContent from "../../../components/Card/CardContent";
import CardHeader from "../../../components/Card/CardHeader";
import CardPill from "../../../components/Card/CardPill";
import Content from "../../../containers/Content";
import { ContribComponent } from "../../../types";
import { SiteItem, SiteItemsResponse } from "../types/contrib";
const parseStatus = (value: string | null | undefined): { label?: string; codes: string[] } => {
if (!value) {
return { codes: [] };
}
const trimmed = value.trim();
const match = /^([^[]+)\[([^\]]*)\]>?$/.exec(trimmed);
if (!match) {
return { label: trimmed, codes: [] };
}
const [, label, codes] = match;
return {
label: label.trim(),
codes: codes
.split(",")
.map((code) => code.trim())
.filter(Boolean),
};
};
const formatItem = (label: string, value: React.ReactNode) => (
{label}
{typeof value === "string" || typeof value === "number" ? (
{value}
) : (
value
)}
);
const ItemCard: React.FC<{ item: SiteItem }> = ({ item }) => {
const { label: errorLabel, codes: errorCodes } = parseStatus(item.error);
const { label: warningLabel, codes: warningCodes } = parseStatus(item.warning);
return (
{formatItem("Reference", item.reference)}
{formatItem("Name", item.name)}
{formatItem("Variant", item.variant || "—")}
{formatItem(
"Price",
item.base_price !== item.price ? (
{item.base_price} {item.currency}
{item.price} {item.currency}
) : (
`${item.price} ${item.currency}`
),
)}
{errorLabel && } label={errorLabel} />}
{errorCodes.map((code) => (
))}
{warningLabel && (
} label={warningLabel} />
)}
{warningCodes.map((code) => (
))}
);
};
const SiteAvailability: ContribComponent = ({ data }) => {
const items = data.items ?? [];
return (
{items.length > 0 ? (
{items.map((item) => (
))}
) : (
No items found
)}
);
};
export default SiteAvailability;