"use client";
'use strict';

var React = require('react');
var clsx = require('clsx');
var tailwindMerge = require('tailwind-merge');
var jsxRuntime = require('react/jsx-runtime');

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespace(React);

// components/blocks/banner/banner-delivery-countdown.tsx
function cn(...inputs) {
  return tailwindMerge.twMerge(clsx.clsx(inputs));
}
var svgCache = /* @__PURE__ */ new Map();
function DynamicIcon({
  name,
  size = 28,
  color,
  className,
  alt
}) {
  const [svgContent, setSvgContent] = React__namespace.useState(null);
  const [isLoading, setIsLoading] = React__namespace.useState(true);
  const [error, setError] = React__namespace.useState(null);
  const { url, iconName } = React__namespace.useMemo(() => {
    const separator = name.includes("/") ? "/" : ":";
    const [prefix, iconName2] = name.split(separator);
    const baseUrl = `https://icons.opensite.ai/api/icon/${prefix}/${iconName2}?format=svg&width=${size}&height=${size}`;
    return {
      url: baseUrl,
      iconName: iconName2
    };
  }, [name, size]);
  React__namespace.useEffect(() => {
    let isMounted = true;
    const fetchSvg = async () => {
      const cached = svgCache.get(url);
      if (cached) {
        if (isMounted) {
          setSvgContent(cached);
          setIsLoading(false);
        }
        return;
      }
      try {
        setIsLoading(true);
        setError(null);
        const response = await fetch(url);
        if (!response.ok) {
          throw new Error(`Failed to fetch icon: ${response.status}`);
        }
        let svg = await response.text();
        svg = processSvgForCurrentColor(svg);
        svgCache.set(url, svg);
        if (isMounted) {
          setSvgContent(svg);
          setIsLoading(false);
        }
      } catch (err) {
        if (isMounted) {
          setError(err instanceof Error ? err.message : "Failed to load icon");
          setIsLoading(false);
        }
      }
    };
    fetchSvg();
    return () => {
      isMounted = false;
    };
  }, [url]);
  if (isLoading) {
    return /* @__PURE__ */ jsxRuntime.jsx(
      "span",
      {
        className: cn("inline-block", className),
        style: { width: size, height: size },
        "aria-hidden": "true"
      }
    );
  }
  if (error || !svgContent) {
    return /* @__PURE__ */ jsxRuntime.jsx(
      "span",
      {
        className: cn("inline-block", className),
        style: { width: size, height: size },
        role: "img",
        "aria-label": alt || iconName
      }
    );
  }
  return /* @__PURE__ */ jsxRuntime.jsx(
    "span",
    {
      className: cn("inline-flex items-center justify-center", className),
      style: {
        width: size,
        height: size,
        color: color || "inherit"
      },
      role: "img",
      "aria-label": alt || iconName,
      dangerouslySetInnerHTML: { __html: svgContent }
    }
  );
}
function processSvgForCurrentColor(svg) {
  let processed = svg;
  processed = processed.replace(
    /stroke=["'](#000000|#000|black)["']/gi,
    'stroke="currentColor"'
  );
  processed = processed.replace(
    /fill=["'](#000000|#000|black)["']/gi,
    'fill="currentColor"'
  );
  return processed;
}
function BannerDeliveryCountdown({
  icon,
  iconName,
  deliveryDate,
  cutoffTime,
  prefixText,
  middleText,
  timerSlot,
  renderTimer,
  className,
  containerClassName,
  contentClassName,
  iconClassName,
  messageClassName,
  timerClassName,
  deliveryDateClassName
}) {
  const targetTime = cutoffTime;
  const [timeLeft, setTimeLeft] = React.useState({
    hours: 0,
    minutes: 0,
    seconds: 0
  });
  React.useEffect(() => {
    if (!targetTime) {
      return;
    }
    const calculateTimeLeft = () => {
      const now = (/* @__PURE__ */ new Date()).getTime();
      const target = targetTime.getTime();
      const diff = target - now;
      if (diff <= 0) {
        return { hours: 0, minutes: 0, seconds: 0 };
      }
      return {
        hours: Math.floor(diff / (1e3 * 60 * 60)),
        minutes: Math.floor(diff % (1e3 * 60 * 60) / (1e3 * 60)),
        seconds: Math.floor(diff % (1e3 * 60) / 1e3)
      };
    };
    setTimeLeft(calculateTimeLeft());
    const timer = setInterval(() => {
      setTimeLeft(calculateTimeLeft());
    }, 1e3);
    return () => clearInterval(timer);
  }, [targetTime]);
  const pad = React.useCallback((n) => n.toString().padStart(2, "0"), []);
  const iconContent = React.useMemo(() => {
    if (icon) return icon;
    if (!iconName) return null;
    return /* @__PURE__ */ jsxRuntime.jsx(DynamicIcon, { name: iconName, size: 16, className: iconClassName });
  }, [icon, iconName, iconClassName]);
  const timerContent = React.useMemo(() => {
    if (timerSlot) return timerSlot;
    if (renderTimer) return renderTimer(timeLeft);
    if (!targetTime) return null;
    return /* @__PURE__ */ jsxRuntime.jsxs("span", { className: cn("font-mono font-bold", timerClassName), children: [
      pad(timeLeft.hours),
      ":",
      pad(timeLeft.minutes),
      ":",
      pad(timeLeft.seconds)
    ] });
  }, [timerSlot, renderTimer, timeLeft, timerClassName, pad, targetTime]);
  const deliveryDateContent = React.useMemo(() => {
    if (!deliveryDate) return null;
    return typeof deliveryDate === "string" ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: cn("font-semibold", deliveryDateClassName), children: deliveryDate }) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: deliveryDateClassName, children: deliveryDate });
  }, [deliveryDate, deliveryDateClassName]);
  const messageParts = React.useMemo(() => {
    return [prefixText, timerContent, middleText, deliveryDateContent].filter(
      (part) => part !== null && part !== void 0
    );
  }, [prefixText, timerContent, middleText, deliveryDateContent]);
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className: cn("w-full bg-accent text-accent-foreground", className), children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: cn("container py-2.5", containerClassName), children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: cn("flex flex-wrap items-center justify-center gap-3 text-sm", contentClassName), children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cn("flex items-center gap-2", messageClassName), children: [
    iconContent,
    messageParts.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("span", { children: messageParts.map((part, index) => /* @__PURE__ */ jsxRuntime.jsxs(React__namespace.Fragment, { children: [
      index > 0 ? " " : null,
      part
    ] }, index)) })
  ] }) }) }) });
}

exports.BannerDeliveryCountdown = BannerDeliveryCountdown;
