import { useEffect, useState } from "preact/hooks";
import Markdown from "../Markdown.jsx";
import OffersSwitches from "../Switches.jsx";
import InternetAddonsModal from "./InternetAddonsModal.jsx";
import "../../styles/cards.css";
function formatMoney(amount, currency = "PLN") {
if (typeof amount !== "number" || Number.isNaN(amount)) return "";
try {
return new Intl.NumberFormat("pl-PL", {
style: "",
currency,
maximumFractionDigits: 0,
}).format(amount);
} catch {
return String(amount);
}
}
// ✅ mapper: InternetCard(YAML) + match + labels -> plan (dla modala)
function mapCardToPlan(card, match, labels, waluta) {
const baseParams = Array.isArray(card?.parametry) ? card.parametry : [];
const features = baseParams.map((p) => ({
label: p.label,
value: p.value,
}));
// na końcu jak parametry:
features.push({ label: "Umowa", value: labels?.umowa || "—" });
features.push({
label: "Aktywacja",
value:
typeof match?.aktywacja === "number" ? formatMoney(match.aktywacja, waluta) : "—",
});
return {
name: card?.nazwa || "—",
price_monthly: typeof match?.miesiecznie === "number" ? match.miesiecznie : 0,
price_installation: typeof match?.aktywacja === "number" ? match.aktywacja : 0,
features,
};
}
/**
* @param {{
* title?: string,
* description?: string,
* cards?: any[],
* waluta?: string,
* cenaOpis?: string,
* phoneCards?: any[],
* addons?: any[],
* addonsCenaOpis?: string,
* switches?: any[] // ✅ NOWE: przełączniki z YAML
* }} props
*/
export default function InternetCards({
title = "",
description = "",
cards = [],
waluta = "PLN",
cenaOpis = "zł/mies.",
phoneCards = [],
addons = [],
addonsCenaOpis = "zł/mies.",
switches = [], // ✅ NOWE
}) {
const visibleCards = Array.isArray(cards) ? cards : [];
// switch state (teraz idzie z OffersSwitches na podstawie YAML)
const [selected, setSelected] = useState({});
const [labels, setLabels] = useState({});
// modal
const [addonsModalOpen, setAddonsModalOpen] = useState(false);
const [activePlan, setActivePlan] = useState(null);
useEffect(() => {
if (typeof window !== "undefined" && window.fuzSwitchState) {
const { selected: sel, labels: labs } = window.fuzSwitchState;
if (sel) setSelected(sel);
if (labs) setLabels(labs);
}
function handler(e) {
const detail = e?.detail || {};
if (detail.selected) setSelected(detail.selected);
if (detail.labels) setLabels(detail.labels);
}
window.addEventListener("fuz:switch-change", handler);
return () => window.removeEventListener("fuz:switch-change", handler);
}, []);
return (
Brak dostępnych pakietów.{title}
}
{description && (