import { jsx, jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import Gradient from "ink-gradient";
import { semantic, palette } from "../theme/palette.js";
import { reducedMotion } from "../theme/motion-guard.js";
import { spinner } from "../theme/motion.js";
import { useState, useEffect } from "react";
function toastColor(sev) {
  return sev === "ok" ? semantic.ok : sev === "warn" ? semantic.warn : semantic.err;
}
const GRADIENT_PHASES = [
  [palette.pink, palette.purple],
  [palette.purple, palette.pink]
];
function Header(props) {
  const rm = reducedMotion();
  const [busy, setBusy] = useState(() => props.activityStore?.busy() ?? false);
  const [pulseFrame, setPulseFrame] = useState(0);
  const [gradientPhase, setGradientPhase] = useState(0);
  useEffect(() => {
    if (!props.activityStore) return;
    const unsub = props.activityStore.subscribe(() => {
      setBusy(props.activityStore.busy());
    });
    return unsub;
  }, [props.activityStore]);
  useEffect(() => {
    if (rm || !busy) {
      setPulseFrame(0);
      return;
    }
    const t = setInterval(() => setPulseFrame((f) => (f + 1) % spinner.pulse.length), 250);
    return () => clearInterval(t);
  }, [rm, busy]);
  useEffect(() => {
    if (rm || !busy) {
      setGradientPhase(0);
      return;
    }
    const t = setInterval(() => setGradientPhase((p) => (p + 1) % GRADIENT_PHASES.length), 80);
    return () => clearInterval(t);
  }, [rm, busy]);
  const dotColor = props.status === "ok" ? semantic.ok : props.status === "warn" ? semantic.warn : semantic.err;
  const dotChar = busy && !rm ? spinner.pulse[pulseFrame] : spinner.pulse[0];
  const gradientColors = GRADIENT_PHASES[gradientPhase];
  const width = props.width ?? "wide";
  const title = width !== "wide" ? /* @__PURE__ */ jsx(Text, { color: semantic.textDim, children: props.breadcrumb ?? "wigolo" }) : rm ? /* @__PURE__ */ jsx(Text, { color: semantic.accent, bold: true, children: "wigolo" }) : /* @__PURE__ */ jsx(Gradient, { colors: [...gradientColors], children: /* @__PURE__ */ jsx(Text, { bold: true, children: "wigolo" }) });
  const rightInfo = (() => {
    if (width === "tiny") return null;
    if (props.saveLabel !== void 0) {
      return /* @__PURE__ */ jsx(Text, { color: dotColor, children: props.saveLabel });
    }
    if (props.toast) {
      return /* @__PURE__ */ jsx(Text, { color: toastColor(props.toast.severity), children: props.toast.message });
    }
    if (props.pending > 0) {
      return /* @__PURE__ */ jsxs(Text, { color: semantic.accent, children: [
        props.pending,
        " pending"
      ] });
    }
    return null;
  })();
  return /* @__PURE__ */ jsxs(Box, { justifyContent: "space-between", paddingX: 1, children: [
    title,
    /* @__PURE__ */ jsxs(Box, { gap: 2, children: [
      /* @__PURE__ */ jsx(Text, { color: dotColor, children: dotChar }),
      rightInfo
    ] })
  ] });
}
export {
  Header,
  toastColor
};
//# sourceMappingURL=Header.js.map