"use strict";
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const styles_lib_components_Accordion_AccordionItem_cjs = require("../Accordion/AccordionItem.cjs");
const assert = require("assert");
const styles_lib_components_Box_Box_cjs = require("../Box/Box.cjs");
const styles_lib_components_Divider_Divider_cjs = require("../Divider/Divider.cjs");
const styles_lib_components_Hidden_Hidden_cjs = require("../Hidden/Hidden.cjs");
const styles_lib_components_Hidden_Hidden_css_cjs = require("../Hidden/Hidden.css.cjs");
const styles_lib_utils_align_cjs = require("../../utils/align.cjs");
const styles_lib_components_Column_Column_cjs = require("../Column/Column.cjs");
const styles_lib_css_negativeMargin_negativeMargin_cjs = require("../../css/negativeMargin/negativeMargin.cjs");
const styles_lib_css_atoms_sprinkles_css_cjs = require("../../css/atoms/sprinkles.css.cjs");
const styles_lib_components_Accordion_Accordion_cjs = require("../Accordion/Accordion.cjs");
const _interopDefaultCompat = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const assert__default = /* @__PURE__ */ _interopDefaultCompat(assert);
const alignToDisplay = {
  left: "block",
  center: "flex",
  right: "flex"
};
const validStackComponents = ["div", "span", "ol", "ul"];
const useStackItem = ({ align, space, component }) => ({
  paddingTop: space,
  display: component === "span" ? "block" : void 0,
  // If we're aligned left across all screen sizes,
  // there's actually no alignment work to do.
  ...align === "left" ? null : {
    display: styles_lib_css_atoms_sprinkles_css_cjs.mapResponsiveValue(align, (value) => alignToDisplay[value]),
    flexDirection: "column",
    alignItems: styles_lib_utils_align_cjs.alignToFlexAlign(align)
  }
});
const extractHiddenPropsFromChild = (child) => child && typeof child === "object" && "type" in child && child.type === styles_lib_components_Hidden_Hidden_cjs.Hidden ? child.props : null;
const resolveHiddenProps = ({ screen, above, below }) => screen ? [true, true, true, true] : styles_lib_components_Hidden_Hidden_cjs.resolveResponsiveRangeProps({
  above,
  below
});
const calculateHiddenStackItemProps = (stackItemProps, [hiddenOnMobile, hiddenOnTablet, hiddenOnDesktop, hiddenOnWide]) => {
  const normalizedValue = styles_lib_css_atoms_sprinkles_css_cjs.normalizeResponsiveValue(
    stackItemProps.display !== void 0 ? stackItemProps.display : "block"
  );
  const {
    mobile: displayMobile = "block",
    tablet: displayTablet = displayMobile,
    desktop: displayDesktop = displayTablet,
    wide: displayWide = displayDesktop
  } = normalizedValue;
  return {
    ...stackItemProps,
    display: styles_lib_components_Column_Column_cjs.optimizeResponsiveArray([
      hiddenOnMobile ? "none" : displayMobile,
      hiddenOnTablet ? "none" : displayTablet,
      hiddenOnDesktop ? "none" : displayDesktop,
      hiddenOnWide ? "none" : displayWide
    ])
  };
};
const Stack = ({
  component = "div",
  children,
  space = "none",
  align = "left",
  dividers = false,
  data,
  ...restProps
}) => {
  assert__default.default(
    validStackComponents.includes(component),
    `Invalid Stack component: '${component}'. Should be one of [${validStackComponents.map((c) => `'${c}'`).join(", ")}]`
  );
  const stackItemProps = useStackItem({ space, align, component });
  const stackItems = styles_lib_components_Accordion_AccordionItem_cjs.flattenChildren(children);
  const isList = component === "ol" || component === "ul";
  const stackItemComponent = isList ? "li" : component;
  let firstItemOnMobile = null;
  let firstItemOnTablet = null;
  let firstItemOnDesktop = null;
  let firstItemOnWide = null;
  return /* @__PURE__ */ jsxRuntime.jsx(
    styles_lib_components_Box_Box_cjs.Box,
    {
      component,
      display: component === "span" ? "block" : void 0,
      className: styles_lib_css_negativeMargin_negativeMargin_cjs.negativeMargin("top", space),
      ...styles_lib_components_Accordion_Accordion_cjs.buildDataAttributes({ data, validateRestProps: restProps }),
      children: React.Children.map(stackItems, (child, index) => {
        assert__default.default(
          !(typeof child === "object" && child.type === styles_lib_components_Hidden_Hidden_cjs.Hidden && child.props.inline !== void 0),
          'The "inline" prop is invalid on Hidden elements within a Stack'
        );
        const hiddenProps = extractHiddenPropsFromChild(child);
        const hidden = hiddenProps ? resolveHiddenProps(hiddenProps) : [false, false, false, false];
        const [hiddenOnMobile, hiddenOnTablet, hiddenOnDesktop, hiddenOnWide] = hidden;
        const responsivelyHidden = hiddenOnMobile || hiddenOnTablet || hiddenOnDesktop || hiddenOnWide;
        if (firstItemOnMobile === null && !hiddenOnMobile) {
          firstItemOnMobile = index;
        }
        if (firstItemOnTablet === null && !hiddenOnTablet) {
          firstItemOnTablet = index;
        }
        if (firstItemOnDesktop === null && !hiddenOnDesktop) {
          firstItemOnDesktop = index;
        }
        if (firstItemOnWide === null && !hiddenOnWide) {
          firstItemOnWide = index;
        }
        return /* @__PURE__ */ jsxRuntime.jsxs(
          styles_lib_components_Box_Box_cjs.Box,
          {
            component: stackItemComponent,
            className: [
              hiddenProps && hiddenProps.print ? styles_lib_components_Hidden_Hidden_css_cjs.hiddenOnPrint : null
            ],
            ...responsivelyHidden ? calculateHiddenStackItemProps(stackItemProps, hidden) : stackItemProps,
            children: [
              dividers && index > 0 ? /* @__PURE__ */ jsxRuntime.jsx(
                styles_lib_components_Box_Box_cjs.Box,
                {
                  component: "span",
                  width: "full",
                  paddingBottom: space,
                  display: styles_lib_components_Column_Column_cjs.optimizeResponsiveArray([
                    index === firstItemOnMobile ? "none" : "block",
                    index === firstItemOnTablet ? "none" : "block",
                    index === firstItemOnDesktop ? "none" : "block",
                    index === firstItemOnWide ? "none" : "block"
                  ]),
                  children: typeof dividers === "string" ? /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Divider_Divider_cjs.Divider, { weight: dividers }) : /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Divider_Divider_cjs.Divider, {})
                }
              ) : null,
              hiddenProps ? hiddenProps.children : child
            ]
          }
        );
      })
    }
  );
};
exports.Stack = Stack;
exports.validStackComponents = validStackComponents;
