import { jsx, jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { createContext, useCallback, useContext, useEffect, useId, useState } from "react";
import { semantic } from "../theme/palette.js";
const FooterContext = createContext(null);
function FooterProvider({ children }) {
  const [stack, setStack] = useState(() => /* @__PURE__ */ new Map());
  const register = useCallback((id, hints) => {
    setStack((prev) => {
      const next = new Map(prev);
      next.set(id, hints);
      return next;
    });
  }, []);
  const unregister = useCallback((id) => {
    setStack((prev) => {
      const next = new Map(prev);
      next.delete(id);
      return next;
    });
  }, []);
  return /* @__PURE__ */ jsx(FooterContext.Provider, { value: { register, unregister, stack }, children });
}
function useFooterHints(hints) {
  const ctx = useContext(FooterContext);
  const id = useId();
  const hintsKey = hints.join("|");
  useEffect(() => {
    if (!ctx) {
      if (process.env.NODE_ENV !== "production" && hints.length > 0) {
        process.stderr.write(
          `useFooterHints called outside FooterProvider; hints dropped: ${hints.join(" \xB7 ")}
`
        );
      }
      return;
    }
    ctx.register(id, hints);
    return () => {
      ctx.unregister(id);
    };
  }, [hintsKey]);
}
function Footer({ width }) {
  const ctx = useContext(FooterContext);
  const top = ctx ? Array.from(ctx.stack.values()).at(-1) ?? [] : [];
  if (width === "tiny" && top.length > 0) {
    const mid = Math.ceil(top.length / 2);
    const firstLine = top.slice(0, mid);
    const secondLine = top.slice(mid);
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingX: 1, children: [
      /* @__PURE__ */ jsx(Text, { color: semantic.textDim, children: firstLine.join(" \xB7 ") }),
      secondLine.length > 0 && /* @__PURE__ */ jsx(Text, { color: semantic.textDim, children: secondLine.join(" \xB7 ") })
    ] });
  }
  return /* @__PURE__ */ jsx(Box, { paddingX: 1, children: /* @__PURE__ */ jsx(Text, { color: semantic.textDim, children: top.join(" \xB7 ") }) });
}
export {
  Footer,
  FooterProvider,
  useFooterHints
};
//# sourceMappingURL=Footer.js.map